-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNeuralNetwork.py
More file actions
102 lines (72 loc) · 2.86 KB
/
NeuralNetwork.py
File metadata and controls
102 lines (72 loc) · 2.86 KB
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
import numpy as np
import os
import tensorflow as tf
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.callbacks import LambdaCallback
from keras.models import model_from_json
import csv
class NeuralNetwork():
def __init__(self):
pass
def train(self, X, y, epochs, verbose=1):
self.model.fit(X, y, epochs=epochs, verbose=verbose)
# def train_debug(self, X, y, epochs, verbose=1):
# print_weights = LambdaCallback(
# on_epoch_end=lambda batch, logs: print(self.model.layers[0].get_weights()))
# self.model.fit(X, y, epochs=epochs, verbose=verbose,
# callbacks=[print_weights])
def test(self, X, y, verbose=1):
for i in range(len(X)):
input = X[i].reshape((1,self.steps, self.input_shape[1]))
yhat = self.model.predict(input, verbose=verbose)
print(y[i], yhat[0][0])
def save(self):
# create new directory if not already there
path = "./Models/" + self.filename + "/"
file = path + self.filename
try:
os.system("mkdir " + path)
except:
print("hey")
pass
# serialize model to JSON
model_json = self.model.to_json()
with open(file + ".json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
self.model.save_weights(file + ".h5")
# saving normalization values to csv
with open(file + '.csv', "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(self.max_vals)
writer.writerow(self.min_vals)
print("Saved model to disk.")
@staticmethod
def load_network(filename):
# path directory variables
path = "./Models/" + filename + "/"
file = path + filename
# load json and create model
json_file = open(file + '.json', '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(file + ".h5")
# print("Loaded model from disk.")
return loaded_model
@staticmethod
def normalization_values(filename):
# path directory variables
path = "./Models/" + filename + "/"
file = path + filename
# values used to normalize training data
max_vals = np.array([])
min_vals = np.array([])
with open(file + '.csv', "r") as f:
f_data = list(csv.reader(f))
max_vals = np.array([float(f.strip()) for f in f_data[0]])
min_vals = np.array([float(f.strip()) for f in f_data[1]])
return max_vals, min_vals