-
Notifications
You must be signed in to change notification settings - Fork 1
/
oracle-new.py
58 lines (41 loc) · 1.62 KB
/
oracle-new.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
import os
import sys
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
from keras import regularizers
from keras.models import Sequential
from keras.layers import BatchNormalization, Dense, CuDNNLSTM
from keras.preprocessing.sequence import TimeseriesGenerator
sys.stderr = stderr
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.logging.set_verbosity(tf.logging.ERROR)
np.random.seed(0)
df = pd.read_csv('../data/aapl.csv')[::-1]
x = StandardScaler().fit_transform(df[df.columns.difference(['Date', 'Correction'])].astype(float))
y = df['Correction'].values
batch_size = 353
timesteps = 14
test_n = 353
series = TimeseriesGenerator(x, y, timesteps, batch_size=len(x))[0]
series_x = series[0]
series_y = series[1]
train_x = series_x[:-test_n]
train_y = series_y[:-test_n]
test_x = series_x[-test_n:]
test_y = series_y[-test_n:]
model = Sequential()
model.add(CuDNNLSTM(10, batch_input_shape=(batch_size, series_x.shape[1], series_x.shape[2]), stateful=True))
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
for i in range(1, 1001):
model.fit(train_x, train_y, batch_size=batch_size, epochs=1, shuffle=False, verbose=0)
train_acc = model.evaluate(train_x, train_y, batch_size=batch_size, verbose=0)[1]
test_acc = model.evaluate(test_x, test_y, batch_size=batch_size, verbose=0)[1]
print(model.predict(test_x, batch_size=batch_size, verbose=0))
print(i, round(train_acc, 2), round(test_acc, 2))
model.reset_states()