-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_model.py
176 lines (144 loc) · 6.7 KB
/
train_model.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pickle
import time
import numpy as np
import pandas as pd
from keras import metrics, callbacks
from keras.layers import Input, LSTM, Dense, concatenate, Bidirectional, Conv1D, GlobalMaxPooling1D, \
GlobalAveragePooling1D, Dropout
from keras.models import Model
from keras.utils import np_utils
def pre_df(_d):
"""
pre train d, transform to array, (49, 205)
:param _d:
:return: (uid_id, lstm x, lstm y, valid y, aux data(time information which are not used in this task))
"""
for uid_index, v in _d.items():
v = np.array(v)
# split the data to X and Y
pre_X, y = v[:-2, 44:], category_dict[v[-2, 0]]
# flatten a
p = pre_X.flatten()
# get nodata count, ready to padding
padding_weight = (49 * 200 - p.shape[0])
# padding and reshape to 49 * 200
X = np.pad(p, (0, padding_weight), 'constant', constant_values=-1.).reshape(49, 200)
return uid_index, X, y
def lstm_cnn(model_main_data, model_label):
"""
build model
:return:
"""
print("Building lstm cnn model *** ")
main_input = Input(shape=(49, 200), dtype="float32", name="main_input")
# define lstm output size == 256
lstm_out = Bidirectional(LSTM(256, return_sequences=True))(main_input)
lstm_out = Dropout(0.5)(lstm_out)
conv1d_2 = Conv1D(64, kernel_size=2, padding="valid", kernel_initializer="he_uniform")(lstm_out)
conv1d_3 = Conv1D(64, kernel_size=3, padding="valid", kernel_initializer="he_uniform")(lstm_out)
conv1d_5 = Conv1D(64, kernel_size=5, padding="valid", kernel_initializer="he_uniform")(lstm_out)
avg_pool_2 = GlobalAveragePooling1D()(conv1d_2)
max_pool_2 = GlobalMaxPooling1D()(conv1d_2)
avg_pool_3 = GlobalAveragePooling1D()(conv1d_3)
max_pool_3 = GlobalMaxPooling1D()(conv1d_3)
avg_pool_5 = GlobalAveragePooling1D()(conv1d_5)
max_pool_5 = GlobalMaxPooling1D()(conv1d_5)
conc = concatenate([avg_pool_2, max_pool_2, avg_pool_3, max_pool_3, avg_pool_5, max_pool_5])
conc = Dropout(0.5)(conc)
main_output = Dense(category_len, activation='softmax', name='aux_output')(conc)
model = Model(inputs=main_input, outputs=main_output)
model.compile(optimizer="rmsprop", loss="categorical_crossentropy",
metrics=[metrics.top_k_categorical_accuracy, 'accuracy'])
# start to train
print("start *** ")
start_time = time.time()
tensorboard = callbacks.TensorBoard(log_dir=f'logs_{category_len}')
callback_lists = [tensorboard]
history = model.fit(model_main_data, model_label, epochs=40, batch_size=128,
validation_split=0.1, callbacks=callback_lists)
print(max(history.history['val_acc']))
print(max(history.history['val_top_k_categorical_accuracy']))
print("save model")
spend_time = time.time() - start_time
print(spend_time)
model.save("./data/lstm_cnn.h5")
def only_cnn(model_main_data, model_label):
print("Building only cnn model *** ")
main_input = Input(shape=(49, 200), dtype="float32", name="main_input")
# define lstm output size == 256
conv1d_2 = Conv1D(64, kernel_size=2, padding="valid", kernel_initializer="he_uniform")(main_input)
conv1d_3 = Conv1D(64, kernel_size=3, padding="valid", kernel_initializer="he_uniform")(main_input)
conv1d_5 = Conv1D(64, kernel_size=5, padding="valid", kernel_initializer="he_uniform")(main_input)
avg_pool_2 = GlobalAveragePooling1D()(conv1d_2)
max_pool_2 = GlobalMaxPooling1D()(conv1d_2)
avg_pool_3 = GlobalAveragePooling1D()(conv1d_3)
max_pool_3 = GlobalMaxPooling1D()(conv1d_3)
avg_pool_5 = GlobalAveragePooling1D()(conv1d_5)
max_pool_5 = GlobalMaxPooling1D()(conv1d_5)
conc = concatenate([avg_pool_2, max_pool_2, avg_pool_3, max_pool_3, avg_pool_5, max_pool_5])
main_output = Dense(category_len, activation='softmax', name='aux_output')(conc)
model = Model(inputs=main_input, outputs=main_output)
model.compile(optimizer="rmsprop", loss="categorical_crossentropy",
metrics=[metrics.top_k_categorical_accuracy, 'accuracy'])
# start to train
print("start *** ")
start_time = time.time()
tensorboard = callbacks.TensorBoard(log_dir=f'logs_{category_len}')
callback_lists = [tensorboard]
history = model.fit(model_main_data, model_label, epochs=40, batch_size=128,
validation_split=0.1, callbacks=callback_lists)
print(max(history.history['val_acc']))
print(max(history.history['val_top_k_categorical_accuracy']))
print("save model")
spend_time = time.time() - start_time
print(spend_time)
model.save("./data/only_cnn.h5")
def only_lstm(model_main_data, model_label):
print("Building only lstm model *** ")
main_input = Input(shape=(49, 200), dtype="float32", name="main_input")
lstm_out = LSTM(256)(main_input)
main_output = Dense(category_len, activation='softmax', name='aux_output')(lstm_out)
model = Model(inputs=main_input, outputs=main_output)
model.compile(optimizer="rmsprop", loss="categorical_crossentropy",
metrics=[metrics.top_k_categorical_accuracy, 'accuracy'])
# start to train
print("start *** ")
start_time = time.time()
tensorboard = callbacks.TensorBoard(log_dir=f'logs_{category_len}')
callback_lists = [tensorboard]
history = model.fit(model_main_data, model_label, epochs=60, batch_size=128,
validation_split=0.1, callbacks=callback_lists)
print(max(history.history['val_acc']))
print(max(history.history['val_top_k_categorical_accuracy']))
print("save model")
spend_time = time.time() - start_time
print(spend_time)
model.save("./data/only_lstm.h5")
if __name__ == '__main__':
# parse = argparse.ArgumentParser()
# parse.add_argument('--model', type=str, default='both', help="train or predict (default True)")
# get nid embedding
nid_dict = {}
category_dict = {}
cls = 1000
with open('./data/emb_cntr.txt', 'r') as f:
for i in f.readlines()[1:]:
nid_dict[i.split(" ")[0]] = list(map(float, i.split()[1:]))
for index, k in enumerate(nid_dict.keys()):
category_dict[k] = index
# load data.pkl
data = pickle.load(open('./data/data.pkl', 'rb'))
main_data = []
label = []
# create train data
for index, d in enumerate(data):
k, X, y = pre_df(d)
main_data.append(X)
label.append(y)
category_len = max(label) + 1
y_label = np_utils.to_categorical(np.array(label))
# lstm_cnn(model_main_data=np.array(main_data), model_label=y_label)
# only_lstm(model_main_data=np.array(main_data), model_label=y_label)
only_cnn(model_main_data=np.array(main_data), model_label=y_label)