Keras LSTM批量输入形状

时间:2017-11-08 18:31:22

标签: keras lstm

你好我似乎无法弄清楚在处理LSTM时,重新交换X,Y与Keras的批量输入形状之间的关系。

当前数据库是我带来的84119,190个pandas数据帧。从那里突破到X和Y.所以功能是189.如果你能指出我错在哪里,因为它与(序列,时间步,尺寸)我们将不胜感激。

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM    

# load dataset
training_data_df = pd.read_csv("C:/Users/####/python_folders/stock_folder/XYstore/Big_data22.csv")


X = training_data_df.drop('Change Month End Stock Price', axis=1).values
Y = training_data_df[['Change Month End Stock Price']].values


data_dim = 189
timesteps = 4
numberofSequence = 1

X=X.reshape(numberofSequence,timesteps,data_dim)
Y=Y.reshape(numberofSequence,timesteps, 1)
model = Sequential()
model.add(LSTM(200, return_sequences=True,batch_input_shape=(timesteps,data_dim)))
model.add(LSTM(200, return_sequences=True))  
model.add(LSTM(100,return_sequences=True))  
model.add(LSTM(1,return_sequences=False, activation='linear'))
model.compile(loss='mse',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(X,Y,epochs=100)

编辑以解决问题

感谢下面的帮助。两者都帮助我思考问题。还有一些工作要做才能真正理解它。

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM




training_data_df = pd.read_csv("C:/Users/TurnerJ/python_folders/stock_folder/XYstore/Big_data22.csv")

training_data_df.replace(np.nan,value=0,inplace=True)
training_data_df.replace(np.inf,value=0,inplace=True)
training_data_df = training_data_df.loc[279:,:]
X = training_data_df.drop('Change Month End Stock Price', axis=1).values
Y = training_data_df[['Change Month End Stock Price']].values


data_dim = 189
timesteps = 1
numberofSequence = 83840

X=X.reshape(numberofSequence,timesteps,data_dim)
Y=Y.reshape(numberofSequence,timesteps, 1)
model = Sequential()
model.add(LSTM(200, return_sequences=True,batch_input_shape=(32,timesteps,data_dim)))
model.add(LSTM(200, return_sequences=True))  
model.add(LSTM(100,return_sequences=True))  
model.add(LSTM(1,return_sequences=True, activation='linear'))
model.compile(loss='mse',
              optimizer='rmsprop',
              metrics=['accuracy'])


model.fit(X,Y,epochs=100)

1 个答案:

答案 0 :(得分:0)

  • batch_input_shape需要批量的大小:(numberofSequence,timesteps,data_dim)
  • input_shape只需要样本的形状:(timesteps,data_dim)

现在,有一个问题.... 84199不是4的倍数,我们怎么能期望以4的步长重塑它?

可能还有其他问题,例如:

  • 如果你有一个长序列,为什么要分步?
  • 如果您打算使用滑动窗口案例,则需要按以下方式准备数据:样本1 = [步骤1,步骤2,步骤3,步骤4];样品2 = [步骤2,步骤3,步骤4,步骤5];等等。这将意味着numberofSquence > 1(接近90000)
  • 如果您打算因内存/性能问题而划分单个序列,则应使用stateful=True并在每个纪元的开头调用model.reset_states()