-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
136 lines (110 loc) · 3.86 KB
/
main.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
#Import Libraries
import math
import numpy as np
from numpy import array, hstack
import pandas
from scipy.io import loadmat
import tensorflow as tf
from keras.models import Sequential, Model
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
# Loads matlab files data into a python dict
mat_data = loadmat('Data/TRAIN_LGHG2@n10degC_to_25degC_Norm_5Inputs.mat')
#Test data set
mat_data_2 = loadmat('Data/04_TEST_LGHG2@25degC_Norm_(05_Inputs).mat')
# split a multivariate sequence into samples
def split_sequences(sequences, n_steps):
X, Y = list(), list()
for i in range(len(sequences)):
# find the end of this pattern
current_end = i + n_steps
# check if we are beyond the dataset
if current_end > len(sequences):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequences[i:current_end, :-1], sequences[current_end-1, -1]
X.append(seq_x)
Y.append(seq_y)
return array(X), array(Y)
## Define NN Network Architecture
# Using variables copied from MATLAB file
num_responses = 1
num_features = 3
num_hidden_units = 10
epochs = 1
batch_size = 100
learn_rate_drop_period = 2000
LearningRate = 0.01
learn_rate_drop_factor = 0.5
timesteps = 10
n_features = 3
## prepare train data
# slices matlab data into each element
V = array(mat_data['X'][0,:])
I = array(mat_data['X'][1,:])
T = array(mat_data['X'][2,:])
SOC = array(mat_data['Y'][0,:])
# prepare individial elements for merge
V = V.reshape(len(V),1)
I = I.reshape(len(I),1)
T = T.reshape(len(T),1)
SOC = SOC.reshape(len(SOC),1)
# merge elements into one dataset
train_data = hstack((V,I,T,SOC))
## prepare test data
# slices matlab data into each element
V = array(mat_data_2['X'][0,:])
I = array(mat_data_2['X'][1,:])
T = array(mat_data_2['X'][2,:])
SOC = array(mat_data_2['Y'][0,:])
# prepare individial elements for merge
V = V.reshape(len(V),1)
I = I.reshape(len(I),1)
T = T.reshape(len(T),1)
SOC = SOC.reshape(len(SOC),1)
# merge elements into one dataset
test_data = hstack((V,I,T,SOC))
x_train, y_train = split_sequences(train_data, timesteps)
print(x_train.shape, y_train.shape)
x_test, y_test = split_sequences(test_data, timesteps)
print(x_test.shape, y_test.shape)
## Build the LSTM model
# Defined the model using same design as the Abstract
model = Sequential()
#model.add(LSTM(10, batch_input_shape=(0,x_train.shape[1],n_features), stateful=True,return_sequences=False))
model.add(LSTM(10, input_shape=(timesteps, n_features)))
model.add(Dense(1))
model.add(tf.keras.layers.ReLU(max_value=1))
model.summary()
#Define the learning rate scheduler
def scheduler(epoch, lr):
if epoch % learn_rate_drop_period == 0 and epoch:
return lr * learn_rate_drop_factor
else:
return lr
#Define the learning rate scheduler callback
lr_scheduler = tf.keras.callbacks.LearningRateScheduler(scheduler)
#Compile the model with a learning rate scheduler
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LearningRate), loss='mean_squared_error')
#Train the model
model.fit(x_train,y_train, batch_size=batch_size, epochs=epochs, callbacks=[lr_scheduler],validation_data=(x_test, y_test),)
## Predicts SOC data using the trained LSTM model
# make predictions Stateful = True
trainPredict = model.predict(x_train, batch_size=batch_size)
testPredict = model.predict(x_test, batch_size=batch_size)
#Get the root mean squared error (RMSE)
#rmse_train=np.sqrt(np.mean(((trainPredict[:40000] - y_train[:40000])**2)))*100
#print("training data rmse", rmse_train)
#Get the root mean squared error (RMSE)
mse_test = np.mean(((testPredict - y_test)**2))
rmse_test = math.sqrt(mse_test)
print("test data rmse", rmse_test)
# Plot the predictions
plt.plot(testPredict, label="Predictions")
# Plot the true values
plt.plot(y_test, label="Objective")
# Add a legend
plt.legend()
# Show the plot
plt.show()