-
Notifications
You must be signed in to change notification settings - Fork 0
/
architecture_4.py
133 lines (115 loc) · 4.88 KB
/
architecture_4.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
import numpy as np
import pandas as pd
from keras.models import Model
from keras.layers import Input, Dense, concatenate
import keras.backend as K
# train data generator
def train_data_generator(batch_size=100):
reader = pd.read_csv("train.txt", delimiter="#", low_memory=False, chunksize=batch_size, dtype=np.str)
while True:
try:
chunk = next(reader)
except StopIteration:
reader = pd.read_csv("train.txt", delimiter="#", low_memory=False, chunksize=batch_size, dtype=np.str)
chunk = next(reader)
chunk = chunk.to_numpy(dtype=np.str)
chunk_x_1 = []
chunk_x_2 = []
chunk_x_3 = []
chunk_y = []
for row in chunk:
chunk_x_1.append([float(item) for item in row[0].split(',')])
chunk_x_2.append([float(item) for item in row[1].split(',')])
chunk_x_3_temp_1 = row[2].split(',')
chunk_x_3_temp_2 = row[4].split(',')
chunk_x_3.append([float(chunk_x_3_temp_1[0]), float(chunk_x_3_temp_1[1]), float(chunk_x_3_temp_1[2]), float(chunk_x_3_temp_1[3]), float(chunk_x_3_temp_1[4]), float(chunk_x_3_temp_2[0]), float(chunk_x_3_temp_2[1])])
chunk_y.append(float(row[5]))
yield [np.array(chunk_x_1), np.array(chunk_x_2), np.array(chunk_x_3)], np.array(chunk_y)
# test data generator
def test_data_generator(batch_size=100):
reader = pd.read_csv("test.txt", delimiter="#", low_memory=False, chunksize=batch_size, dtype=np.str)
while True:
try:
chunk = next(reader)
except StopIteration:
reader = pd.read_csv("test.txt", delimiter="#", low_memory=False, chunksize=batch_size, dtype=np.str)
chunk = next(reader)
chunk = chunk.to_numpy(dtype=np.str)
chunk_x_1 = []
chunk_x_2 = []
chunk_x_3 = []
chunk_y = []
for row in chunk:
chunk_x_1.append([float(item) for item in row[0].split(',')])
chunk_x_2.append([float(item) for item in row[1].split(',')])
chunk_x_3_temp_1 = row[2].split(',')
chunk_x_3_temp_2 = row[4].split(',')
chunk_x_3.append([float(chunk_x_3_temp_1[0]), float(chunk_x_3_temp_1[1]), float(chunk_x_3_temp_1[2]), float(chunk_x_3_temp_1[3]), float(chunk_x_3_temp_1[4]), float(chunk_x_3_temp_2[0]), float(chunk_x_3_temp_2[1])])
chunk_y.append(float(row[5]))
yield [np.array(chunk_x_1), np.array(chunk_x_2), np.array(chunk_x_3)], np.array(chunk_y)
# rmse
def rmse(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true)))
# get data
batch_size = 32
user_input_dim = 4567
recipe_features_input_dim = 4567
other_features_input_dim = 7
num_total_samples = 2975564
num_training_samples = int(num_total_samples * 0.8)
num_testing_samples = num_total_samples - num_training_samples
train_data_generator_iterator = train_data_generator(batch_size)
test_data_generator_iterator = test_data_generator(batch_size)
test_data_generator_iterator2 = test_data_generator(batch_size)
# create model
# user
user_model_input = Input(shape=(user_input_dim,))
user_model = Dense(units=1024, activation='relu')(user_model_input)
user_model = Dense(units=512, activation='relu')(user_model)
user_model = Dense(units=256, activation='relu')(user_model)
user_model = Dense(units=128, activation='relu')(user_model)
# recipe
recipe_model_input = Input(shape=(recipe_features_input_dim,))
recipe_model = Dense(units=1024, activation='relu')(recipe_model_input)
recipe_model = Dense(units=512, activation='relu')(recipe_model)
recipe_model = Dense(units=256, activation='relu')(recipe_model)
recipe_model = Dense(units=128, activation='relu')(recipe_model)
other_features_input = Input(shape=(other_features_input_dim,))
# main model
main_model = concatenate([user_model, recipe_model, other_features_input])
main_model = Dense(units=256, activation='relu')(main_model)
main_model = Dense(units=128, activation='relu')(main_model)
main_model = Dense(units=64, activation='relu')(main_model)
main_model = Dense(units=1, activation='linear')(main_model)
model = Model(inputs=[user_model_input, recipe_model_input, other_features_input], outputs=main_model)
model.summary()
# compile
model.compile(
optimizer='adam',
loss=rmse,
metrics=['mse', 'mae', 'mape', rmse]
)
# fit
model.fit_generator(
generator=train_data_generator_iterator,
steps_per_epoch=(num_training_samples // batch_size) + 1,
epochs=3,
verbose=1,
)
# save
model.save_weights('architecture_4.h5')
# evaluate
metrics = model.evaluate_generator(
generator=test_data_generator_iterator,
steps=(num_testing_samples // batch_size)+1,
verbose=1,
)
print('metrics:', metrics)
# predict
prediction = model.predict_generator(
generator=test_data_generator_iterator2,
steps=(num_testing_samples // batch_size) + 1,
verbose=1,
)
print('prediction:', prediction)
pd.DataFrame(prediction).to_csv("architecture_4.csv", index=False)