-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
executable file
·86 lines (75 loc) · 4.31 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
import argparse
import os
import pickle
import yaml
from keras.callbacks import TensorBoard
from models import tcn, lstm, lstm_tf, lstm_regularize_tf, lstm_regularize_bn_tf
from util import data_generator
def run_tcn(d, x_train, x_test, y_train, y_test):
model, param_str = tcn.dilated_tcn(output_slice_index='last', # try 'first'.
num_feat=1,
num_classes=y_train.shape[1],
nb_filters=64,
kernel_size=8,
dilatations=[1, 2, 4, 8],
nb_stacks=8,
max_len=x_train[0:1].shape[1],
activation='norm_relu',
use_skip_connections=False,
return_param_str=True)
print(f'x_train.shape = {x_train.shape}')
print(f'y_train.shape = {y_train.shape}')
print(f'x_test.shape = {x_test.shape}')
print(f'y_test.shape = {y_test.shape}')
model.summary()
tb_callback = TensorBoard(log_dir=d, histogram_freq=0, batch_size=32, write_graph=True, write_grads=False,
write_images=False, embeddings_freq=0, embeddings_layer_names=None,
embeddings_metadata=None,
embeddings_data=None)
model.fit(x_train, y_train, epochs=32, validation_data=(x_test, y_test), callbacks=[tb_callback])
def run_lstm(d, x_train, x_test, y_train, y_test):
model = lstm.lstm((11, args.window, 1))
model.summary()
tb_callback = TensorBoard(log_dir=d, histogram_freq=0, write_graph=True, write_grads=False,
write_images=False, embeddings_freq=0, embeddings_layer_names=None,
embeddings_metadata=None, embeddings_data=None)
model.fit(x_train, y_train, epochs=128, validation_data=(x_test, y_test),
batch_size=11, callbacks=[tb_callback], shuffle=False)
return model
with open('config.yaml') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
parser = argparse.ArgumentParser(description='data related parameters')
parser.add_argument('--reset_state_window', help='Reset state after this length stateful lstm', default=30)
parser.add_argument('--stride', type=int, default=3)
parser.add_argument('--predict_length', type=int, default=3)
parser.add_argument('--embed', type=str, required=True, choices=config['embed'])
args = parser.parse_args()
embed = args.embed
window = config['window']
if not os.path.isfile('x_train_%s.pickle' % embed):
x_train, x_test, y_train, y_test = data_generator.generate(window, future=True, train_percentage=0.6, stride=args.stride, embed=args.embed, predict_length=args.predict_length)
with open(r"x_train_%s.pickle" % embed, "wb") as output_file:
pickle.dump(x_train, output_file)
with open(r"y_train_%s.pickle" % embed, "wb") as output_file:
pickle.dump(y_train, output_file)
with open(r"x_test_%s.pickle" % embed, "wb") as output_file:
pickle.dump(x_test, output_file)
with open(r"y_test_%s.pickle" % embed, "wb") as output_file:
pickle.dump(y_test, output_file)
else:
with open(r"x_train_%s.pickle" % embed, "rb") as output_file:
x_train = pickle.load(output_file)
with open(r"y_train_%s.pickle" % embed, "rb") as output_file:
y_train = pickle.load(output_file)
with open(r"x_test_%s.pickle" % embed, "rb") as output_file:
x_test = pickle.load(output_file)
with open(r"y_test_%s.pickle" % embed, "rb") as output_file:
y_test = pickle.load(output_file)
# spacy
#lstm_regularize_tf.train(x_train, y_train, x_test, y_test, time_steps=window, layer_shape=[128, 32], learning_rate=0.001, epoch=4096, predict_length=args.predict_length)
lstm_regularize_bn_tf.train(x_train, y_train, x_test, y_test, time_steps=window, layer_shape=[128, 32], learning_rate=0.0000001, epoch=5000, predict_length=args.predict_length)
# fasttext
#lstm_tf.train(x_train, y_train, x_test, y_test, time_steps=window, layer_shape=[128, 32], learning_rate=0.005, epoch=512, predict_length=args.predict_length)