-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesian LSTM_GPU2.py
421 lines (331 loc) · 15.9 KB
/
Bayesian LSTM_GPU2.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
#energy_df = pd.read_csv('C:\\Users\\NIU004\\OneDrive - CSIRO\\Desktop\\energydata_complete.csv')
df = pd.read_csv(r'C:\Users\NIU004\OneDrive - CSIRO\Desktop\Data_3.csv')
df['data_value1'] = (df['data_value1'] -df['data_value1'].mean())/(df['data_value1'].std())
df = df[['data_value1']]
# df['data_value1'] = np.log(df['data_value1'] + 0.1)
df['index'] = range(1, len(df) + 1)
df = df[0:10000]
#df = df[['data_value1','data_value2']]
# result = adfuller(df.values)
# print('1. ADF:',result[0])
# print('2. p-value:',result[1])
# print('3. Num of Lags:',result[2])
# print('4. Num of Observations Used For ADF Regression and Critical Values Calculation:', result[3])
# print('5. Critical Values:')
# for key,val in result[4].items():
# print('\t',key,':',val)
import numpy as np
import plotly.express as px
from sklearn.preprocessing import MinMaxScaler
def create_sliding_window(data, sequence_length, stride=1):
X_list, y_list = [], []
for i in range(len(data)):
if (i + sequence_length) < len(data):
X_list.append(data.iloc[i:i+sequence_length:stride, :].values)
y_list.append(data.iloc[i+sequence_length, 0])
return np.array(X_list), np.array(y_list)
train_split = 0.8
n_train = int(train_split * len(df))
n_test = int(len(df))- n_train
feature_array = df.values
# feature_scaler = MinMaxScaler()
# feature_scaler.fit(feature_array[:n_train])
# target_scaler = MinMaxScaler()
# target_scaler.fit(feature_array[:n_train, 0].reshape(-1, 1))
# Transfom on both Training and Test data
scaled_array = pd.DataFrame(feature_array)
sequence_length = 10
X, y = create_sliding_window(scaled_array,
sequence_length)
X_train = X[:n_train]
y_train = y[:n_train]
X_test = X[n_train:]
y_test = y[n_train:]
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
device = torch.device("cuda:0")
class BayesianLSTM(nn.Module):
def __init__(self, n_features, output_length, batch_size):
super(BayesianLSTM, self).__init__()
self.batch_size = batch_size # user-defined
self.hidden_size_1 = 128 #128 # number of encoder cells (from paper)
self.hidden_size_2 = 32#32 # number of decoder cells (from paper)
self.stacked_layers = 2 # number of (stacked) LSTM layers for each stage
self.dropout_probability = 0.5 # arbitrary value (the paper suggests that performance is generally stable across all ranges)
self.lstm1 = nn.LSTM(n_features,
self.hidden_size_1,
num_layers=self.stacked_layers,
batch_first=True)
self.lstm2 = nn.LSTM(self.hidden_size_1,
self.hidden_size_2,
num_layers=self.stacked_layers,
batch_first=True)
self.fc = nn.Linear(self.hidden_size_2, output_length)
self.loss_fn = nn.MSELoss()
def forward(self, x):
batch_size, seq_len, _ = x.size()
hidden = self.init_hidden1(batch_size)
output, _ = self.lstm1(x, hidden)
output = F.dropout(output, p=self.dropout_probability, training=True)
state = self.init_hidden2(batch_size)
output, state = self.lstm2(output, state)
output = F.dropout(output, p=self.dropout_probability, training=True)
output = output[:, -1, :] # take the last decoder cell's outputs
y_pred = self.fc(output)
return y_pred
def init_hidden1(self, batch_size):
hidden_state = Variable(torch.zeros(self.stacked_layers, batch_size, self.hidden_size_1)).to(device)
cell_state = Variable(torch.zeros(self.stacked_layers, batch_size, self.hidden_size_1)).to(device)
return hidden_state, cell_state
def init_hidden2(self, batch_size):
hidden_state = Variable(torch.zeros(self.stacked_layers, batch_size, self.hidden_size_2)).to(device)
cell_state = Variable(torch.zeros(self.stacked_layers, batch_size, self.hidden_size_2)).to(device)
return hidden_state, cell_state
def loss(self, pred, truth):
return self.loss_fn(pred, truth)
def predict(self, X):
return self(torch.tensor(X, dtype=torch.float32)).view(-1).detach().numpy()
n_features = scaled_array.shape[-1]
output_length = 1
batch_size = 32
n_epochs =100
learning_rate = 0.0001
bayesian_lstm = BayesianLSTM(n_features=n_features,
output_length=output_length,
batch_size = batch_size)
bayesian_lstm = bayesian_lstm.to(device)
criterion = torch.nn.MSELoss()
criterion = criterion.to(device)
optimizer = torch.optim.Adam(bayesian_lstm.parameters(), lr=learning_rate)
# optimizer = torch.optim.RMSprop(bayesian_lstm.parameters(), lr=learning_rate)
# scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=10, gamma=0.9)
loss_train=[]
loss_test=[]
for e in range(1, n_epochs+1):
# for e in range(1, 2):
bayesian_lstm.train()
for b in range(0, len(X_train), batch_size):
features = X_train[b:b+batch_size,:,:]
target = y_train[b:b+batch_size]
X_batch = torch.tensor(features,dtype=torch.float32)
y_batch = torch.tensor(target,dtype=torch.float32)
#X_batch,y_batch = Variable(features),Variable(target)
X_batch = X_batch.to(device)
y_batch = y_batch.to(device)
output = bayesian_lstm(X_batch)
loss = criterion(output, y_batch)
loss = loss.to(device)
loss.backward()
optimizer.step()
optimizer.zero_grad()
#print(optimizer.param_groups[0]["lr"])
#scheduler.step()
#print('epoch', e, 'loss: ', loss.item())
loss_train.append(loss.item())
bayesian_lstm.eval()
with torch.no_grad():
for b in range(0, len(X_test), batch_size):
features = X_train[b:b+batch_size,:,:]
target = y_train[b:b+batch_size]
X_batch = torch.tensor(features,dtype=torch.float32)
y_batch = torch.tensor(target,dtype=torch.float32)
#X_batch,y_batch = Variable(features),Variable(target)
X_batch = X_batch.to(device)
y_batch = y_batch.to(device)
output = bayesian_lstm(X_batch)
loss = criterion(output, y_batch)
loss = loss.to(device)
loss_test.append(loss.item())
if e % 10 == 0:
print('epoch', e, 'loss: ', loss.item())
torch.save(bayesian_lstm.state_dict(), 'C:\\Users\\NIU004\\OneDrive - CSIRO\\Desktop\\Mineral sorting\\Kansanshi\\Bayesian_LSTM.pt')
#################load model###########################
bayesian_lstm.load_state_dict(torch.load('C:\\Users\\NIU004\\OneDrive - CSIRO\\Desktop\\Mineral sorting\\Kansanshi\\Bayesian_LSTM.pt'))
bayesian_lstm.eval()
offset = sequence_length
X_train = torch.tensor(X_train,dtype=torch.float32)
X_train = X_train.to(device)
training_df = pd.DataFrame()
training_df['index'] = df['index'].iloc[offset:n_train + offset:1]
X_train = (torch.tensor(X_train, dtype=torch.float32)).detach().to(device)
training_predictions = bayesian_lstm(X_train)
training_df['data_value1'] = training_predictions.detach().cpu().numpy()
training_df['source'] = 'Training Prediction'
training_truth_df = pd.DataFrame()
training_truth_df['index'] = training_df['index']
training_truth_df['data_value1'] = df['data_value1'].iloc[offset:n_train + offset:1]
training_truth_df['source'] = 'True Values'
testing_df = pd.DataFrame()
testing_df['index'] = df['index'].iloc[n_train + offset::1]
X_test = (torch.tensor(X_test, dtype=torch.float32)).detach().to(device)
testing_predictions = bayesian_lstm(X_test)
testing_df['data_value1'] = testing_predictions.detach().cpu().numpy()
testing_df['source'] = 'Test Prediction'
testing_truth_df = pd.DataFrame()
testing_truth_df['index'] = testing_df['index']
testing_truth_df['data_value1'] = df['data_value1'].iloc[n_train + offset::1]
testing_truth_df['source'] = 'True Values'
import plotly.io as pio
# evaluation = pd.concat([training_df,
# testing_df,
# training_truth_df,
# testing_truth_df
# ], axis=0)
# evaluation['MR grade'] = evaluation['all grade over 2000tonnage']
# evaluation['Time Step'] = evaluation['index']
# fig = px.line(evaluation.loc[evaluation['index'].between(7000, 8000)],
# x="Time Step",
# y="MR grade",
# color="source",
# title="MR grade measured every 4seconds vs Time Step")
# #fig.show()
plt.plot(loss_train,color='r')
plt.plot(loss_test,color='b')
n_experiments = 300
pio.renderers.default='browser'
test_uncertainty_df = pd.DataFrame()
test_uncertainty_df['index'] = testing_df['index']
for i in range(n_experiments):
X_test = (torch.tensor(X_test, dtype=torch.float32)).detach().to(device)
experiment_predictions = bayesian_lstm(X_test)
test_uncertainty_df['grade_{}'.format(i)] = experiment_predictions.detach().cpu().numpy()
grade_df = test_uncertainty_df.filter(like='grade', axis=1)
test_uncertainty_df['grade_mean'] = grade_df.mean(axis=1)
test_uncertainty_df['grade_std'] = grade_df.std(axis=1)
test_uncertainty_df = test_uncertainty_df[['index', 'grade_mean', 'grade_std']]
test_uncertainty_df['lower_bound'] = test_uncertainty_df['grade_mean'] - 3*test_uncertainty_df['grade_std']
test_uncertainty_df['upper_bound'] = test_uncertainty_df['grade_mean'] + 3*test_uncertainty_df['grade_std']
import plotly.graph_objects as go
test_uncertainty_plot_df = test_uncertainty_df.copy(deep=True)
test_uncertainty_plot_df = test_uncertainty_plot_df.loc[test_uncertainty_plot_df['index'].between(len(df)*0.8, len(df)*0.8+2000)]
truth_uncertainty_plot_df = testing_truth_df.copy(deep=True)
truth_uncertainty_plot_df = truth_uncertainty_plot_df.loc[testing_truth_df['index'].between(len(df)*0.8, len(df)*0.8+2000)]
upper_trace = go.Scatter(
x=test_uncertainty_plot_df['index'],
y=test_uncertainty_plot_df['upper_bound'],
mode='lines',
fill=None,
name='99% Upper Confidence Bound'
)
lower_trace = go.Scatter(
x=test_uncertainty_plot_df['index'],
y=test_uncertainty_plot_df['lower_bound'],
mode='lines',
fill='tonexty',
fillcolor='rgba(255, 211, 0, 0.1)',
name='99% Lower Confidence Bound'
)
real_trace = go.Scatter(
x=truth_uncertainty_plot_df['index'],
y=truth_uncertainty_plot_df['data_value1'],
mode='lines',
fill=None,
name='Real Values'
)
predicted_mean_trace_test = go.Scatter(
x=test_uncertainty_plot_df['index'],
y=test_uncertainty_plot_df['grade_mean'],
mode='lines',
fill=None,
name='Predicted Mean Values'
)
data = [upper_trace, lower_trace, real_trace,predicted_mean_trace_test]
fig = go.Figure(data=data)
fig.update_layout(title='Uncertainty Quantification',
xaxis_title='Time',
yaxis_title='value')
fig.show()
###############validation##################
# df_new = pd.read_csv(r'C:\Users\NIU004\OneDrive - CSIRO\Desktop\Mineral sorting\Kansanshi\Trial Data - Copy\grade_over_2000tonnage_each_row\PLC_CMP_Data_For11D10M2021Y.csv')
# df_new = df_new.groupby(np.arange(len(df_new))//1).mean()
# # # # # plt.plot(df['all grade over 2000tonnage'])
# # df['moving average 5'] = ' '
# # for i in range(5,50000,1):
# # df['moving average 5'][i] = df['all grade over 2000tonnage'][i-5:i].mean()
# # df1=df[15000:20000]
# # df1 = df1.drop(['all grade over 2000tonnage'],axis=1)
# # df1['all grade over 2000tonnage'] = df1['moving average 5']
# # df1 = df1.drop(['moving average 5'],axis=1)
# df_new['index'] = df_new.index
# feature_array1 = df_new.values
# feature_scaler1 = MinMaxScaler()
# feature_scaler1.fit(feature_array1)
# target_scaler1 = MinMaxScaler()
# target_scaler1.fit(feature_array1[:, 0].reshape(-1, 1))
# # # Transfom on both Training and Test data
# scaled_array1 = pd.DataFrame(feature_scaler1.transform(feature_array1))
# X1, y1 = create_sliding_window(scaled_array1,
# sequence_length)
# X_validation = X1
# y_validation = y1
# X_validation = torch.tensor(X_validation,dtype=torch.float32)
# X_validation = X_validation.to(device)
# validation_df = pd.DataFrame()
# validation_df['index'] = df_new['index'].iloc[0 + offset::1]
# X_validation = (torch.tensor(X_validation, dtype=torch.float32)).detach().to(device)
# validation_predictions = bayesian_lstm(X_validation)
# validation_df['all grade over 2000tonnage'] = inverse_transform(validation_predictions.detach().cpu().numpy())
# validation_df['source'] = 'Test Prediction'
# n_experiments = 200
# validation_uncertainty_df = pd.DataFrame()
# validation_uncertainty_df['index'] = validation_df['index']
# for i in range(n_experiments):
# X_validation = (torch.tensor(X_validation, dtype=torch.float32)).detach().to(device)
# experiment_predictions1 = bayesian_lstm(X_validation)
# validation_uncertainty_df['grade_{}'.format(i)] = inverse_transform(experiment_predictions1.detach().cpu().numpy())
# grade_df = validation_uncertainty_df.filter(like='grade', axis=1)
# validation_uncertainty_df['grade_mean'] = grade_df.mean(axis=1)
# validation_uncertainty_df['grade_std'] = grade_df.std(axis=1)
# validation_uncertainty_df = validation_uncertainty_df[['index', 'grade_mean', 'grade_std']]
# validation_uncertainty_df['lower_bound'] = validation_uncertainty_df['grade_mean'] - 1*validation_uncertainty_df['grade_std']
# validation_uncertainty_df['upper_bound'] = validation_uncertainty_df['grade_mean'] + 1*validation_uncertainty_df['grade_std']
# import plotly.graph_objects as go
# validation_truth_df = pd.DataFrame()
# validation_truth_df['index'] = validation_df['index']
# validation_truth_df['all grade over 2000tonnage'] = df_new['grade over 2000tonnage '].iloc[0 + offset::1]
# validation_truth_df['source'] = 'True Values'
# validation_uncertainty_plot_df = validation_uncertainty_df.copy(deep=True)
# validation_uncertainty_plot_df = validation_uncertainty_plot_df.loc[validation_uncertainty_plot_df['index'].between(0, 5000)]
# validation_truth_uncertainty_plot_df = validation_truth_df.copy(deep=True)
# validation_truth_uncertainty_plot_df = validation_truth_uncertainty_plot_df.loc[validation_truth_df['index'].between(0, 5000)]
# upper_trace = go.Scatter(
# x=validation_uncertainty_plot_df['index'],
# y=validation_uncertainty_plot_df['upper_bound'],
# mode='lines',
# fill=None,
# name='99% Upper Confidence Bound'
# )
# lower_trace = go.Scatter(
# x=validation_uncertainty_plot_df['index'],
# y=validation_uncertainty_plot_df['lower_bound'],
# mode='lines',
# fill='tonexty',
# fillcolor='rgba(255, 211, 0, 0.1)',
# name='99% Lower Confidence Bound'
# )
# real_trace = go.Scatter(
# x=validation_truth_uncertainty_plot_df['index'],
# y=validation_truth_uncertainty_plot_df['all grade over 2000tonnage'],
# mode='lines',
# fill=None,
# name='Real Values'
# )
# predicted_mean_trace_validation = go.Scatter(
# x=validation_uncertainty_plot_df['index'],
# y=validation_uncertainty_plot_df['grade_mean'],
# mode='lines',
# fill=None,
# name='Real Values'
# )
# data = [upper_trace, lower_trace, real_trace, predicted_mean_trace_validation]
# fig = go.Figure(data=data)
# fig.update_layout(title='Uncertainty Quantification for MR grade test data measured every 4seconds vs Time Step',
# xaxis_title='Time',
# yaxis_title='Copper Grade (wt%)')
# fig.show()