forked from sjvasquez/instacart-basket-prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn_product.py
278 lines (239 loc) · 10.9 KB
/
rnn_product.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import numpy as np
import tensorflow as tf
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from data_frame import DataFrame
from tf_utils import lstm_layer, time_distributed_dense_layer, dense_layer, sequence_log_loss, temporal_convolution_layer
from tf_base_model import TFBaseModel
class DataReader(object):
def __init__(self, data_dir):
data_cols = [
'user_id',
'product_id',
'aisle_id',
'department_id',
'is_ordered_history',
'index_in_order_history',
'order_dow_history',
'order_hour_history',
'days_since_prior_order_history',
'order_size_history',
'reorder_size_history',
'order_is_weekend_history',
'order_part_of_day_history',
'order_number_history',
'history_length',
'product_name',
'product_name_length',
'eval_set',
'label'
]
data = [np.load(os.path.join(data_dir, '{}.npy'.format(i)), mmap_mode='r') for i in data_cols]
self.test_df = DataFrame(columns=data_cols, data=data)
print self.test_df.shapes()
print 'loaded data'
self.train_df, self.val_df = self.test_df.train_test_split(train_size=0.9)
print 'train size', len(self.train_df)
print 'val size', len(self.val_df)
print 'test size', len(self.test_df)
def train_batch_generator(self, batch_size):
return self.batch_generator(
batch_size=batch_size,
df=self.train_df,
shuffle=True,
num_epochs=10000,
is_test=False
)
def val_batch_generator(self, batch_size):
return self.batch_generator(
batch_size=batch_size,
df=self.val_df,
shuffle=True,
num_epochs=10000,
is_test=False
)
def test_batch_generator(self, batch_size):
return self.batch_generator(
batch_size=batch_size,
df=self.test_df,
shuffle=False,
num_epochs=1,
is_test=True
)
def batch_generator(self, batch_size, df, shuffle=True, num_epochs=10000, is_test=False):
batch_gen = df.batch_generator(batch_size, shuffle=shuffle, num_epochs=num_epochs, allow_smaller_final_batch=is_test)
for batch in batch_gen:
batch['order_dow_history'] = np.roll(batch['order_dow_history'], -1, axis=1)
batch['order_hour_history'] = np.roll(batch['order_hour_history'], -1, axis=1)
batch['days_since_prior_order_history'] = np.roll(batch['days_since_prior_order_history'], -1, axis=1)
batch['order_is_weekend_history'] = np.roll(batch['order_is_weekend_history'], -1, axis=1)
batch['order_part_of_day_history'] = np.roll(batch['order_part_of_day_history'], -1, axis=1)
batch['order_number_history'] = np.roll(batch['order_number_history'], -1, axis=1)
batch['next_is_ordered'] = np.roll(batch['is_ordered_history'], -1, axis=1)
batch['is_none'] = batch['product_id'] == 0
if not is_test:
batch['history_length'] = batch['history_length'] - 1
yield batch
class rnn(TFBaseModel):
def __init__(self, lstm_size=300, **kwargs):
self.lstm_size = lstm_size
super(rnn, self).__init__(**kwargs)
def calculate_loss(self):
x = self.get_input_sequences()
preds = self.calculate_outputs(x)
loss = sequence_log_loss(self.next_is_ordered, preds, self.history_length, 100)
return loss
def get_input_sequences(self):
self.user_id = tf.placeholder(tf.int32, [None])
self.product_id = tf.placeholder(tf.int32, [None])
self.aisle_id = tf.placeholder(tf.int32, [None])
self.department_id = tf.placeholder(tf.int32, [None])
self.is_none = tf.placeholder(tf.int32, [None])
self.history_length = tf.placeholder(tf.int32, [None])
self.is_ordered_history = tf.placeholder(tf.int32, [None, 100])
self.index_in_order_history = tf.placeholder(tf.int32, [None, 100])
self.order_dow_history = tf.placeholder(tf.int32, [None, 100])
self.order_hour_history = tf.placeholder(tf.int32, [None, 100])
self.days_since_prior_order_history = tf.placeholder(tf.int32, [None, 100])
self.order_size_history = tf.placeholder(tf.int32, [None, 100])
self.reorder_size_history = tf.placeholder(tf.int32, [None, 100])
self.order_is_weekend_history = tf.placeholder(tf.int32, [None, 100])
self.order_part_of_day_history = tf.placeholder(tf.int32, [None, 100])
self.order_number_history = tf.placeholder(tf.int32, [None, 100])
self.product_name = tf.placeholder(tf.int32, [None, 30])
self.product_name_length = tf.placeholder(tf.int32, [None])
self.next_is_ordered = tf.placeholder(tf.int32, [None, 100])
self.keep_prob = tf.placeholder(tf.float32)
self.is_training = tf.placeholder(tf.bool)
# product data
product_embeddings = tf.get_variable(
name='product_embeddings',
shape=[50000, self.lstm_size],
dtype=tf.float32
)
aisle_embeddings = tf.get_variable(
name='aisle_embeddings',
shape=[250, 50],
dtype=tf.float32
)
department_embeddings = tf.get_variable(
name='department_embeddings',
shape=[50, 10],
dtype=tf.float32
)
product_names = tf.one_hot(self.product_name, 2532)
product_names = tf.reduce_max(product_names, 1)
product_names = dense_layer(product_names, 100, activation=tf.nn.relu)
is_none = tf.cast(tf.expand_dims(self.is_none, 1), tf.float32)
x_product = tf.concat([
tf.nn.embedding_lookup(product_embeddings, self.product_id),
tf.nn.embedding_lookup(aisle_embeddings, self.aisle_id),
tf.nn.embedding_lookup(department_embeddings, self.department_id),
is_none,
product_names
], axis=1)
x_product = tf.tile(tf.expand_dims(x_product, 1), (1, 100, 1))
# user data
user_embeddings = tf.get_variable(
name='user_embeddings',
shape=[207000, self.lstm_size],
dtype=tf.float32
)
x_user = tf.nn.embedding_lookup(user_embeddings, self.user_id)
x_user = tf.tile(tf.expand_dims(x_user, 1), (1, 100, 1))
# sequence data
is_ordered_history = tf.one_hot(self.is_ordered_history, 2)
index_in_order_history = tf.one_hot(self.index_in_order_history, 20)
order_dow_history = tf.one_hot(self.order_dow_history, 8)
order_hour_history = tf.one_hot(self.order_hour_history, 25)
days_since_prior_order_history = tf.one_hot(self.days_since_prior_order_history, 31)
order_size_history = tf.one_hot(self.order_size_history, 60)
reorder_size_history = tf.one_hot(self.reorder_size_history, 50)
order_is_weekend_history = tf.one_hot(self.order_is_weekend_history, 2)
order_part_of_day_history = tf.one_hot(self.order_part_of_day_history, 3)
order_number_history = tf.one_hot(self.order_number_history, 101)
index_in_order_history_scalar = tf.expand_dims(tf.cast(self.index_in_order_history, tf.float32) / 20.0, 2)
order_dow_history_scalar = tf.expand_dims(tf.cast(self.order_dow_history, tf.float32) / 8.0, 2)
order_hour_history_scalar = tf.expand_dims(tf.cast(self.order_hour_history, tf.float32) / 25.0, 2)
days_since_prior_order_history_scalar = tf.expand_dims(tf.cast(self.days_since_prior_order_history, tf.float32) / 31.0, 2)
order_size_history_scalar = tf.expand_dims(tf.cast(self.order_size_history, tf.float32) / 60.0, 2)
reorder_size_history_scalar = tf.expand_dims(tf.cast(self.reorder_size_history, tf.float32) / 50.0, 2)
order_number_history_scalar = tf.expand_dims(tf.cast(self.order_number_history, tf.float32) / 100.0, 2)
x_history = tf.concat([
is_ordered_history,
index_in_order_history,
order_dow_history,
order_hour_history,
days_since_prior_order_history,
order_size_history,
reorder_size_history,
order_is_weekend_history,
order_part_of_day_history,
order_number_history,
index_in_order_history_scalar,
order_dow_history_scalar,
order_hour_history_scalar,
days_since_prior_order_history_scalar,
order_size_history_scalar,
reorder_size_history_scalar,
order_number_history_scalar,
], axis=2)
x = tf.concat([x_history, x_product, x_user], axis=2)
return x
def calculate_outputs(self, x):
# lstm
h = lstm_layer(x, self.history_length, self.lstm_size, scope='lstm-1')
# cnn
c = time_distributed_dense_layer(x, self.lstm_size, activation=tf.nn.relu, scope='dense-1')
for i in range(6):
c_i = temporal_convolution_layer(
inputs=c,
output_units=self.lstm_size,
convolution_width=2,
activation=tf.nn.relu,
causal=True,
dilation_rate=[2**i],
scope='cnn-exp-{}'.format(i)
)
c += c_i
h = tf.concat([h, c, x], axis=2)
self.h_final = time_distributed_dense_layer(h, 50, activation=tf.nn.relu, scope='dense-2')
y_hat = time_distributed_dense_layer(self.h_final, 1, activation=tf.nn.sigmoid, scope='dense-3')
y_hat = tf.squeeze(y_hat, 2)
final_temporal_idx = tf.stack([tf.range(tf.shape(self.history_length)[0]), self.history_length - 1], axis=1)
self.final_states = tf.gather_nd(self.h_final, final_temporal_idx)
self.final_predictions = tf.gather_nd(y_hat, final_temporal_idx)
self.prediction_tensors = {
'user_ids': self.user_id,
'product_ids': self.product_id,
'final_states': self.final_states,
'predictions': self.final_predictions
}
return y_hat
if __name__ == '__main__':
base_dir = './'
dr = DataReader(data_dir=os.path.join(base_dir, 'data'))
nn = rnn(
reader=dr,
log_dir=os.path.join(base_dir, 'logs'),
checkpoint_dir=os.path.join(base_dir, 'checkpoints'),
prediction_dir=os.path.join(base_dir, 'predictions'),
optimizer='adam',
learning_rate=.001,
lstm_size=300,
batch_size=128,
num_training_steps=200000,
early_stopping_steps=30000,
warm_start_init_step=0,
regularization_constant=0.0,
keep_prob=1.0,
enable_parameter_averaging=False,
num_restarts=2,
min_steps_to_checkpoint=5000,
log_interval=20,
num_validation_batches=4,
)
nn.fit()
nn.restore()
nn.predict()