Sequential Recommendation for cold-start users with meta transitional learning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. from collections import OrderedDict
  2. import torch
  3. import torch.nn as nn
  4. from torch.nn import functional as F
  5. from numpy import linalg as LA
  6. import numpy as np
  7. class Embedding(nn.Module):
  8. def __init__(self, num_ent, parameter):
  9. super(Embedding, self).__init__()
  10. # self.device = torch.device('cuda:0')
  11. self.device = torch.device(parameter['device'])
  12. self.es = parameter['embed_dim']
  13. self.embedding = nn.Embedding(num_ent + 1, self.es)
  14. nn.init.xavier_uniform_(self.embedding.weight)
  15. def forward(self, triples):
  16. idx = [[[t[0], t[2]] for t in batch] for batch in triples]
  17. idx = torch.LongTensor(idx).to(self.device)
  18. return self.embedding(idx)
  19. class MetaLearner(nn.Module):
  20. def __init__(self, K, embed_size=100, num_hidden1=500, num_hidden2=200, out_size=100, dropout_p=0.5):
  21. super(MetaLearner, self).__init__()
  22. self.embed_size = embed_size
  23. self.K = K
  24. # self.out_size = out_size
  25. # self.hidden_size = out_size
  26. self.out_size = embed_size
  27. self.hidden_size = embed_size
  28. # self.rnn = nn.LSTM(embed_size,self.hidden_size,2,dropout=0.2)
  29. self.rnn = nn.GRU(input_size=embed_size, hidden_size=self.embed_size * 2, num_layers=1)
  30. self.activation = nn.LeakyReLU()
  31. self.linear = nn.Linear(self.embed_size * 2, self.embed_size)
  32. self.norm = nn.BatchNorm1d(num_features=self.out_size)
  33. # nn.init.xavier_normal_(self.linear.weight)
  34. def forward(self, inputs, evaluation=False):
  35. size = inputs.shape
  36. x = torch.stack([inputs[:, 0, 0, :], inputs[:, 0, 1, :], inputs[:, 1, 1, :]], dim=1)
  37. x = x.transpose(0, 1)
  38. # _,(x,c) = self.rnn(x)
  39. x, c = self.rnn(x)
  40. x = x[-1]
  41. if not evaluation:
  42. x = x.squeeze(0)
  43. x = self.activation(x)
  44. x = self.linear(x)
  45. x = self.norm(x)
  46. return x.view(size[0], 1, 1, self.out_size)
  47. class EmbeddingLearner(nn.Module):
  48. def __init__(self):
  49. super(EmbeddingLearner, self).__init__()
  50. def forward(self, h, t, r, pos_num):
  51. score = -torch.norm(h + r - t, 2, -1).squeeze(2)
  52. p_score = score[:, :pos_num]
  53. n_score = score[:, pos_num:]
  54. return p_score, n_score
  55. def bpr_loss(p_scores, n_values, device):
  56. ratio = int(n_values.shape[1] / p_scores.shape[1])
  57. temp_pvalues = torch.tensor([], device=device)
  58. for i in range(p_scores.shape[1]):
  59. temp_pvalues = torch.cat((temp_pvalues, p_scores[:, i, None].expand(-1, ratio)), dim=1)
  60. d = torch.sub(temp_pvalues, n_values)
  61. t = F.logsigmoid(d)
  62. loss = -1 * (1.0 / n_values.shape[1]) * t.sum(dim=1)
  63. loss = loss.sum(dim=0)
  64. return loss
  65. def bpr_max_loss(p_scores, n_values, device):
  66. s = F.softmax(n_values, dim=1)
  67. ratio = int(n_values.shape[1] / p_scores.shape[1])
  68. temp_pvalues = torch.tensor([], device=device)
  69. for i in range(p_scores.shape[1]):
  70. temp_pvalues = torch.cat((temp_pvalues, p_scores[:, i, None].expand(-1, ratio)), dim=1)
  71. d = torch.sigmoid(torch.sub(temp_pvalues, n_values))
  72. t = torch.mul(s, d)
  73. loss = -1 * torch.log(t.sum(dim=1))
  74. loss = loss.sum()
  75. return loss
  76. def bpr_max_loss_regularized(p_scores, n_values, device, l=0.0001):
  77. s = F.softmax(n_values, dim=1)
  78. ratio = int(n_values.shape[1] / p_scores.shape[1])
  79. temp_pvalues = torch.tensor([], device=device)
  80. for i in range(p_scores.shape[1]):
  81. temp_pvalues = torch.cat((temp_pvalues, p_scores[:, i, None].expand(-1, ratio)), dim=1)
  82. d = torch.sigmoid(torch.sub(temp_pvalues, n_values))
  83. t = torch.mul(s, d)
  84. loss = -1 * torch.log(t.sum(dim=1))
  85. loss = loss.sum()
  86. loss2 = torch.mul(s, n_values ** 2)
  87. loss2 = loss2.sum(dim=1)
  88. loss2 = loss2.sum()
  89. return loss + l * loss2
  90. def top_loss(p_scores, n_values, device):
  91. ratio = int(n_values.shape[1] / p_scores.shape[1])
  92. temp_pvalues = torch.tensor([], device=device)
  93. for i in range(p_scores.shape[1]):
  94. temp_pvalues = torch.cat((temp_pvalues, p_scores[:, i, None].expand(-1, ratio)), dim=1)
  95. t1 = torch.sigmoid(torch.sub(n_values, temp_pvalues))
  96. t2 = torch.sigmoid(torch.pow(n_values, 2))
  97. t = torch.add(t1, t2)
  98. t = t.sum(dim=1)
  99. loss = t / n_values.shape[1]
  100. loss = loss.sum(dim=0)
  101. return loss
  102. class MetaTL(nn.Module):
  103. def __init__(self, itemnum, parameter):
  104. super(MetaTL, self).__init__()
  105. # self.device = torch.device(parameter['device'])
  106. self.device = parameter['device']
  107. self.beta = parameter['beta']
  108. # self.dropout_p = parameter['dropout_p']
  109. self.embed_dim = parameter['embed_dim']
  110. self.margin = parameter['margin']
  111. self.embedding = Embedding(itemnum, parameter)
  112. self.relation_learner = MetaLearner(parameter['K'] - 1, embed_size=self.embed_dim, num_hidden1=500,
  113. num_hidden2=200, out_size=100, dropout_p=0)
  114. self.embedding_learner = EmbeddingLearner()
  115. self.loss_func = nn.MarginRankingLoss(self.margin)
  116. # self.loss_func = bpr_max_loss
  117. # self.loss_func = bpr_loss
  118. self.rel_q_sharing = dict()
  119. def split_concat(self, positive, negative):
  120. pos_neg_e1 = torch.cat([positive[:, :, 0, :],
  121. negative[:, :, 0, :]], 1).unsqueeze(2)
  122. pos_neg_e2 = torch.cat([positive[:, :, 1, :],
  123. negative[:, :, 1, :]], 1).unsqueeze(2)
  124. return pos_neg_e1, pos_neg_e2
  125. def fast_forward(self, tasks, curr_rel=''):
  126. with torch.no_grad():
  127. sup = self.embedding(tasks)
  128. K = sup.shape[1]
  129. rel_q = self.rel_q_sharing[curr_rel]
  130. sup_neg_e1, sup_neg_e2 = sup[:, :, 0, :], sup[:, :, 1, :]
  131. a = sup_neg_e1.cpu().detach().numpy()
  132. b = rel_q.squeeze(1).cpu().detach().numpy()
  133. b = np.tile(b, (1, a.shape[-2], 1))
  134. c = sup_neg_e2.cpu().detach().numpy()
  135. # print(a.shape,b.shape,c.shape)
  136. scores = -LA.norm(a + b - c, 2, -1)
  137. return scores
  138. def forward(self, task, iseval=False, curr_rel=''):
  139. # transfer task string into embedding
  140. support, support_negative, query, negative = [self.embedding(t) for t in task]
  141. K = support.shape[1] # num of K
  142. num_sn = support_negative.shape[1] # num of support negative
  143. num_q = query.shape[1] # num of query
  144. num_n = negative.shape[1] # num of query negative
  145. rel = self.relation_learner(support, iseval)
  146. rel.retain_grad()
  147. rel_s = rel.expand(-1, K + num_sn, -1, -1)
  148. if iseval and curr_rel != '' and curr_rel in self.rel_q_sharing.keys():
  149. rel_q = self.rel_q_sharing[curr_rel]
  150. else:
  151. sup_neg_e1, sup_neg_e2 = self.split_concat(support, support_negative)
  152. p_score, n_score = self.embedding_learner(sup_neg_e1, sup_neg_e2, rel_s, K)
  153. y = torch.Tensor([1]).to(self.device)
  154. self.zero_grad()
  155. # sorted,indecies = torch.sort(n_score, descending=True,dim=1)
  156. # n_values = sorted[:,0:p_score.shape[1]]
  157. loss = self.loss_func(p_score, n_score, y)
  158. # loss = self.loss_func(p_score,n_score,device=self.device)
  159. loss.backward(retain_graph=True)
  160. grad_meta = rel.grad
  161. rel_q = rel - self.beta * grad_meta
  162. self.rel_q_sharing[curr_rel] = rel_q
  163. rel_q = rel_q.expand(-1, num_q + num_n, -1, -1)
  164. que_neg_e1, que_neg_e2 = self.split_concat(query, negative)
  165. p_score, n_score = self.embedding_learner(que_neg_e1, que_neg_e2, rel_q, num_q)
  166. return p_score, n_score