-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_att_seq.py
87 lines (74 loc) · 3.25 KB
/
train_att_seq.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
# training stacked lstm
import ConfigParser
# config
config = ConfigParser.ConfigParser()
config.read('config.ini')
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import csv
import tensorflow as tf
from keras import backend as K
config_tf = tf.ConfigProto()
config_tf.gpu_options.visible_device_list = config.get('att_swipe', 'gpu')
sess = tf.Session(config=config_tf)
K.set_session(sess)
from keras.models import Sequential
from seq2seq import SimpleSeq2Seq, Seq2Seq, AttentionSeq2Seq
from keras.callbacks import ModelCheckpoint
from keras.layers.core import *
from keras.layers.wrappers import *
from keras.models import Model
from keras.layers import Input, LSTM, Dense
from keras.optimizers import *
from keras.preprocessing.sequence import *
from utility.indb_prepare_prepare import *
max_length = max_fea_seq_length(X_train)
print('max length is {:}'.format(max_length))
X_train = pad_sequences(X_train, maxlen=max_length, dtype=np.float64)
X_val = pad_sequences(X_val, maxlen=max_length, dtype=np.float64)
# config
input_dim = X_train[0].shape[1]
output_dim = int(config.get('att_swipe', 'output_dim'))
hidden_dim = int(config.get('att_swipe', 'hidden_dim'))
output_length = int(config.get('att_swipe', 'output_length'))
epoch_num = int(config.get('att_swipe', 'epoch_num'))
batch_size = int(config.get('att_swipe', 'batch_size'))
learning_rate = float(config.get('att_swipe', 'learning_rate'))
depth = int(config.get('att_swipe', 'depth'))
drop_out_ratio = float(config.get('att_swipe', 'drop_out_ratio'))
# model name
model_name = 'attention' + '_' + str(hidden_dim) + '_' + str(learning_rate) + '_' + str(epoch_num) \
+ '_' + str(X_train.shape[0]) + '_' + str(drop_out_ratio) + '_' + str(depth) + '_' + str(freq) + 'hz'
print('model name is {}'.format(model_name))
model_weights_name = "model_" + model_name + ".hdf5"
model_weights_path = './tmp/models/' + model_weights_name
directory = './tmp/models/'
if not os.path.exists(directory):
os.makedirs(directory)
checkpoint = ModelCheckpoint(model_weights_path, monitor='val_acc', verbose=1,
save_best_only=True, save_weights_only=True, mode='max')
callbacks_list = [checkpoint]
model = Sequential()
model.add(AttentionSeq2Seq(output_dim=output_dim, hidden_dim=hidden_dim,
output_length=output_length, input_shape=(X_train[0].shape[0], X_train[0].shape[1]),
dropout=drop_out_ratio, depth=depth))
model.add(TimeDistributed(Dense(output_dim)))
model.add(Activation('softmax'))
model.summary()
optimizer = Adam()
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
hist = model.fit(X_train, y_train_flat, epochs=epoch_num, batch_size=batch_size,
validation_data=(X_val, y_val_flat), shuffle=True,
verbose=1, callbacks=callbacks_list)
# write out as files
directory = './history/'
if not os.path.exists(directory):
os.makedirs(directory)
csvName = './history/hist_' + model_name + '.csv'
with open(csvName, 'ab') as fp:
a = csv.writer(fp)
for key, val in hist.history.items():
a.writerow([key, val])
# move final models from ./tmp/models to ./models
model_weights_path_new = './models/' + model_weights_name
shutil.move(model_weights_path, model_weights_path_new)