-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
100 lines (89 loc) · 3.31 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
'''
GPU command:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python train_model.py
'''
from common import FEELING
from keras.callbacks import Callback
from keras.utils import np_utils
from keras.models import Model
from keras.optimizers import RMSprop
from keras import backend as K
from keras.layers import Input, Dense, Lambda, Dropout, Activation, LSTM, \
TimeDistributed, Convolution1D, MaxPooling1D
from sklearn.model_selection import train_test_split
import numpy as np
import _pickle
from optparse import OptionParser
from sys import stderr, argv
import os
SEED = 42
N_LAYERS = 3
FILTER_LENGTH = 5
CONV_FILTER_COUNT = 256
LSTM_COUNT = 256
BATCH_SIZE = 32
EPOCH_COUNT = 100
def train_model(data):
x = data['x']
y = data['y']
(x_train, x_val, y_train, y_val) = train_test_split(x, y, test_size=0.3,
random_state=SEED)
print ('Building model...')
n_features = x_train.shape[2]
input_shape = (None, n_features)
model_input = Input(input_shape, name='input')
layer = model_input
for i in range(N_LAYERS):
# convolutional layer names are used by extract_filters.py
layer = Convolution1D(
nb_filter=CONV_FILTER_COUNT,
filter_length=FILTER_LENGTH,
name='convolution_' + str(i + 1)
)(layer)
layer = Activation('relu')(layer)
layer = MaxPooling1D(2)(layer)
layer = Dropout(0.5)(layer)
layer = LSTM(LSTM_COUNT, return_sequences=True)(layer)
layer = Dropout(0.5)(layer)
layer = TimeDistributed(Dense(len(FEELING)))(layer)
layer = Activation('softmax', name='output_realtime')(layer)
time_distributed_merge_layer = Lambda(
function=lambda x: K.mean(x, axis=1),
output_shape=lambda shape: (shape[0],) + shape[2:],
name='output_merged'
)
model_output = time_distributed_merge_layer(layer)
model = Model(model_input, model_output)
opt = RMSprop(lr=0.00001)
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy']
)
print ('Training...')
model.fit(x_train, y_train, batch_size=BATCH_SIZE, nb_epoch=EPOCH_COUNT,
validation_data=(x_val, y_val), verbose=1)
return model
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-d', '--data_path', dest='data_path',
default=os.path.join(os.path.dirname(__file__),
'data/data.pkl'),
help='path to the data pickle', metavar='DATA_PATH')
parser.add_option('-m', '--model_path', dest='model_path',
default=os.path.join(os.path.dirname(__file__),
'models/model.yaml'),
help='path to the output model YAML file', metavar='MODEL_PATH')
parser.add_option('-w', '--weights_path', dest='weights_path',
default=os.path.join(os.path.dirname(__file__),
'models/weights.h5'),
help='path to the output model weights hdf5 file',
metavar='WEIGHTS_PATH')
options, args = parser.parse_args()
with open('data/data.pkl', 'rb') as f:
data = _pickle.load(f)
model = train_model(data)
#with open('models/model.yaml', 'wb',encoding="utf-8") as f:
with open('models/model.yaml', 'w', encoding="utf-8") as f:
f.write(model.to_yaml())
model.save_weights('models/weights.h5')