-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_inputs.py
74 lines (63 loc) · 2.94 KB
/
create_inputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random
import numpy as np
import pdb
import json
import pickle as pkl
class creator:
def __init__(self, embed_size, user_embed, item_embed, user_history, user_key, item_key, content_embed):
self.embed_size = embed_size
self.user_embed = np.load(user_embed)
self.item_embed = np.load(item_embed)
self.user_history = json.load(open(user_history))
self.user_key = pkl.load(open(user_key))
self.user_key_rev = {v: k for k, v in self.user_key.iteritems()}
self.item_key = pkl.load(open(item_key))
self.item_key_rev = {v: k for k, v in self.item_key.iteritems()}
self.content_embed = pkl.load(open(content_embed))
def create_input(self, inp_size, n_negs):
user_read = []
user = []
truth = []
pos_neg_left = []
pos_neg_right = []
test_left = []
test_right = []
user_list = []
user_read_list = []
item_list_left = self.item_embed
item_list_right = []
for i in range(self.item_embed.shape[0]) :
item_list_right.append(self.content_embed[str(self.item_key_rev[i])])
for i in range(self.user_embed.shape[0]):
total_hist = map(lambda x: self.content_embed[str(x)], self.user_history[self.user_key_rev[i]])
total_hist2 = map(lambda x: self.item_embed[self.item_key[x]], self.user_history[self.user_key_rev[i]])
size = len(total_hist)
read_hist = []
user_list.append(self.user_embed[i])
test_left.append(total_hist2[-1])
test_right.append(total_hist[-1])
if size > inp_size - 1:
n_pos = size - inp_size - 1
read_hist = total_hist[:inp_size]
else:
n_pos = 1
read_hist = []
padding = np.zeros(self.embed_size)
for j in range(inp_size - size + 2):
read_hist.append(padding)
for j in range(size - 2):
read_hist.append(total_hist[j])
user_read_list.append(read_hist)
for j in range(n_pos):
user_read.append(read_hist)
user.append(self.user_embed[i])
pos_neg_right.append(total_hist[size-2-j])
pos_neg_left.append(total_hist2[size-2-j])
truth.append(1)
for j in range(n_pos*n_negs):
user_read.append(read_hist)
user.append(self.user_embed[i])
pos_neg_right.append(random.choice(self.content_embed.values()))
pos_neg_left.append(random.choice(self.item_embed))
truth.append(0)
return np.array(user), np.array(user_read), np.array(pos_neg_left), np.array(pos_neg_right), np.array(truth), np.array(test_left), np.array(test_right), np.array(user_list), np.array(item_list_left), np.array(item_list_right), np.array(user_read_list)