-
Notifications
You must be signed in to change notification settings - Fork 1
/
lorentzian_model.py
259 lines (213 loc) · 8.04 KB
/
lorentzian_model.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import numpy as np
from numpy.random import choice, randint
import torch as th
from torch import nn
from torch.autograd import Function, Variable
from torch.utils.data import Dataset
from collections import defaultdict as ddict
import torch.nn.functional as F
import random
eps = 1e-5
class Arcosh(Function):
def __init__(self, eps=eps):
super(Arcosh, self).__init__()
self.eps = eps
def forward(self, x):
self.z = th.sqrt(x * x - 1)
return th.log(x + self.z)
def backward(self, g):
z = th.clamp(self.z, min=eps)
z = g / z
return z
class PoincareDistance(Function):
boundary = 1 - eps
def grad(self, x, v, sqnormx, sqnormv, sqdist):
alpha = (1 - sqnormx)
beta = (1 - sqnormv)
z = 1 + 2 * sqdist / (alpha * beta)
a = ((sqnormv - 2 * th.sum(x * v, dim=-1) + 1) / th.pow(alpha, 2)).unsqueeze(-1).expand_as(x)
a = a * x - v / alpha.unsqueeze(-1).expand_as(v)
z = th.sqrt(th.pow(z, 2) - 1)
z = th.clamp(z * beta, min=eps).unsqueeze(-1)
return 4 * a / z.expand_as(x)
def forward(self, u, v):
self.save_for_backward(u, v)
self.squnorm = th.clamp(th.sum(u * u, dim=-1), 0, self.boundary)
self.sqvnorm = th.clamp(th.sum(v * v, dim=-1), 0, self.boundary)
self.sqdist = th.sum(th.pow(u - v, 2), dim=-1)
x = self.sqdist / ((1 - self.squnorm) * (1 - self.sqvnorm)) * 2 + 1
# arcosh
z = th.sqrt(th.pow(x, 2) - 1)
return th.log(x + z)
def backward(self, g):
u, v = self.saved_tensors
g = g.unsqueeze(-1)
gu = self.grad(u, v, self.squnorm, self.sqvnorm, self.sqdist)
gv = self.grad(v, u, self.sqvnorm, self.squnorm, self.sqdist)
return g.expand_as(gu) * gu, g.expand_as(gv) * gv
class LorentzianDistance(nn.Module):
def __init__(self, radius=1, dim=None):
super(LorentzianDistance, self).__init__()
def forward(self, u, v):
u0 = th.sqrt(th.pow(u,2).sum(-1, keepdim=True) + self.beta)
v0 = -th.sqrt(th.pow(v,2).sum(-1, keepdim=True) + self.beta)
u = th.cat((u,u0),-1)
v = th.cat((v,v0),-1)
result = - 2 * self.beta - 2 *th.sum(u * v, dim=-1)
return result
class EuclideanDistance(nn.Module):
def __init__(self, radius=1, dim=None):
super(EuclideanDistance, self).__init__()
def forward(self, u, v):
return th.sum(th.pow(u - v, 2), dim=-1)
class TranseDistance(nn.Module):
def __init__(self, radius=1, dim=None):
super(TranseDistance, self).__init__()
self.r = nn.Parameter(th.randn(dim).view(1, dim))
def forward(self, u, v):
if u.dim() == 3:
r = self.r.unsqueeze(0).expand(v.size(0), v.size(1), self.r.size(1))
else:
r = self.r.expand(v.size(0), self.r.size(1))
return th.sum(th.pow(u - v + r, 2), dim=-1)
class Embedding(nn.Module):
def __init__(self, size, dim, dist=PoincareDistance, max_norm=1, beta=None, regul_param=None):
super(Embedding, self).__init__()
self.dim = dim
self.lt = nn.Embedding(
size, dim,
max_norm=max_norm,
sparse=True,
scale_grad_by_freq=False
)
self.dist = dist
if beta is not None:
self.dist.beta = beta
self.init_weights()
def init_weights(self, scale=1e-4):
self.lt.state_dict()['weight'].uniform_(-scale, scale)
def forward(self, inputs):
e = self.lt(inputs)
fval = self._forward(e)
return fval
def embedding_norm(self, inputs):
e = self.lt(inputs)
fval = self._embedding_norm(e)
return fval
def embedding(self):
return list(self.lt.parameters())[0].data.cpu().numpy()
class SNEmbedding(Embedding):
def __init__(self, size, dim, dist=PoincareDistance, max_norm=1, beta=None, regul_param=None):
super(SNEmbedding, self).__init__(size, dim, dist, max_norm, beta= regul_param)
self.lossfn = nn.CrossEntropyLoss
self.regul_param = regul_param
def _forward(self, e):
o = e.narrow(1, 1, e.size(1) - 1)
s = e.narrow(1, 0, 1).expand_as(o)
dists = self.dist()(s, o).squeeze(-1)
return -dists
def _embedding_norm(self, e):
return th.sum(e * e, dim=-1)
def loss(self, preds, targets, weight=None, size_average=True):
lossfn = self.lossfn(size_average=size_average, weight=weight)
return lossfn(preds, targets)
def rank_loss(self, norms, ordered_ranks, ignore_constraints = 0.95):
if self.regul_param == 0.0:
return 0.0
if self.regul_param is None:
return 0.0
loss = 0
nb_examples = len(ordered_ranks)
for i in range(nb_examples):
ranki = ordered_ranks[i]
for j in range(i):
rankj = ordered_ranks[j]
if ranki > rankj:
if random.random() < ignore_constraints:
continue
loss += F.relu(norms[j] - norms[i])
elif ranki < rankj:
if random.random() < ignore_constraints:
continue
loss += F.relu(norms[i] - norms[j])
return self.regul_param*loss
class GraphDataset(Dataset):
_ntries = 10
_dampening = 1
def __init__(self, idx, objects, nnegs, unigram_size=1e8):
print('Indexing data')
self.idx = idx
self.nnegs = nnegs
self.burnin = False
self.objects = objects
self.max_tries = self.nnegs * self._ntries
self._weights = ddict(lambda: ddict(int))
self._counts = np.ones(len(objects), dtype=np.float)
for i in range(idx.size(0)):
t, h, w = self.idx[i]
#t = t.data.tolist()
#h = h.data.tolist()
#w = w.data.tolist()
self._counts[h] += w
self._weights[t][h] += w
self._weights = dict(self._weights)
nents = int(np.array(list(self._weights.keys())).max() + 1)
assert len(objects) == nents, 'Number of objects do no match'
if unigram_size > 0:
c = self._counts ** self._dampening
self.unigram_table = choice(
len(objects),
size=int(unigram_size),
p=(c / c.sum())
)
def __len__(self):
return self.idx.size(0)
@classmethod
def collate(cls, batch):
inputs, targets = zip(*batch)
return Variable(th.cat(inputs, 0)), Variable(th.cat(targets, 0))
class SNGraphDataset(GraphDataset):
model_name = '%s_%s_dim%d'
def __getitem__(self, i):
t, h, _ = self.idx[i]
negs = set()
ntries = 0
nnegs = self.nnegs
if self.burnin:
nnegs *= 0.1
while ntries < self.max_tries and len(negs) < nnegs:
if self.burnin:
n = randint(0, len(self.unigram_table))
n = int(self.unigram_table[n])
else:
n = randint(0, len(self.objects))
if n not in self._weights[t]:
negs.add(n)
ntries += 1
if len(negs) == 0:
negs.add(t)
ix = [t, h] + list(negs)
while len(ix) < nnegs + 2:
ix.append(ix[randint(2, len(ix))])
return th.LongTensor(ix).view(1, len(ix)), th.zeros(1).long()
@classmethod
def initialize(cls, distfn, opt, idx, objects, max_norm=1):
conf = []
model_name = cls.model_name % (opt.dset, opt.distfn, opt.dim)
data = cls(idx, objects, opt.negs)
model = SNEmbedding(
len(data.objects),
opt.dim,
dist=distfn,
max_norm=max_norm,
beta=opt.beta,
regul_param=opt.lambdaparameter
)
data.objects = objects
return model, data, model_name, conf