-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep_cooking.py
More file actions
378 lines (243 loc) · 10.1 KB
/
deep_cooking.py
File metadata and controls
378 lines (243 loc) · 10.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
###########################################################
# See https://www.kaggle.com/c/whats-cooking/forums/t/16657/deep-cooking/93391
# for further information.
###########################################################
# This script contains some elements from Kappa's script and valerio orfano's script
# https://www.kaggle.com/c/whats-cooking/forums/t/16538/simple-theano-script-with-0-79033-on-leaderboard
import gc
import os
import pickle
import json
import numpy as np
import pandas as pd
from collections import OrderedDict
from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import LabelEncoder
from itertools import chain
import theano
import theano.tensor as T
import lasagne as nn
###########################################################
# auxiliary functions for nn
def sgd(loss, all_parameters, learning_rate):
all_grads = [theano.grad(loss, param) for param in all_parameters]
updates = []
for param_i, grad_i in zip(all_parameters, all_grads):
v = - learning_rate * grad_i
# clip from 0.0 to 1.0
updates.append((param_i, T.clip(param_i + v, 0.0, 1.0)))
return updates
def get_param_values(params):
return [p.get_value() for p in params]
def set_param_values(params, param_values):
for p, pv in zip(params, param_values):
p.set_value(pv)
def normalize_input(X):
return (X.T / np.sum(X, axis=1)).T
###########################################################
# load and preprocess data
print('loading data ...')
input_dir = '../input/'
# train
with open(os.path.join(input_dir, 'train.json')) as train_f:
train_data = json.loads(train_f.read())
X_train = [x['ingredients'] for x in train_data]
X_train = [dict(zip(x,np.ones(len(x)))) for x in X_train]
vec = DictVectorizer()
X_train = vec.fit_transform(X_train).toarray()
X_train = normalize_input(X_train)
X_train = X_train.astype(np.float32)
feature_names = np.array(vec.feature_names_)
lbl = LabelEncoder()
y_train = [y['cuisine'] for y in train_data]
y_train = lbl.fit_transform(y_train).astype(np.int32)
label_names = lbl.classes_
for i, l in enumerate(label_names):
print('i: {}, l: {}'.format(i, l))
## sample here for memory restrictions
#idx = np.random.choice(X_train.shape[0], 4096)
#X_train = X_train[idx]
#y_train = y_train[idx]
# for memory restrictions use only british or chinese
idx = np.logical_or(y_train == 1, y_train ==3 )
y_train = y_train[idx]
X_train = X_train[idx]
print('num_saples: {}'.format( y_train.shape[0] ))
###########################################################
# train nn for weights, this code is too bad
def get_nn_params(X_train, y_train):
LEARNING_RATE = 0.01
OUTPUT_DIM = label_names.size # = 20
BATCH_SIZE = 64
NUM_EPOCHS = 50
# pad samples
n_train = X_train.shape[0]
# theano based neural network
# (1) network
X_batch = T.matrix('x')
y_batch = T.ivector('y')
activation = nn.nonlinearities.rectify
l_in = nn.layers.InputLayer(input_var=X_batch, shape=(BATCH_SIZE, X_train.shape[1]),)
l_hidden0_dropout = nn.layers.DropoutLayer(l_in, p=0.0)
l_hidden1 = nn.layers.DenseLayer( l_hidden0_dropout, num_units=256,
nonlinearity=activation, W=nn.init.GlorotUniform(),)
l_hidden1_dropout = nn.layers.DropoutLayer(l_hidden1, p=0.5)
#l_hidden2 = nn.layers.DenseLayer( l_hidden1_dropout, num_units=128,
# nonlinearity=activation, W=nn.init.GlorotUniform(),)
#l_hidden2_dropout = nn.layers.DropoutLayer(l_hidden2, p=0.5)
# classifier
l_out = nn.layers.DenseLayer( l_hidden1_dropout, num_units=OUTPUT_DIM,
nonlinearity=nn.nonlinearities.softmax, W=nn.init.GlorotUniform(),)
# (2) i/o
X_shared = theano.shared(np.zeros((1, 1,), dtype=theano.config.floatX))
y_shared = theano.shared(np.zeros((1,), dtype=theano.config.floatX))
y_shared_casted = T.cast(y_shared, 'int32')
batch_index = T.lscalar('batch_index')
# (3) loss, outputs, updates
learning_rate = theano.shared(np.array(LEARNING_RATE, dtype=theano.config.floatX))
all_params = nn.layers.get_all_params(l_out)
loss_train = T.mean(-T.log( nn.layers.get_output(l_out) )[T.arange(y_batch.shape[0]), y_batch])
loss_eval = T.mean(-T.log( nn.layers.get_output(l_out, deterministic=True) )[T.arange(y_batch.shape[0]), y_batch])
pred = T.argmax( nn.layers.get_output(l_out, deterministic=True), axis=1)
pred_proba = nn.layers.get_output(l_out, deterministic=True)
updates = nn.updates.nesterov_momentum( loss_train, all_params, learning_rate,)
givens = {
X_batch: X_shared[batch_index*BATCH_SIZE:(batch_index+1)*BATCH_SIZE],
y_batch: y_shared_casted[batch_index*BATCH_SIZE:(batch_index+1)*BATCH_SIZE],
}
train = theano.function( [batch_index], [loss_train], updates=updates, givens=givens)
test = theano.function( [batch_index], [loss_eval, pred_proba], givens=givens)
# train
print('start training')
for e in range(NUM_EPOCHS):
# shuffle and pad train sample
idx = np.arange(y_train.size)
np.random.shuffle(idx)
idx = idx[:(idx.shape[0] / BATCH_SIZE * BATCH_SIZE)]
X_shared.set_value(X_train[idx].astype(np.float32))
y_shared.set_value(y_train[idx].astype(np.float32))
train_losses = []
for b in range(int(idx.shape[0] / BATCH_SIZE)):
(train_loss,) = train(b)
train_losses.append(train_loss)
(_, p) = test(b)
mean_train_loss = np.mean(train_losses)
print(' epoch: {}, loss: {}'.format(e, mean_train_loss))
return get_param_values(all_params)
print('get nn params ...')
nn_params = get_nn_params(X_train, y_train)
###########################################################
# NN for output
LEARNING_RATE = 0.01
OUTPUT_DIM = label_names.size # = 20
BATCH_SIZE = 1 #256
# theano based neural network
# (2) i/o
X_shared = theano.shared(np.zeros((1, 1,), dtype=theano.config.floatX))
y_shared = theano.shared(np.zeros((1, 1,), dtype=theano.config.floatX))
#y_shared = theano.shared(np.zeros((1,), dtype=theano.config.floatX))
#y_shared_casted = T.cast(y_shared, 'int32')
batch_index = T.lscalar('batch_index')
activation = nn.nonlinearities.rectify
l_in = nn.layers.InputLayer(input_var=X_shared, shape=(BATCH_SIZE, X_train.shape[1]),)
l_hidden0_dropout = nn.layers.DropoutLayer(l_in, p=0.0)
l_hidden1 = nn.layers.DenseLayer( l_hidden0_dropout, num_units=256,
nonlinearity=activation, W=nn.init.GlorotUniform(),)
l_hidden1_dropout = nn.layers.DropoutLayer(l_hidden1, p=0.5)
#l_hidden2 = nn.layers.DenseLayer( l_hidden1_dropout, num_units=128,
# nonlinearity=activation, W=nn.init.GlorotUniform(),)
#l_hidden2_dropout = nn.layers.DropoutLayer(l_hidden2, p=0.5)
# classifier
l_out = nn.layers.DenseLayer( l_hidden1_dropout, num_units=OUTPUT_DIM,
nonlinearity=nn.nonlinearities.softmax, W=nn.init.GlorotUniform(),)
# (3) loss, outputs, updates
learning_rate = theano.shared(np.array(LEARNING_RATE, dtype=theano.config.floatX))
all_params = nn.layers.get_all_params(l_out)
# load weights
#nn_params = pickle.load(open('nn_params.pkl'))
set_param_values(all_params, nn_params)
#loss_train = T.mean(-T.log( nn.layers.get_output(l_out) )[T.arange(y_shared_casted.shape[0]), y_shared_casted])
#loss_eval = T.mean(-T.log( nn.layers.get_output(l_out, deterministic=True) )[T.arange(y_shared_casted.shape[0]), y_shared_casted])
loss_train = T.mean( ( nn.layers.get_output(l_out) - y_shared) ** 2.0 )
#loss_train = T.mean( ( nn.layers.get_output(l_out) - y_shared) ** 2.0 ) \
# + 0.0003 * T.mean(T.sum(abs(X_shared), axis=1))
#updates = nn.updates.nesterov_momentum( loss_train, all_params, learning_rate,)
#updates = nn.updates.sgd( loss_train, [X_shared], learning_rate,)
updates = sgd( loss_train, [X_shared], learning_rate,) # sgd with clip
train = theano.function([], [loss_train], updates=updates,)
###########################################################
# html for output
o = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!---
<h3>Title</h3>
<p>Description</p>
--->
'''
table_head = '''
<h4>{}</h4>
<ul>
'''
table_row = '''
<li>{} <small>{}</small></li>
'''
table_foot = '''
</ul>
'''
def write(o, X, title=''):
o += table_head.format(title)
X = np.mean(X, axis=0)
idx = np.argsort(-X)[:10]
for i in idx:
o += table_row.format(feature_names[i], X[i])
o += table_foot
return o
###########################################################
# main
## (1.1) "mean" of british
#X_chinese = X_train[y_train == 1]
#o = write(o, X_chinese, '"Mean" of british cuisine')
# (1.2) nn
X_noise = np.random.uniform(low=0.0, high=1.0,
size=(BATCH_SIZE, X_train.shape[1])) * 0.001
y_noise = np.zeros([BATCH_SIZE, OUTPUT_DIM])
y_noise[:,1] = 1.0
X_shared.set_value(X_noise.astype(np.float32))
y_shared.set_value(y_noise.astype(np.float32))
print('start training')
for i in range(30):
(l,) = train()
print('epoch: {}, loss: {}'.format(i, l))
X_result = X_shared.get_value()
o = write(o, X_result, 'British cuisine by NN model')
## (2.1) "mean" of chinese
#X_chinese = X_train[y_train == 3]
#o = write(o, X_chinese, '"Mean" of chinese cuisine')
# (2.2) nn
X_noise = np.random.uniform(low=0.0, high=1.0,
size=(BATCH_SIZE, X_train.shape[1])) * 0.001
y_noise = np.zeros([BATCH_SIZE, OUTPUT_DIM])
y_noise[:,3] = 1.0
X_shared.set_value(X_noise.astype(np.float32))
y_shared.set_value(y_noise.astype(np.float32))
print('start training')
for i in range(30):
(l,) = train()
print('epoch: {}, loss: {}'.format(i, l))
X_result = X_shared.get_value()
o = write(o, X_result, 'Chinese cuisine by NN model')
###########################################################
# output
o += '''
</html>
'''
with open("results.html","wb") as outfile:
outfile.write(o.encode("utf-8"))