forked from TimDettmers/ConvE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
153 lines (118 loc) · 5.99 KB
/
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
import torch
from torch.nn import functional as F, Parameter
from torch.autograd import Variable
from torch.nn.init import xavier_normal_, xavier_uniform_
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
class Complex(torch.nn.Module):
def __init__(self, args, num_entities, num_relations):
super(Complex, self).__init__()
self.num_entities = num_entities
self.emb_e_real = torch.nn.Embedding(num_entities, args.embedding_dim, padding_idx=0)
self.emb_e_img = torch.nn.Embedding(num_entities, args.embedding_dim, padding_idx=0)
self.emb_rel_real = torch.nn.Embedding(num_relations, args.embedding_dim, padding_idx=0)
self.emb_rel_img = torch.nn.Embedding(num_relations, args.embedding_dim, padding_idx=0)
self.inp_drop = torch.nn.Dropout(args.input_drop)
self.loss = torch.nn.BCELoss()
def init(self):
xavier_normal_(self.emb_e_real.weight.data)
xavier_normal_(self.emb_e_img.weight.data)
xavier_normal_(self.emb_rel_real.weight.data)
xavier_normal_(self.emb_rel_img.weight.data)
def forward(self, e1, rel):
e1_embedded_real = self.emb_e_real(e1).squeeze()
rel_embedded_real = self.emb_rel_real(rel).squeeze()
e1_embedded_img = self.emb_e_img(e1).squeeze()
rel_embedded_img = self.emb_rel_img(rel).squeeze()
e1_embedded_real = self.inp_drop(e1_embedded_real)
rel_embedded_real = self.inp_drop(rel_embedded_real)
e1_embedded_img = self.inp_drop(e1_embedded_img)
rel_embedded_img = self.inp_drop(rel_embedded_img)
# complex space bilinear product (equivalent to HolE)
realrealreal = torch.mm(e1_embedded_real*rel_embedded_real, self.emb_e_real.weight.transpose(1,0))
realimgimg = torch.mm(e1_embedded_real*rel_embedded_img, self.emb_e_img.weight.transpose(1,0))
imgrealimg = torch.mm(e1_embedded_img*rel_embedded_real, self.emb_e_img.weight.transpose(1,0))
imgimgreal = torch.mm(e1_embedded_img*rel_embedded_img, self.emb_e_real.weight.transpose(1,0))
pred = realrealreal + realimgimg + imgrealimg - imgimgreal
pred = torch.sigmoid(pred)
return pred
class DistMult(torch.nn.Module):
def __init__(self, args, num_entities, num_relations):
super(DistMult, self).__init__()
self.emb_e = torch.nn.Embedding(num_entities, args.embedding_dim, padding_idx=0)
self.emb_rel = torch.nn.Embedding(num_relations, args.embedding_dim, padding_idx=0)
self.inp_drop = torch.nn.Dropout(args.input_drop)
self.loss = torch.nn.BCELoss()
def init(self):
xavier_normal_(self.emb_e.weight.data)
xavier_normal_(self.emb_rel.weight.data)
def forward(self, e1, rel):
e1_embedded= self.emb_e(e1)
rel_embedded= self.emb_rel(rel)
e1_embedded = e1_embedded.squeeze()
rel_embedded = rel_embedded.squeeze()
e1_embedded = self.inp_drop(e1_embedded)
rel_embedded = self.inp_drop(rel_embedded)
pred = torch.mm(e1_embedded*rel_embedded, self.emb_e.weight.transpose(1,0))
pred = torch.sigmoid(pred)
return pred
class ConvE(torch.nn.Module):
def __init__(self, args, num_entities, num_relations):
super(ConvE, self).__init__()
self.emb_e = torch.nn.Embedding(num_entities, args.embedding_dim, padding_idx=0)
self.emb_rel = torch.nn.Embedding(num_relations, args.embedding_dim, padding_idx=0)
self.inp_drop = torch.nn.Dropout(args.input_drop)
self.hidden_drop = torch.nn.Dropout(args.hidden_drop)
self.feature_map_drop = torch.nn.Dropout2d(args.feat_drop)
self.loss = torch.nn.BCELoss()
self.emb_dim1 = args.embedding_shape1
self.emb_dim2 = args.embedding_dim // self.emb_dim1
self.conv1 = torch.nn.Conv2d(1, 32, (3, 3), 1, 0, bias=args.use_bias)
self.bn0 = torch.nn.BatchNorm2d(1)
self.bn1 = torch.nn.BatchNorm2d(32)
self.bn2 = torch.nn.BatchNorm1d(args.embedding_dim)
self.register_parameter('b', Parameter(torch.zeros(num_entities)))
self.fc = torch.nn.Linear(args.hidden_size,args.embedding_dim)
print(num_entities, num_relations)
def init(self):
xavier_normal_(self.emb_e.weight.data)
xavier_normal_(self.emb_rel.weight.data)
def forward(self, e1, rel):
e1_embedded= self.emb_e(e1).view(-1, 1, self.emb_dim1, self.emb_dim2)
rel_embedded = self.emb_rel(rel).view(-1, 1, self.emb_dim1, self.emb_dim2)
stacked_inputs = torch.cat([e1_embedded, rel_embedded], 2)
stacked_inputs = self.bn0(stacked_inputs)
x= self.inp_drop(stacked_inputs)
x= self.conv1(x)
x= self.bn1(x)
x= F.relu(x)
x = self.feature_map_drop(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
x = self.hidden_drop(x)
x = self.bn2(x)
x = F.relu(x)
x = torch.mm(x, self.emb_e.weight.transpose(1,0))
x += self.b.expand_as(x)
pred = torch.sigmoid(x)
return pred
# Add your own model here
class MyModel(torch.nn.Module):
def __init__(self, num_entities, num_relations):
super(DistMult, self).__init__()
self.emb_e = torch.nn.Embedding(num_entities, args.embedding_dim, padding_idx=0)
self.emb_rel = torch.nn.Embedding(num_relations, args.embedding_dim, padding_idx=0)
self.inp_drop = torch.nn.Dropout(args.input_drop)
self.loss = torch.nn.BCELoss()
def init(self):
xavier_normal_(self.emb_e.weight.data)
xavier_normal_(self.emb_rel.weight.data)
def forward(self, e1, rel):
e1_embedded= self.emb_e(e1)
rel_embedded= self.emb_rel(rel)
# Add your model function here
# The model function should operate on the embeddings e1 and rel
# and output scores for all entities (you will need a projection layer
# with output size num_relations (from constructor above)
# generate output scores here
prediction = torch.sigmoid(output)
return prediction