-
Notifications
You must be signed in to change notification settings - Fork 82
/
library_models.py
224 lines (178 loc) · 9.07 KB
/
library_models.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'''
This is a supporting library with the code of the model.
Paper: Predicting Dynamic Embedding Trajectory in Temporal Interaction Networks. S. Kumar, X. Zhang, J. Leskovec. ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD), 2019.
'''
from __future__ import division
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.autograd import Variable
from torch import optim
import numpy as np
import math, random
import sys
from collections import defaultdict
import os
import gpustat
from itertools import chain
from tqdm import tqdm, trange, tqdm_notebook, tnrange
import csv
import json
PATH = "./"
try:
get_ipython
trange = tnrange
tqdm = tqdm_notebook
except NameError:
pass
total_reinitialization_count = 0
# A NORMALIZATION LAYER
class NormalLinear(nn.Linear):
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.normal_(0, stdv)
if self.bias is not None:
self.bias.data.normal_(0, stdv)
# THE JODIE MODULE
class JODIE(nn.Module):
def __init__(self, args, num_features, num_users, num_items):
super(JODIE,self).__init__()
print("*** Initializing the JODIE model ***")
self.modelname = args.model
self.embedding_dim = args.embedding_dim
self.num_users = num_users
self.num_items = num_items
self.user_static_embedding_size = num_users
self.item_static_embedding_size = num_items
print("Initializing user and item embeddings")
self.initial_user_embedding = nn.Parameter(torch.Tensor(args.embedding_dim))
self.initial_item_embedding = nn.Parameter(torch.Tensor(args.embedding_dim))
rnn_input_size_items = rnn_input_size_users = self.embedding_dim + 1 + num_features
print("Initializing user and item RNNs")
self.item_rnn = nn.RNNCell(rnn_input_size_users, self.embedding_dim)
self.user_rnn = nn.RNNCell(rnn_input_size_items, self.embedding_dim)
print("Initializing linear layers")
self.linear_layer1 = nn.Linear(self.embedding_dim, 50)
self.linear_layer2 = nn.Linear(50, 2)
self.prediction_layer = nn.Linear(self.user_static_embedding_size + self.item_static_embedding_size + self.embedding_dim * 2, self.item_static_embedding_size + self.embedding_dim)
self.embedding_layer = NormalLinear(1, self.embedding_dim)
print("*** JODIE initialization complete ***\n\n")
def forward(self, user_embeddings, item_embeddings, timediffs=None, features=None, select=None):
if select == 'item_update':
input1 = torch.cat([user_embeddings, timediffs, features], dim=1)
item_embedding_output = self.item_rnn(input1, item_embeddings)
return F.normalize(item_embedding_output)
elif select == 'user_update':
input2 = torch.cat([item_embeddings, timediffs, features], dim=1)
user_embedding_output = self.user_rnn(input2, user_embeddings)
return F.normalize(user_embedding_output)
elif select == 'project':
user_projected_embedding = self.context_convert(user_embeddings, timediffs, features)
#user_projected_embedding = torch.cat([input3, item_embeddings], dim=1)
return user_projected_embedding
def context_convert(self, embeddings, timediffs, features):
new_embeddings = embeddings * (1 + self.embedding_layer(timediffs))
return new_embeddings
def predict_label(self, user_embeddings):
X_out = nn.ReLU()(self.linear_layer1(user_embeddings))
X_out = self.linear_layer2(X_out)
return X_out
def predict_item_embedding(self, user_embeddings):
X_out = self.prediction_layer(user_embeddings)
return X_out
# INITIALIZE T-BATCH VARIABLES
def reinitialize_tbatches():
global current_tbatches_interactionids, current_tbatches_user, current_tbatches_item, current_tbatches_timestamp, current_tbatches_feature, current_tbatches_label, current_tbatches_previous_item
global tbatchid_user, tbatchid_item, current_tbatches_user_timediffs, current_tbatches_item_timediffs, current_tbatches_user_timediffs_next
# list of users of each tbatch up to now
current_tbatches_interactionids = defaultdict(list)
current_tbatches_user = defaultdict(list)
current_tbatches_item = defaultdict(list)
current_tbatches_timestamp = defaultdict(list)
current_tbatches_feature = defaultdict(list)
current_tbatches_label = defaultdict(list)
current_tbatches_previous_item = defaultdict(list)
current_tbatches_user_timediffs = defaultdict(list)
current_tbatches_item_timediffs = defaultdict(list)
current_tbatches_user_timediffs_next = defaultdict(list)
# the latest tbatch a user is in
tbatchid_user = defaultdict(lambda: -1)
# the latest tbatch a item is in
tbatchid_item = defaultdict(lambda: -1)
global total_reinitialization_count
total_reinitialization_count +=1
# CALCULATE LOSS FOR THE PREDICTED USER STATE
def calculate_state_prediction_loss(model, tbatch_interactionids, user_embeddings_time_series, y_true, loss_function):
# PREDCIT THE LABEL FROM THE USER DYNAMIC EMBEDDINGS
prob = model.predict_label(user_embeddings_time_series[tbatch_interactionids,:])
y = Variable(torch.LongTensor(y_true).cuda()[tbatch_interactionids])
loss = loss_function(prob, y)
return loss
# SAVE TRAINED MODEL TO DISK
def save_model(model, optimizer, args, epoch, user_embeddings, item_embeddings, train_end_idx, user_embeddings_time_series=None, item_embeddings_time_series=None, path=PATH):
print("*** Saving embeddings and model ***")
state = {
'user_embeddings': user_embeddings.data.cpu().numpy(),
'item_embeddings': item_embeddings.data.cpu().numpy(),
'epoch': epoch,
'state_dict': model.state_dict(),
'optimizer' : optimizer.state_dict(),
'train_end_idx': train_end_idx
}
if user_embeddings_time_series is not None:
state['user_embeddings_time_series'] = user_embeddings_time_series.data.cpu().numpy()
state['item_embeddings_time_series'] = item_embeddings_time_series.data.cpu().numpy()
directory = os.path.join(path, 'saved_models/%s' % args.network)
if not os.path.exists(directory):
os.makedirs(directory)
filename = os.path.join(directory, "checkpoint.%s.ep%d.tp%.1f.pth.tar" % (args.model, epoch, args.train_proportion))
torch.save(state, filename)
print("*** Saved embeddings and model to file: %s ***\n\n" % filename)
# LOAD PREVIOUSLY TRAINED AND SAVED MODEL
def load_model(model, optimizer, args, epoch):
modelname = args.model
filename = PATH + "saved_models/%s/checkpoint.%s.ep%d.tp%.1f.pth.tar" % (args.network, modelname, epoch, args.train_proportion)
checkpoint = torch.load(filename)
print("Loading saved embeddings and model: %s" % filename)
args.start_epoch = checkpoint['epoch']
user_embeddings = Variable(torch.from_numpy(checkpoint['user_embeddings']).cuda())
item_embeddings = Variable(torch.from_numpy(checkpoint['item_embeddings']).cuda())
try:
train_end_idx = checkpoint['train_end_idx']
except KeyError:
train_end_idx = None
try:
user_embeddings_time_series = Variable(torch.from_numpy(checkpoint['user_embeddings_time_series']).cuda())
item_embeddings_time_series = Variable(torch.from_numpy(checkpoint['item_embeddings_time_series']).cuda())
except:
user_embeddings_time_series = None
item_embeddings_time_series = None
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
return [model, optimizer, user_embeddings, item_embeddings, user_embeddings_time_series, item_embeddings_time_series, train_end_idx]
# SET USER AND ITEM EMBEDDINGS TO THE END OF THE TRAINING PERIOD
def set_embeddings_training_end(user_embeddings, item_embeddings, user_embeddings_time_series, item_embeddings_time_series, user_data_id, item_data_id, train_end_idx):
userid2lastidx = {}
for cnt, userid in enumerate(user_data_id[:train_end_idx]):
userid2lastidx[userid] = cnt
itemid2lastidx = {}
for cnt, itemid in enumerate(item_data_id[:train_end_idx]):
itemid2lastidx[itemid] = cnt
try:
embedding_dim = user_embeddings_time_series.size(1)
except:
embedding_dim = user_embeddings_time_series.shape[1]
for userid in userid2lastidx:
user_embeddings[userid, :embedding_dim] = user_embeddings_time_series[userid2lastidx[userid]]
for itemid in itemid2lastidx:
item_embeddings[itemid, :embedding_dim] = item_embeddings_time_series[itemid2lastidx[itemid]]
user_embeddings.detach_()
item_embeddings.detach_()
# SELECT THE GPU WITH MOST FREE MEMORY TO SCHEDULE JOB
def select_free_gpu():
mem = []
gpus = list(set(range(torch.cuda.device_count()))) # list(set(X)) is done to shuffle the array
for i in gpus:
gpu_stats = gpustat.GPUStatCollection.new_query()
mem.append(gpu_stats.jsonify()["gpus"][i]["memory.used"])
return str(gpus[np.argmin(mem)])