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.

sampler.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import sys
  2. import copy
  3. import torch
  4. import random
  5. import numpy as np
  6. from collections import defaultdict, Counter
  7. from multiprocessing import Process, Queue
  8. def random_neq(l, r, s, user_train,usernum):
  9. # t = np.random.randint(l, r)
  10. # while t in s:
  11. # t = np.random.randint(l, r)
  12. # return t
  13. user = np.random.choice(1, usernum + 1)
  14. candid_item = user_train[user][np.random.randint(0, len(user_train[user]))]
  15. while candid_item in s:
  16. user = np.random.randint(1, usernum + 1)
  17. candid_item = user_train[user][np.random.randint(0, len(user_train[user]))]
  18. return candid_item
  19. def random_negetive_batch(l, r, s, user_train,usernum, batch_users):
  20. user = np.random.choice(batch_users)
  21. candid_item = user_train[user][np.random.randint(0, len(user_train[user]))]
  22. while candid_item in s:
  23. user = np.random.choice(batch_users)
  24. candid_item = user_train[user][np.random.randint(0, len(user_train[user]))]
  25. return candid_item
  26. def sample_function_mixed(user_train, usernum, itemnum, batch_size, maxlen, result_queue, SEED,number_of_neg):
  27. def sample(user,batch_users):
  28. if random.random()<=0.5:
  29. # user = np.random.randint(1, usernum + 1)
  30. # while len(user_train[user]) <= 1: user = np.random.randint(1, usernum + 1)
  31. seq = np.zeros([maxlen], dtype=np.int32)
  32. pos = np.zeros([maxlen], dtype=np.int32)
  33. neg = np.zeros([maxlen*number_of_neg], dtype=np.int32)
  34. if len(user_train[user]) < maxlen:
  35. nxt_idx = len(user_train[user]) - 1
  36. else:
  37. nxt_idx = np.random.randint(maxlen,len(user_train[user]))
  38. nxt = user_train[user][nxt_idx]
  39. idx = maxlen - 1
  40. ts = set(user_train[user])
  41. for i in reversed(user_train[user][min(0, nxt_idx - 1 - maxlen) : nxt_idx - 1]):
  42. seq[idx] = i
  43. pos[idx] = nxt
  44. # if nxt != 0: neg[idx] = random_neq(1, itemnum + 1, ts, user_train,usernum)
  45. nxt = i
  46. idx -= 1
  47. if idx == -1: break
  48. for i in range(len(neg)):
  49. # neg[i] = random_neq(1, itemnum + 1, ts, user_train,usernum)
  50. neg[i] = random_negetive_batch(1, itemnum + 1, ts, user_train, usernum, batch_users = batch_users)
  51. curr_rel = user
  52. support_triples, support_negative_triples, query_triples, negative_triples = [], [], [], []
  53. for idx in range(maxlen-1):
  54. support_triples.append([seq[idx],curr_rel,pos[idx]])
  55. # support_negative_triples.append([seq[idx],curr_rel,neg[idx]])
  56. # support_negative_triples.append([seq[-1], curr_rel, neg[idx]])
  57. # for idx in range(maxlen*30 - 1):
  58. # support_negative_triples.append([seq[-1], curr_rel, neg[idx]])
  59. for j in range(number_of_neg):
  60. for idx in range(maxlen-1):
  61. support_negative_triples.append([seq[idx], curr_rel, neg[j*maxlen + idx]])
  62. query_triples.append([seq[-1],curr_rel,pos[-1]])
  63. negative_triples.append([seq[-1],curr_rel,neg[-1]])
  64. return support_triples, support_negative_triples, query_triples, negative_triples, curr_rel
  65. else:
  66. # print("bug happened in sample_function_mixed")
  67. # user = np.random.randint(1, usernum + 1)
  68. # while len(user_train[user]) <= 1: user = np.random.randint(1, usernum + 1)
  69. seq = np.zeros([maxlen], dtype=np.int32)
  70. pos = np.zeros([maxlen], dtype=np.int32)
  71. neg = np.zeros([maxlen*number_of_neg], dtype=np.int32)
  72. list_idx = random.sample([i for i in range(len(user_train[user]))], maxlen + 1)
  73. list_item = [user_train[user][i] for i in sorted(list_idx)]
  74. nxt = list_item[-1]
  75. idx = maxlen - 1
  76. ts = set(user_train[user])
  77. for i in reversed(list_item[:-1]):
  78. seq[idx] = i
  79. pos[idx] = nxt
  80. # if nxt != 0: neg[idx] = random_neq(1, itemnum + 1, ts)
  81. nxt = i
  82. idx -= 1
  83. if idx == -1: break
  84. curr_rel = user
  85. support_triples, support_negative_triples, query_triples, negative_triples = [], [], [], []
  86. for i in range(len(neg)):
  87. # neg[i] = random_neq(1, itemnum + 1, ts, user_train,usernum)
  88. neg[i] = random_negetive_batch(1, itemnum + 1, ts, user_train, usernum, batch_users = batch_users)
  89. for j in range(number_of_neg):
  90. for idx in range(maxlen-1):
  91. support_negative_triples.append([seq[idx], curr_rel, neg[j*maxlen + idx]])
  92. for idx in range(maxlen-1):
  93. support_triples.append([seq[idx],curr_rel,pos[idx]])
  94. # support_negative_triples.append([seq[idx],curr_rel,neg[idx]])
  95. query_triples.append([seq[-1],curr_rel,pos[-1]])
  96. negative_triples.append([seq[-1],curr_rel,neg[-1]])
  97. return support_triples, support_negative_triples, query_triples, negative_triples, curr_rel
  98. np.random.seed(SEED)
  99. while True:
  100. one_batch = []
  101. users = []
  102. for i in range(batch_size):
  103. user = np.random.randint(1, usernum + 1)
  104. while len(user_train[user]) <= 1: user = np.random.randint(1, usernum + 1)
  105. users.append(user)
  106. for i in range(batch_size):
  107. one_batch.append(sample(user = users[i], batch_users = users))
  108. support, support_negative, query, negative, curr_rel = zip(*one_batch)
  109. result_queue.put(([support, support_negative, query, negative], curr_rel))
  110. class WarpSampler(object):
  111. def __init__(self, User, usernum, itemnum, batch_size=64, maxlen=10, n_workers=1,params = None):
  112. self.result_queue = Queue(maxsize=n_workers * 10)
  113. self.processors = []
  114. for i in range(n_workers):
  115. self.processors.append(
  116. Process(target=sample_function_mixed, args=(User,
  117. usernum,
  118. itemnum,
  119. batch_size,
  120. maxlen,
  121. self.result_queue,
  122. np.random.randint(2e9),
  123. params['number_of_neg']
  124. )))
  125. self.processors[-1].daemon = True
  126. self.processors[-1].start()
  127. def next_batch(self):
  128. return self.result_queue.get()
  129. def close(self):
  130. for p in self.processors:
  131. p.terminate()
  132. p.join()