This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodels.py
74 lines (43 loc) · 2.22 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Code2Vec(nn.Module):
def __init__(self, nodes_dim, paths_dim, embedding_dim, output_dim, dropout):
super().__init__()
self.node_embedding = nn.Embedding(nodes_dim, embedding_dim)
self.path_embedding = nn.Embedding(paths_dim, embedding_dim)
self.W = nn.Parameter(torch.randn(1, embedding_dim, 3*embedding_dim))
self.a = nn.Parameter(torch.randn(1, embedding_dim, 1))
self.out = nn.Linear(embedding_dim, output_dim)
self.do = nn.Dropout(dropout)
def forward(self, starts, paths, ends):
#starts = paths = ends = [batch size, max length]
W = self.W.repeat(starts.shape[0], 1, 1)
#W = [batch size, embedding dim, embedding dim * 3]
embedded_starts = self.node_embedding(starts)
embedded_paths = self.path_embedding(paths)
embedded_ends = self.node_embedding(ends)
#embedded_* = [batch size, max length, embedding dim]
c = self.do(torch.cat((embedded_starts, embedded_paths, embedded_ends), dim=2))
#c = [batch size, max length, embedding dim * 3]
c = c.permute(0, 2, 1)
#c = [batch size, embedding dim * 3, max length]
x = torch.tanh(torch.bmm(W, c))
#x = [batch size, embedding dim, max length]
x = x.permute(0, 2, 1)
#x = [batch size, max length, embedding dim]
a = self.a.repeat(starts.shape[0], 1, 1)
#a = [batch size, embedding dim, 1]
z = torch.bmm(x, a).squeeze(2)
#z = [batch size, max length]
z = F.softmax(z, dim=1)
#z = [batch size, max length]
z = z.unsqueeze(2)
#z = [batch size, max length, 1]
x = x.permute(0, 2, 1)
#x = [batch size, embedding dim, max length]
v = torch.bmm(x, z).squeeze(2)
#v = [batch size, embedding dim]
out = self.out(v)
#out = [batch size, output dim]
return out