-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_and_eval.py
173 lines (128 loc) · 5.18 KB
/
train_and_eval.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import torch
from torch.utils.data import TensorDataset
from lstm.modeling_lstm import LSTM
from utils import load_model
def create_window(input, seq_length):
data_raw = input.values
data = []
# create all possible sequences of window size
for index in range(len(data_raw) - seq_length):
data.append(data_raw[index: index + seq_length])
data = np.array(data)
x_data = data[:, :-1, :]
y_data = data[:, -1, :]
return x_data, y_data
def run_train(input_data, to_model, hps):
# data load
train_data = pd.read_csv(input_data)
# pre-processing
train_data = train_data.set_index('date')
# train_data_index = train_data.index
# column_name = list(train_data)
# scaler = MinMaxScaler()
# train_data = scaler.fit_transform(train_data)
# train_data = pd.DataFrame(train_data, columns=column_name, index=train_data_index)
# scaler = MinMaxScaler(feature_range=(-1, 1))
scaler = MinMaxScaler()
if hps.target == 'close':
train_data = train_data[['close']]
train_data['close'] = scaler.fit_transform(train_data['close'].values.reshape(-1, 1))
if hps.target == 'open':
train_data = train_data[['open']]
train_data['open'] = scaler.fit_transform(train_data['open'].values.reshape(-1, 1))
if hps.target == 'volume':
train_data = train_data[['volume']]
train_data['volume'] = scaler.fit_transform(train_data['volume'].values.reshape(-1, 1))
# train_data = pd.DataFrame(train_data, columns=['close'], index=train_data_index)
# creating window for lstm
if hps.model_type == 'lstm':
window_size = hps.window_size
X_train, y_train = create_window(train_data, window_size+1)
# print(X_train)
# for s in range(1, window_size+1):
# train_data['close_{}'.format(s)] = train_data['close'].shift(s)
#
# X_train = train_data.dropna().drop('close', axis=1)
# y_train = train_data.dropna()[['close']]
#
# X_train = X_train.values
#
# y_train = y_train.values
#
# X_train = X_train.reshape(X_train.shape[0], window_size, 1)
print("------------------ LSTM Data ------------------")
print("Num Examples : {}".format(X_train.shape[0]))
print("X Train Window Size : {}".format(X_train.shape[1]))
print("-----------------------------------------------")
model = LSTM(input_dim=1, hidden_size=32, output_size=1, num_layer=2, hps=hps)
# not implement for another model
else:
pass
# numpy to tensor
X_train = torch.from_numpy(X_train).type(torch.Tensor)
y_train = torch.from_numpy(y_train).type(torch.Tensor)
# For Testing
# print(X_train.shape) # (data_num, window_size(sequence_length), 1)
# print(y_train.shape) # (data_num, 1)
if hps.model_type == "lstm":
from lstm.train import train
else:
pass
# Train DataSet
train_dataset = TensorDataset(X_train, y_train)
train(train_dataset, model, hps, to_model)
def run_eval(input_data, from_model, batch_size):
model = load_model(from_model)
hps = model.hps
# data load
test_data = pd.read_csv(input_data)
# pre-processing
test_data = test_data.set_index('date')
# scaler = MinMaxScaler(feature_range=(-1, 1))
scaler = MinMaxScaler()
if hps.target == 'close':
test_data = test_data[['close']]
test_data['close'] = scaler.fit_transform(test_data['close'].values.reshape(-1, 1))
if hps.target == 'open':
test_data = test_data[['open']]
test_data['open'] = scaler.fit_transform(test_data['open'].values.reshape(-1, 1))
if hps.target == 'volume':
test_data = test_data[['volume']]
test_data['volume'] = scaler.fit_transform(test_data['volume'].values.reshape(-1, 1))
# creating window for lstm
if hps.model_type == 'lstm':
window_size = hps.window_size
X_test, y_test = create_window(test_data, window_size+1)
print("------------------ LSTM Data ------------------")
print("Num Examples : {}".format(X_test.shape[0]))
print("X Test Window Size : {}".format(X_test.shape[1]))
print("-----------------------------------------------")
# not implement for another model
else:
pass
# numpy to tensor
X_test = torch.from_numpy(X_test).type(torch.Tensor)
y_test = torch.from_numpy(y_test).type(torch.Tensor)
if hps.model_type == "lstm":
from lstm.eval import eval
else:
pass
# Test DataSet
test_dataset = TensorDataset(X_test, y_test)
y_preds = eval(test_dataset, model, batch_size)
y_test = y_test.detach().numpy()
y_preds = scaler.inverse_transform(y_preds)
y_test = scaler.inverse_transform(y_test)
# result_to_dict = {'label' : [y[0] for y in y_test], 'pred' : [y[0] for y in y_preds]}
# predict_results = pd.DataFrame(result_to_dict)
# print(predict_results.head())
plt.plot(y_test, label="Original")
plt.plot(y_preds, label="Predict")
plt.legend()
plt.show()
# print(y_test)
return None