forked from FanGeGo/xxxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
138 lines (108 loc) · 3.69 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import time
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.models import model_from_json
import preprocess
# Global hyper-parameters
sequence_length = 19
epochs = 1
batch_size = 50
feature_dimension = 341
def save_model_weight_into_file(model, modelname="model.json", weight="model.h5"):
model_json = model.to_json()
with open(modelname, "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(weight)
print("Saved model to disk in {} and {}".format(modelname,weight))
def load_model_and_wieght_from_file(modelname="model.json", weight="model.h5"):
json_file = open(modelname, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(weight)
print("Loaded model from disk, you can do more analysis more")
pass
def build_model():
model = Sequential()
layers = {'input': feature_dimension, 'hidden1': 64, 'hidden2': 256, 'hidden3': 100, 'output': feature_dimension}
model.add(LSTM(
layers['hidden1'],
input_length=sequence_length,
input_dim=layers['input'],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
layers['hidden2'],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
layers['hidden3'],
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
layers['output'],
activation='softmax'))
#model.add(Activation("linear"))
start = time.time()
model.compile(loss="categorical_crossentropy", optimizer='rmsprop', metrics=['accuracy'])
#model.compile(loss="mse", optimizer="rmsprop")
print ("Compilation Time : ", time.time() - start)
return model
def run_network(model=None, data=None):
global_start_time = time.time()
if data is None:
print ('Loading data... ')
# train on first 700 samples and test on next 300 samples (has anomaly)
X_train, y_train = preprocess.preprocess()
else:
X_train, y_train = data
print ("X_train, y_train,shape")
print (X_train.shape)
print (y_train.shape)
print ('\nData Loaded. Compiling...\n')
if model is None:
model = build_model()
print("Training...")
model.fit(
X_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_split=0.05)
model.summary()
print("Done Training...")
#predicted = model.predict(X_test)
#print("Reshaping predicted")
#predicted = np.reshape(predicted, (predicted.size,))
"""
except KeyboardInterrupt:
print("prediction exception")
print 'Training duration (s) : ', time.time() - global_start_time
return model, y_test, 0
try:
plt.figure(1)
plt.subplot(311)
plt.title("Actual Test Signal w/Anomalies")
plt.plot(y_test[:len(y_test)], 'b')
plt.subplot(312)
plt.title("Predicted Signal")
plt.plot(predicted[:len(y_test)], 'g')
plt.subplot(313)
plt.title("Squared Error")
mse = ((y_test - predicted) ** 2)
plt.plot(mse, 'r')
plt.show()
except Exception as e:
print("plotting exception")
print str(e)
print 'Training duration (s) : ', time.time() - global_start_time
return model, y_test, predicted
"""
if __name__ == "__main__":
run_network()