繁体   English   中英

为什么我无法用我的 Keras LSTM model 进行预测?

[英]Why I can't predict with my Keras LSTM model as I want?

我创建了一个 LSTM model 用于股票价格预测。 那是我的代码:

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

我这样做的原因是我想通过提供适合未来 model 的数据来进行预测。 例如,我将使用过去 7 天的数据预测明天。 我怎样才能做到这一点? (这个例子只是为了测试这个系统。我随机选择 x_test_t[4:5])

这看起来过于复杂。 请参阅下面的代码。 它对我来说非常好。

from pandas_datareader import data as wb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler

start = '2019-06-30'
end = '2020-06-30'

tickers = ['GOOG']

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])

#names = np.reshape(price_data, (len(price_data), 1))

df = pd.concat(price_data)
df.reset_index(inplace=True)

for col in df.columns: 
    print(col) 
    
#used for setting the output figure size
rcParams['figure.figsize'] = 20,10
#to normalize the given input data
scaler = MinMaxScaler(feature_range=(0, 1))
#to read input data set (place the file name inside  ' ') as shown below


df['Adj Close'].plot()
plt.legend(loc=2)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

在此处输入图像描述

ntrain = 80
df_train = df.head(int(len(df)*(ntrain/100)))
ntest = -80
df_test = df.tail(int(len(df)*(ntest/100)))


#importing the packages 
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM

#dataframe creation
seriesdata = df.sort_index(ascending=True, axis=0)
new_seriesdata = pd.DataFrame(index=range(0,len(df)),columns=['Date','Adj Close'])
length_of_data=len(seriesdata)
for i in range(0,length_of_data):
    new_seriesdata['Date'][i] = seriesdata['Date'][i]
    new_seriesdata['Adj Close'][i] = seriesdata['Adj Close'][i]
#setting the index again
new_seriesdata.index = new_seriesdata.Date
new_seriesdata.drop('Date', axis=1, inplace=True)
#creating train and test sets this comprises the entire data’s present in the dataset
myseriesdataset = new_seriesdata.values
totrain = myseriesdataset[0:255,:]
tovalid = myseriesdataset[255:,:]
#converting dataset into x_train and y_train
scalerdata = MinMaxScaler(feature_range=(0, 1))
scale_data = scalerdata.fit_transform(myseriesdataset)
x_totrain, y_totrain = [], []
length_of_totrain=len(totrain)
for i in range(60,length_of_totrain):
    x_totrain.append(scale_data[i-60:i,0])
    y_totrain.append(scale_data[i,0])
x_totrain, y_totrain = np.array(x_totrain), np.array(y_totrain)
x_totrain = np.reshape(x_totrain, (x_totrain.shape[0],x_totrain.shape[1],1))



#LSTM neural network
lstm_model = Sequential()
lstm_model.add(LSTM(units=50, return_sequences=True, input_shape=(x_totrain.shape[1],1)))
lstm_model.add(LSTM(units=50))
lstm_model.add(Dense(1))
lstm_model.compile(loss='mean_squared_error', optimizer='adadelta')
lstm_model.fit(x_totrain, y_totrain, epochs=10, batch_size=1, verbose=2)
#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values
myinputs = myinputs.reshape(-1,1)
myinputs  = scalerdata.transform(myinputs)
tostore_test_result = []
for i in range(60,myinputs.shape[0]):
    tostore_test_result.append(myinputs[i-60:i,0])
tostore_test_result = np.array(tostore_test_result)
tostore_test_result = np.reshape(tostore_test_result,(tostore_test_result.shape[0],tostore_test_result.shape[1],1))
myclosing_priceresult = lstm_model.predict(tostore_test_result)
myclosing_priceresult = scalerdata.inverse_transform(myclosing_priceresult)
    
totrain = df_train
tovalid = df_test

#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values


#  Printing the next day’s predicted stock price. 
print(myclosing_priceresult);

结果:

[[1396.532]]

https://github.com/ASH-WICUS/Notebooks/blob/master/Long%20Short%20Term%20Memory%20-%20Stock%20Price%20Prediction.ipynb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM