-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathnearest_embed.py
152 lines (128 loc) · 5.44 KB
/
nearest_embed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import numpy as np
import torch
from torch import nn
from torch.autograd import Function, Variable
import torch.nn.functional as F
class NearestEmbedFunc(Function):
"""
Input:
------
x - (batch_size, emb_dim, *)
Last dimensions may be arbitrary
emb - (emb_dim, num_emb)
"""
@staticmethod
def forward(ctx, input, emb):
if input.size(1) != emb.size(0):
raise RuntimeError('invalid argument: input.size(1) ({}) must be equal to emb.size(0) ({})'.
format(input.size(1), emb.size(0)))
# save sizes for backward
ctx.batch_size = input.size(0)
ctx.num_latents = int(np.prod(np.array(input.size()[2:])))
ctx.emb_dim = emb.size(0)
ctx.num_emb = emb.size(1)
ctx.input_type = type(input)
ctx.dims = list(range(len(input.size())))
# expand to be broadcast-able
x_expanded = input.unsqueeze(-1)
num_arbitrary_dims = len(ctx.dims) - 2
if num_arbitrary_dims:
emb_expanded = emb.view(
emb.shape[0], *([1] * num_arbitrary_dims), emb.shape[1])
else:
emb_expanded = emb
# find nearest neighbors
dist = torch.norm(x_expanded - emb_expanded, 2, 1)
_, argmin = dist.min(-1)
shifted_shape = [input.shape[0], *
list(input.shape[2:]), input.shape[1]]
result = emb.t().index_select(0, argmin.view(-1)
).view(shifted_shape).permute(0, ctx.dims[-1], *ctx.dims[1:-1])
ctx.save_for_backward(argmin)
return result.contiguous(), argmin
@staticmethod
def backward(ctx, grad_output, argmin=None):
grad_input = grad_emb = None
if ctx.needs_input_grad[0]:
grad_input = grad_output
if ctx.needs_input_grad[1]:
argmin, = ctx.saved_variables
latent_indices = torch.arange(ctx.num_emb).type_as(argmin)
idx_choices = (argmin.view(-1, 1) ==
latent_indices.view(1, -1)).type_as(grad_output.data)
n_idx_choice = idx_choices.sum(0)
n_idx_choice[n_idx_choice == 0] = 1
idx_avg_choices = idx_choices / n_idx_choice
grad_output = grad_output.permute(0, *ctx.dims[2:], 1).contiguous()
grad_output = grad_output.view(
ctx.batch_size * ctx.num_latents, ctx.emb_dim)
grad_emb = torch.sum(grad_output.data.view(-1, ctx.emb_dim, 1) *
idx_avg_choices.view(-1, 1, ctx.num_emb), 0)
return grad_input, grad_emb, None, None
def nearest_embed(x, emb):
return NearestEmbedFunc().apply(x, emb)
class NearestEmbed(nn.Module):
def __init__(self, num_embeddings, embeddings_dim):
super(NearestEmbed, self).__init__()
self.weight = nn.Parameter(torch.rand(embeddings_dim, num_embeddings))
def forward(self, x, weight_sg=False):
"""Input:
---------
x - (batch_size, emb_size, *)
"""
return nearest_embed(x, self.weight.detach() if weight_sg else self.weight)
# adapted from https://github.com/rosinality/vq-vae-2-pytorch/blob/master/vqvae.py#L25
# that adapted from https://github.com/deepmind/sonnet
class NearestEmbedEMA(nn.Module):
def __init__(self, n_emb, emb_dim, decay=0.99, eps=1e-5):
super(NearestEmbedEMA, self).__init__()
self.decay = decay
self.eps = eps
self.embeddings_dim = emb_dim
self.n_emb = n_emb
self.emb_dim = emb_dim
embed = torch.rand(emb_dim, n_emb)
self.register_buffer('weight', embed)
self.register_buffer('cluster_size', torch.zeros(n_emb))
self.register_buffer('embed_avg', embed.clone())
def forward(self, x):
"""Input:
---------
x - (batch_size, emb_size, *)
"""
dims = list(range(len(x.size())))
x_expanded = x.unsqueeze(-1)
num_arbitrary_dims = len(dims) - 2
if num_arbitrary_dims:
emb_expanded = self.weight.view(
self.emb_dim, *([1] * num_arbitrary_dims), self.n_emb)
else:
emb_expanded = self.weight
# find nearest neighbors
dist = torch.norm(x_expanded - emb_expanded, 2, 1)
_, argmin = dist.min(-1)
shifted_shape = [x.shape[0], *list(x.shape[2:]), x.shape[1]]
result = self.weight.t().index_select(
0, argmin.view(-1)).view(shifted_shape).permute(0, dims[-1], *dims[1:-1])
if self.training:
latent_indices = torch.arange(self.n_emb).type_as(argmin)
emb_onehot = (argmin.view(-1, 1) ==
latent_indices.view(1, -1)).type_as(x.data)
n_idx_choice = emb_onehot.sum(0)
n_idx_choice[n_idx_choice == 0] = 1
flatten = x.permute(
1, 0, *dims[-2:]).contiguous().view(x.shape[1], -1)
self.cluster_size.data.mul_(self.decay).add_(
1 - self.decay, n_idx_choice
)
embed_sum = flatten @ emb_onehot
self.embed_avg.data.mul_(self.decay).add_(
1 - self.decay, embed_sum)
n = self.cluster_size.sum()
cluster_size = (
(self.cluster_size + self.eps) /
(n + self.n_emb * self.eps) * n
)
embed_normalized = self.embed_avg / cluster_size.unsqueeze(0)
self.weight.data.copy_(embed_normalized)
return result, argmin