-
Notifications
You must be signed in to change notification settings - Fork 14
/
cnn_model.py
104 lines (87 loc) · 3.83 KB
/
cnn_model.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
import math
import matplotlib.pyplot as plt
import numpy as np
from keras.layers import Dense, MaxPooling1D, Flatten
from keras.layers.convolutional import Conv1D
from keras.models import Sequential
from keras.optimizers import *
from sklearn.metrics import mean_squared_error
def load_close(data):
f = open(data, 'r').readlines()[1:]
raw_data = []
raw_dates = []
for line in f:
try:
close_price = float(line.split(',')[4])
raw_data.append(close_price)
raw_dates.append(line.split(',')[0])
except:
continue
return raw_data, raw_dates
def load_returns(data):
f = open(data, 'r').readlines()[1:]
raw_data = []
raw_dates = []
for line in f:
try:
open_price = float(line.split(',')[1])
close_price = float(line.split(',')[4])
raw_data.append(close_price - open_price)
raw_dates.append(line.split(',')[0])
except:
continue
return raw_data[::-1], raw_dates[::-1]
def make_timeseries_regressor(window_size, filter_length, nb_input_series=1, nb_outputs=1, nb_filter=4):
model = Sequential((
Conv1D(input_shape=(window_size, nb_input_series),
kernel_size=filter_length, activation="relu", filters=nb_filter,
padding='causal'),
MaxPooling1D(),
Conv1D(kernel_size=filter_length, activation="relu", filters=nb_filter,
padding='causal'),
MaxPooling1D(),
Flatten(),
Dense(nb_outputs, activation='linear'),
))
opt = Adam(lr=0.001)
model.compile(loss='mse', optimizer=opt, metrics=['mae'])
return model
def make_timeseries_instances(timeseries, window_size):
timeseries = np.asarray(timeseries)
assert 0 < window_size < timeseries.shape[0]
X = np.atleast_3d(
np.array([timeseries[start:start + window_size] for start in range(0, timeseries.shape[0] - window_size)]))
y = timeseries[window_size:]
q = np.atleast_3d([timeseries[-window_size:]])
return X, y, q
def evaluate_timeseries(timeseries, window_size, epochs, batch_size):
filter_length = 5
nb_filter = 4
timeseries = np.atleast_2d(timeseries)
if timeseries.shape[0] == 1:
timeseries = timeseries.T
nb_samples, nb_series = timeseries.shape
# print('\n\nTimeseries ({} samples by {} series):\n'.format(nb_samples, nb_series), timeseries)
model = make_timeseries_regressor(window_size=window_size, filter_length=filter_length, nb_input_series=nb_series,
nb_outputs=nb_series, nb_filter=nb_filter)
print('\n\nModel with input size {}, output size {}, {} conv filters of length {}'.format(model.input_shape,
model.output_shape,
nb_filter, filter_length))
model.summary()
X, y, q = make_timeseries_instances(timeseries, window_size)
print('\n\nInput features:', X, '\n\nOutput labels:', y, '\n\nQuery vector:', q, sep='\n')
test_size = int(0.1 * nb_samples)
X_train, X_test, y_train, y_test = X[:-test_size], X[-test_size:], y[:-test_size], y[-test_size:]
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test, y_test))
pred = model.predict(X_test)
print('\n\nactual', 'predicted', sep='\t')
for actual, predicted in zip(y_test, pred.squeeze()):
print(actual.squeeze(), predicted, sep='\t')
print('next', model.predict(q).squeeze(), sep='\t')
"""Prepare input data, build model, evaluate."""
np.set_printoptions(threshold=25)
window_size = 50
epochs = 25
batch_size = 2
timeseries = load_close('stockdatas/VTI.csv')
evaluate_timeseries(timeseries[0], window_size, epochs, batch_size)