-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
72 lines (61 loc) · 2.14 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
import torch
import numpy as np
import matplotlib.pyplot as plt
from DBN import DBN
from sklearn.preprocessing import *
from sklearn.metrics import mean_squared_error
# Set parameter
# data
input_length = 50
output_length = 1
test_percentage = 0.2
# network
hidden_units = [128, 64]
device = 'cuda'
if device == 'cuda':
assert torch.cuda.is_available() is True, "cuda isn't available"
print('Using GPU backend.\n'
'GPU type: {}'.format(torch.cuda.get_device_name(0)))
else:
print('Using CPU backend.')
# train & predict
batch_size = 128
epoch_pretrain = 100
epoch_finetune = 200
loss_function = torch.nn.MSELoss()
optimizer = torch.optim.Adam
# Generate input and output data
dataset = 2 * np.sin([i / 2000 * 50 * np.pi for i in range(2000)]) + 5
scaler = StandardScaler()
dataset_norm = scaler.fit_transform(dataset.reshape(-1, 1)).flatten()
dataset_list = []
for i in range(len(dataset) - input_length - output_length):
dataset_list.append(dataset_norm[i:i + input_length + output_length])
dataset_list = np.array(dataset_list)
trainset = dataset_list[:int(len(dataset_list) * (1 - test_percentage))]
testset = dataset_list[int(len(dataset_list) * (1 - test_percentage)):]
x_train = trainset[:, :-1]
y_train = trainset[:, -1:]
x_test = testset[:, :-1]
y_test = testset[:, -1:]
print('x_train.shape:' + str(x_train.shape))
print('y_train.shape:' + str(y_train.shape))
print('x_test.shape:' + str(x_test.shape))
print('y_test.shape' + str(y_test.shape))
# Build model
dbn = DBN(hidden_units, input_length, output_length, device=device)
# Train model
dbn.pretrain(x_train, epoch=epoch_pretrain, batch_size=batch_size)
dbn.finetune(x_train, y_train, epoch_finetune, batch_size, loss_function,
optimizer(dbn.parameters()))
# Make prediction and plot
y_predict = dbn.predict(x_test, batch_size)
y_real = scaler.inverse_transform(y_test.reshape(-1, 1)).flatten()
y_predict = scaler.inverse_transform(y_predict.reshape(-1, 1)).flatten()
plt.figure(1)
plt.plot(y_real, label='real')
plt.plot(y_predict, label='prediction')
plt.xlabel('MSE Error: {}'.format(mean_squared_error(y_real, y_predict)))
plt.legend()
plt.title('Prediction result')
plt.show()