【发布时间】:2020-04-08 16:27:46
【问题描述】:
我创建了一个用于股票价格预测的 LSTM 模型。那是我的代码:
from tqdm import tqdm
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers.recurrent import LSTM
from keras.layers.core import Dense,Activation,Dropout,Flatten,Reshape
from sklearn.preprocessing import MinMaxScaler
import keras as kr
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
veri =pd.read_csv("eurusd.csv")
veri['trh'] = pd.to_datetime(veri.trh, format='%d.%m.%Y')
########################
del veri['puan']
del veri['yuzde']
del veri['sira']
del veri['trh']
df_train, df_test = train_test_split(veri, train_size=0.8, test_size=0.2, shuffle=False)
print("Train and Test size", len(df_train), len(df_test))
x = df_train.loc[:,:].values
scaler = MinMaxScaler(feature_range=(0,1))
x_train = scaler.fit_transform(x)
x_test = scaler.transform(df_test.loc[:,:])
TIME_STEPS=7
BATCH_SIZE=128
def build_timeseries(mat, y_col_index):
# y_col_index tahmin etmek istediğimiz değerin sütun numarası
# total number of time-series samples would be len(mat) - TIME_STEPS
dim_0 = mat.shape[0] - TIME_STEPS #1328-7 gibi bir şey
dim_1 = mat.shape[1]
x = np.zeros((dim_0, TIME_STEPS, dim_1))
y = np.zeros((dim_0,))
for i in tqdm(range(dim_0)):
x[i] = mat[i:TIME_STEPS + i]
y[i] = mat[TIME_STEPS + i, y_col_index]
print("length of time-series i/o", x.shape, y.shape)
return x, y
def trim_dataset(mat, batch_size):
"""
trims dataset to a size that's divisible by BATCH_SIZE
"""
no_of_rows_drop = mat.shape[0]%batch_size
if(no_of_rows_drop > 0):
return mat[:-no_of_rows_drop]
else:
return mat
x_t, y_t = build_timeseries(x_train, 0)
#x_t =3 boyutlu besleme verileri
#y_t =de sonuç satırının timestepsten sonraki kısmı(1. değişkeni aldık)
x_t = trim_dataset(x_t, BATCH_SIZE)#xtrain
y_t = trim_dataset(y_t, BATCH_SIZE)#ytrain(sonuc)
x_temp, y_temp = build_timeseries(x_test, 0)
x_val, x_test_t = np.split(trim_dataset(x_temp, BATCH_SIZE),2)
y_val, y_test_t = np.split(trim_dataset(y_temp, BATCH_SIZE),2)
model = Sequential()
model.add(LSTM(100, batch_input_shape=(BATCH_SIZE, TIME_STEPS, x_t.shape[2]), dropout=0.0, recurrent_dropout=0.0, stateful=True, kernel_initializer='random_uniform'))
model.add(Dropout(0.2))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer=kr.optimizers.rmsprop(0.01))
csv_logger = kr.callbacks.CSVLogger('sonuclar.log')
history = model.fit(x_t, #train girdiler
y_t, #train çıktılar
epochs=175,
verbose=2,
batch_size=BATCH_SIZE,
shuffle=False,
validation_data=((trim_dataset(x_val, BATCH_SIZE)),
(trim_dataset(y_val, BATCH_SIZE))),
callbacks=[csv_logger])
grafik1=model.predict(trim_dataset(x_test_t,BATCH_SIZE), batch_size=BATCH_SIZE)
#grafik1= grafik1[:,0] (gerekli değil python liste döndürüyor)
grafik2= y_test_t
plt.plot(grafik1,label='Yreel',color='blue')
plt.plot(grafik2,label='Ypred',color='red')
blue_patch = mpatches.Patch(color='blue', label='Yreel')
red_patch = mpatches.Patch(color='red', label='Ypred')
plt.legend(handles=[blue_patch,red_patch])
plt.show()
我可以用这种风格进行预测,但如果我想用新样本进行预测,例如:
grafik1=model.predict(x_test_t[4:5], batch_size=BATCH_SIZE)
我收到此错误:
2020-04-08 19:22:02.902570: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: Specified a list with shape [128,4] from a tensor with shape [1,4]
[[{{node lstm_1/TensorArrayUnstack/TensorListFromTensor}}]]
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/phylo/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1462, in predict
callbacks=callbacks)
File "/home/phylo/.local/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 324, in predict_loop
batch_outs = f(ins_batch)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py", line 3727, in __call__
outputs = self._graph_fn(*converted_inputs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1551, in __call__
return self._call_impl(args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1591, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1692, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 545, in call
ctx=ctx)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Specified a list with shape [128,4] from a tensor with shape [1,4]
[[node lstm_1/TensorArrayUnstack/TensorListFromTensor (defined at /home/phylo/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_9454]
Function call stack:
keras_scratch_graph
我这样做的原因是我想通过提供未来适合模型的数据来进行预测。例如,我将使用过去 7 天的数据预测明天。我怎样才能做到这一点? (例如,仅用于测试此系统。我随机选择 x_test_t[4:5])
【问题讨论】:
标签: python tensorflow keras deep-learning lstm