简体   繁体   English

如何为Keras LSTM模型正确指定输入形状

[英]How to correctly specify input shape for a Keras LSTM model

I have a generator that yields my x and y which is what I'm using to train my LSMT model in Keras. 我有一个生成x和y的生成器,这就是我在Keras中训练LSMT模型所使用的生成器。

The X is of the format [0.0, 1.0, 0.0004]. X的格式为[0.0,1.0,0.0004]。

I set the shape of this data earlier: 我之前设置了此数据的形状:

x_out = np.array(list(x_drop.values())).reshape(len(x_drop), 1)
y_out = np.array([y])

Which I get in an async generator, so then I pass into a syncing function and then use model.fit_generator(): 我进入了一个异步生成器,然后进入一个同步函数,然后使用model.fit_generator():

train_gen = to_sync_generator(replay(traintime_0, traintime_1, test=0))
    model = lstm_model()

model.fit_generator(
    train_gen,
    epochs = 5,
    verbose = 1,
    steps_per_epoch=500,
    initial_epoch=0,
    validation_data=valid_gen,
    validation_steps=500)

My model code is below. 我的模型代码如下。

   def lstm_model():
       model = Sequential()
       model.add(LSTM(100, input_shape=(3,1)))
       model.add(Dense(1))
       model.compile(loss='mean_squared_error', optimizer='adam')
       return model

I get the following error: 我收到以下错误:

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (3, 1)

Note, I can not preprocess this data. 注意,我无法对此数据进行预处理。

I haven't really seen examples of using lstm with non-preprocessed data, any advise is greatly appreciated! 我还没有真正看到过将lstm与非预处理数据一起使用的示例,非常感谢任何建议! thank you. 谢谢。

Your data must be a tuple using the format (x, y, z), where => x is your batch number (you can initially start this with 1), y is your data array and z is your data shape. 您的数据必须是使用(x,y,z)格式的元组,其中=> x是批处理编号(您可以首先以1开头),y是数据数组,z是数据形状。 By your case, I can deduce your data have one feature and has 3 steps. 根据您的情况,我可以推断出您的数据具有一项功能并包含3个步骤。 So, you can prepare it using the following code: 因此,您可以使用以下代码进行准备:

data = data.reshape((1, 3, 1))

If you cannot change the data directly, you can merge it into a new array, then fit the array into the model 如果您不能直接更改数据,则可以将其合并到一个新的数组中,然后将该数组放入模型中

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

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