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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from collections import OrderedDict
  2. import torch
  3. import torch.nn as nn
  4. from torch.nn import functional as F
  5. class Embedding(nn.Module):
  6. def __init__(self, num_ent, parameter):
  7. super(Embedding, self).__init__()
  8. # self.device = torch.device('cuda:0')
  9. self.device = torch.device(parameter['device'])
  10. self.es = parameter['embed_dim']
  11. self.embedding = nn.Embedding(num_ent + 1, self.es)
  12. nn.init.xavier_uniform_(self.embedding.weight)
  13. def forward(self, triples):
  14. idx = [[[t[0], t[2]] for t in batch] for batch in triples]
  15. idx = torch.LongTensor(idx).to(self.device)
  16. return self.embedding(idx)
  17. class MetaLearner(nn.Module):
  18. def __init__(self, K, embed_size=100, num_hidden1=500, num_hidden2=200, out_size=100, dropout_p=0.5):
  19. super(MetaLearner, self).__init__()
  20. self.embed_size = embed_size
  21. self.K = K
  22. # self.out_size = out_size
  23. # self.hidden_size = out_size
  24. self.out_size = embed_size
  25. self.hidden_size = embed_size
  26. self.rnn = nn.LSTM(embed_size,self.hidden_size,2,dropout=0.2)
  27. # nn.init.xavier_normal_(self.rnn.all_weights)
  28. def forward(self, inputs):
  29. size = inputs.shape
  30. x = torch.stack([inputs[:,0,0,:],inputs[:,0,1,:],inputs[:,1,1,:]],dim=1)
  31. x = x.transpose(0,1)
  32. _,(x,c) = self.rnn(x)
  33. x = x[-1]
  34. x = x.squeeze(0)
  35. return x.view(size[0], 1, 1, self.out_size)
  36. class EmbeddingLearner(nn.Module):
  37. def __init__(self):
  38. super(EmbeddingLearner, self).__init__()
  39. def forward(self, h, t, r, pos_num):
  40. score = -torch.norm(h + r - t, 2, -1).squeeze(2)
  41. p_score = score[:, :pos_num]
  42. n_score = score[:, pos_num:]
  43. return p_score, n_score
  44. class MetaTL(nn.Module):
  45. def __init__(self, itemnum, parameter):
  46. super(MetaTL, self).__init__()
  47. self.device = torch.device(parameter['device'])
  48. self.beta = parameter['beta']
  49. # self.dropout_p = parameter['dropout_p']
  50. self.embed_dim = parameter['embed_dim']
  51. self.margin = parameter['margin']
  52. self.embedding = Embedding(itemnum, parameter)
  53. self.relation_learner = MetaLearner(parameter['K'] - 1, embed_size=self.embed_dim, num_hidden1=500,
  54. num_hidden2=200, out_size=100, dropout_p=0)
  55. self.embedding_learner = EmbeddingLearner()
  56. self.loss_func = nn.MarginRankingLoss(self.margin)
  57. self.rel_q_sharing = dict()
  58. def split_concat(self, positive, negative):
  59. pos_neg_e1 = torch.cat([positive[:, :, 0, :],
  60. negative[:, :, 0, :]], 1).unsqueeze(2)
  61. pos_neg_e2 = torch.cat([positive[:, :, 1, :],
  62. negative[:, :, 1, :]], 1).unsqueeze(2)
  63. return pos_neg_e1, pos_neg_e2
  64. def forward(self, task, iseval=False, curr_rel=''):
  65. # transfer task string into embedding
  66. support, support_negative, query, negative = [self.embedding(t) for t in task]
  67. K = support.shape[1] # num of K
  68. num_sn = support_negative.shape[1] # num of support negative
  69. num_q = query.shape[1] # num of query
  70. num_n = negative.shape[1] # num of query negative
  71. rel = self.relation_learner(support)
  72. rel.retain_grad()
  73. rel_s = rel.expand(-1, K+num_sn, -1, -1)
  74. if iseval and curr_rel != '' and curr_rel in self.rel_q_sharing.keys():
  75. rel_q = self.rel_q_sharing[curr_rel]
  76. else:
  77. sup_neg_e1, sup_neg_e2 = self.split_concat(support, support_negative)
  78. p_score, n_score = self.embedding_learner(sup_neg_e1, sup_neg_e2, rel_s, K)
  79. y = torch.Tensor([1]).to(self.device)
  80. self.zero_grad()
  81. loss = self.loss_func(p_score, n_score, y)
  82. loss.backward(retain_graph=True)
  83. grad_meta = rel.grad
  84. rel_q = rel - self.beta*grad_meta
  85. self.rel_q_sharing[curr_rel] = rel_q
  86. rel_q = rel_q.expand(-1, num_q + num_n, -1, -1)
  87. que_neg_e1, que_neg_e2 = self.split_concat(query, negative)
  88. p_score, n_score = self.embedding_learner(que_neg_e1, que_neg_e2, rel_q, num_q)
  89. return p_score, n_score