-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnn_train.py
75 lines (62 loc) · 2.12 KB
/
rnn_train.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
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout, Activation
from keras.optimizers import Adam
import numpy as np
import pickle
def lstm_model(input_shape):
hidden_units = 512 # may increase/decrease depending on capacity needed
timesteps = 20
input_dim = input_shape
num_classes = 22 # num of classes for output
model = Sequential()
model.add(LSTM(hidden_units, input_shape=(timesteps, input_dim)))
model.add(Dense(256))
model.add(Dropout(0.1))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Dropout(0.1))
model.add(Activation('relu'))
model.add(Dense(64))
model.add(Dropout(0.1))
model.add(Activation('relu'))
#
# model.add(Dense(32))
# model.add(Dropout(0.25))
# model.add(Activation('relu'))
# output layer
# model.add(Dense(2))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
adam = Adam(lr=0.001)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
# model.compile(loss='mse', optimizer=adam, metrics=['accuracy'])
model.summary()
return model
def train(load_path, save_path, epochs):
print("loading samples")
pickle_in = open(load_path, "rb")
data = pickle.load(pickle_in)
print("samples loaded")
xt = data['bigdata1'] # input_data shape = (num_trials, timesteps, input_dim)
# yt = data['y_data'].reshape(24000,6) # out_data shape = (num_trials, num_classes)
print xt.shape[2]
yt = data['class']
from sklearn.preprocessing import OneHotEncoder, normalize
#xt = normalize(xt)
enc = OneHotEncoder()
yt = enc.fit_transform(yt).toarray()
print(yt[:10])
print(xt.shape, yt.shape)
batch_size = 64
epochs = epochs
model = lstm_model(xt.shape[2])
model.fit(xt, yt, epochs=epochs, batch_size=batch_size, shuffle=True)
model.save(save_path)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('data_path', type=str)
parser.add_argument('-e', type=int, default=50)
parser.add_argument('-s', type=str)
args = parser.parse_args()
train(args.data_path, args.s, args.e)