|
|
|
|
|
|
|
|
self.embedding = nn.Embedding(num_ent + 1, self.es) |
|
|
self.embedding = nn.Embedding(num_ent + 1, self.es) |
|
|
nn.init.xavier_uniform_(self.embedding.weight) |
|
|
nn.init.xavier_uniform_(self.embedding.weight) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, triples): |
|
|
def forward(self, triples): |
|
|
idx = [[[t[0], t[2]] for t in batch] for batch in triples] |
|
|
idx = [[[t[0], t[2]] for t in batch] for batch in triples] |
|
|
idx = torch.LongTensor(idx).to(self.device) |
|
|
idx = torch.LongTensor(idx).to(self.device) |
|
|
|
|
|
|
|
|
self.embed_size = embed_size |
|
|
self.embed_size = embed_size |
|
|
self.K = K |
|
|
self.K = K |
|
|
self.out_size = out_size |
|
|
self.out_size = out_size |
|
|
self.rel_fc1 = nn.Sequential(OrderedDict([ |
|
|
|
|
|
('fc', nn.Linear(2*embed_size, num_hidden1)), |
|
|
|
|
|
('bn', nn.BatchNorm1d(K)), |
|
|
|
|
|
('relu', nn.LeakyReLU()), |
|
|
|
|
|
('drop', nn.Dropout(p=dropout_p)), |
|
|
|
|
|
])) |
|
|
|
|
|
self.rel_fc2 = nn.Sequential(OrderedDict([ |
|
|
|
|
|
('fc', nn.Linear(num_hidden1, num_hidden2)), |
|
|
|
|
|
('bn', nn.BatchNorm1d(K)), |
|
|
|
|
|
('relu', nn.LeakyReLU()), |
|
|
|
|
|
('drop', nn.Dropout(p=dropout_p)), |
|
|
|
|
|
])) |
|
|
|
|
|
self.rel_fc3 = nn.Sequential(OrderedDict([ |
|
|
|
|
|
('fc', nn.Linear(num_hidden2, out_size)), |
|
|
|
|
|
('bn', nn.BatchNorm1d(K)), |
|
|
|
|
|
])) |
|
|
|
|
|
nn.init.xavier_normal_(self.rel_fc1.fc.weight) |
|
|
|
|
|
nn.init.xavier_normal_(self.rel_fc2.fc.weight) |
|
|
|
|
|
nn.init.xavier_normal_(self.rel_fc3.fc.weight) |
|
|
|
|
|
|
|
|
self.hidden_size = out_size |
|
|
|
|
|
self.rnn = nn.LSTM(embed_size,self.hidden_size,1) |
|
|
|
|
|
# nn.init.xavier_normal_(self.rnn.all_weights) |
|
|
|
|
|
|
|
|
def forward(self, inputs): |
|
|
def forward(self, inputs): |
|
|
size = inputs.shape |
|
|
size = inputs.shape |
|
|
x = inputs.contiguous().view(size[0], size[1], -1) |
|
|
|
|
|
x = self.rel_fc1(x) |
|
|
|
|
|
x = self.rel_fc2(x) |
|
|
|
|
|
x = self.rel_fc3(x) |
|
|
|
|
|
x = torch.mean(x, 1) |
|
|
|
|
|
|
|
|
x = inputs.contiguous().view(size[0]*2, size[2], size[3]) |
|
|
|
|
|
x = x.transpose(1,0) |
|
|
|
|
|
|
|
|
|
|
|
_,(x,c) = self.rnn(x) |
|
|
|
|
|
x = x[-1] |
|
|
|
|
|
|
|
|
|
|
|
x = x.squeeze(0) |
|
|
|
|
|
x = x.reshape(size[0],2,100) |
|
|
|
|
|
x = torch.mean(x, 1) |
|
|
return x.view(size[0], 1, 1, self.out_size) |
|
|
return x.view(size[0], 1, 1, self.out_size) |
|
|
|
|
|
|
|
|
|
|
|
|