import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
data = []
data1 = []
data2 = []
target = []
kairi = []
jyousyou = []
maxlen = 60
day1 = []
day2 = []
day3 = []
owarine = []
test_kairi_data = []
test_day = []
test_kairi = []
test_day2 = []
test_high = []
test_low = []
path_kawase = "/content/USDJPY_Candlestick_5_m_BID_01.01.2016-31.12.2018.txt" #デューカスからのデータをテキストにしたもの
path_kairi = "/content/USDJPY_2016-2018_kairi.txt" #乖離率のデータ
path_jyosyo = "/content/USDJPY_2016-2018_jyosyo.txt" #上昇度のデータ
test_path = "/content/USDJPY_2019_kairi.txt" #乖離率のデータ
test_price_path = "/content/USDJPY_Candlestick_5_m_BID_01.01.2019-31.12.2019.txt" #テスト用価格データのテキスト
def normalization(a):
b = []
for i in a:
b.append((i-min(a))/(max(a)-min(a)))
return b
with open(test_price_path,"r",encoding="utf-8") as f: #テスト用価格データ
next(f)
for line in f:
test_data = line.split(',')
test_day2.append(str(test_data[0]))
test_high.append(float(test_data[2]))
test_low.append(float(test_data[3]))
with open(test_path,"r",encoding="utf-8") as f: #テスト用乖離率のデータ
next(f)
for line in f:
test_data2 = line.split(',')
test_day.append(str(test_data2[0]))
test_kairi.append(float(test_data2[1]))
for i in range(0, len(test_kairi)-maxlen):
test_kairi_data.append(normalization(test_kairi[i: i + maxlen]))
Z = np.array(test_kairi_data).reshape(len(test_kairi_data), maxlen, 1)
for line in open (path_kawase,"r",encoding="utf-8"): #デューカスコピーでダウンロードしたデータ
data1 = line.split(',')
day1.append(str(data1[0]))
owarine.append (float(data1[4]))
with open(path_kairi,"r",encoding="utf-8") as f: #乖離率のデータ
next(f)
for line in f:
data2 = line.split(',')
day2.append(str(data2[0]))
kairi.append(float(data2[1]))
with open(path_jyosyo,"r",encoding="utf-8") as f: #上昇度のデータ
next(f)
for line in f:
data3 = line.split(',')
day3.append(str(data3[0]))
jyousyou.append(str(data3[1]))
'''
日付合わせ
'''
for i in range(len(owarine)-1,0,-1):
if(day1[i] == day3[-1]):
del day1[i+1:len(day1)]
del owarine[i+1:len(owarine)]
break
for i in range(len(kairi)-1,0,-1):
if(day2[i] == day3[-1]):
del day2[i+1:len(day2)]
del kairi[i+1:len(kairi)]
break
if(len(day2) > len(day3)):
for i in range(0,len(owarine)):
if(day1[i] == day3[0]):
del day1[0:i]
del owarine[0:i]
break
for i in range(0,len(kairi)-1):
if(day2[i] == day3[0]):
del day2[0:i]
del kairi[0:i]
break
else:
for i in range(0,len(owarine)):
if(day1[i] == day2[0]):
del day1[0:i]
del owarine[0:i]
break
for i in range(0,len(jyousyou)):
if(day3[i] == day2[0]):
del day3[0:i]
del jyousyou[0:i]
break`
for i in range(0, len(kairi)-maxlen):
data.append(normalization(kairi[i: i + maxlen]))
target.append(jyousyou[i + maxlen])
X = np.array(data).reshape(len(data), maxlen, 1)
Y = np.array(target).reshape(len(data), 1)
N_train = int(len(data) * 0.9)
N_validation = len(data) - N_train
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=N_validation,shuffle=False)
for i in range(len(test_kairi)-1,0,-1):
if(test_day[i] == test_day[-1]):
del test_day[i+1:len(test_day)]
del test_kairi[i+1:len(test_kairi)]
break
if(len(test_day) > len(test_day)):
for i in range(0,len(kairi)-1):
if(test_day[i] == test_day[0]):
del test_day[0:i]
del test_kairi[0:i]
break
else:
for i in range(0,len(test_high)):
if(day3[i] == test_day[0]):
del test_day[0:i]
del test_high[0:i]
del test_low[0:i]・
break・
・
・
・
'''
モデル設定
'''
n_in = len(X[0][0])
n_hidden = 100
n_out = len(Y[0])
def weight_variable(shape, name=None):
return np.random.normal(scale=.01, size=shape)
model = Sequential()
model.add(LSTM(n_hidden,
kernel_initializer="random_uniform",
input_shape=(maxlen, n_in)))
model.add(Dense(n_hidden, kernel_initializer="random_uniform"))
model.add(Activation('sigmoid'))
model.add(Dense(n_out, kernel_initializer="random_uniform"))
model.add(Activation('sigmoid'))
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
model.compile(loss='mean_squared_error',
optimizer=optimizer)
'''
モデル学習
'''
epochs = 500
batch_size = 1000
model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=epochs,
validation_split = 0.25)
'''
学習はここまで
'''
model.save("LSTM_model.h5") #モデルの保存
print("保存完了")
UnimplementedError Traceback (most recent call last)
<ipython-input-19-41cd48e6f4fe> in <module>()
173 batch_size=batch_size,
174 epochs=epochs,
--> 175 validation_split = 0.25)
UnimplementedError Traceback (most recent call last)
in ()
173 batch_size=batch_size,
174 epochs=epochs,
--> 175 validation_split = 0.25)