def forward(self, pos_u, pos_v, neg_u, neg_v):
losses = []
emb_v = []
for i in range(len(pos_v)):
emb_v_v = self.u_embeddings(Variable(torch.LongTensor(pos_v[i])))
emb_v_v_numpy = emb_v_v.data.numpy()
emb_v_v_numpy = np.sum(emb_v_v_numpy, axis=0)
emb_v_v_list = emb_v_v_numpy.tolist()
emb_v.append(emb_v_v_list)
emb_v = Variable(torch.FloatTensor(emb_v))
emb_u = self.v_embeddings(Variable(torch.LongTensor(pos_u)))
score = torch.mul(emb_u, emb_v)
score = torch.sum(score, dim=1)
score = F.logsigmoid(score)
losses.append(sum(score))
neg_emb_v = []
for i in range(len(neg_v)):
neg_emb_v_v = self.u_embeddings(Variable(torch.LongTensor(neg_v[i])))
neg_emb_v_v_numpy = neg_emb_v_v.data.numpy()
neg_emb_v_v_numpy = np.sum(neg_emb_v_v_numpy, axis=0)
neg_emb_v_v_list = neg_emb_v_v_numpy.tolist()
neg_emb_v.append(neg_emb_v_v_list)
neg_emb_v = Variable(torch.FloatTensor(neg_emb_v))
neg_emb_u = self.v_embeddings(Variable(torch.LongTensor(neg_u)))
neg_score = torch.mul(neg_emb_u, neg_emb_v)
neg_score = torch.sum(neg_score, dim=1)
neg_score = F.logsigmoid(-1 * neg_score)
losses.append(sum(neg_score))
return -1 * sum(losses)
评论列表
文章目录