Keras有状态LSTM错误

时间:2016-09-02 18:09:22

标签: keras lstm

我想在keras中创建有状态的LSTM。我给了它一个这样的命令:

model.add(LSTM(300,input_dim=4,activation='tanh',stateful=True,batch_input_shape=(19,13,4),return_sequences=True))

批量大小= 19。但在运行时会出现错误

 Exception: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 8816 samples. Batch size: 32.

我没有在我的脚本中的任何地方指定批量大小32,而19可以被8816整除

4 个答案:

答案 0 :(得分:11)

model.fit()执行批处理(例如,与model.train_on_batch相对)。因此,它有一个batch_size参数,默认为32。

将其更改为您的输入批量大小,它应该按预期工作。

示例:

batch_size = 19

model = Sequential()
model.add(LSTM(300,input_dim=4,activation='tanh',stateful=True,batch_input_shape=(19,13,4),return_sequences=True))

model.fit(x, y, batch_size=batch_size)

答案 1 :(得分:3)

动态调整数据和批次的大小:

大小数据和训练样本分割:

data_size = int(len(supervised_values))
train_size_initial = int(data_size * train_split)
x_samples = supervised_values[-data_size:, :]

培训样本的批量大小为批量大小:

if train_size_initial < batch_size_div:
    batch_size = 1
else:
    batch_size = int(train_size_initial / batch_size_div)
train_size = int(int(train_size_initial / batch_size) * batch_size)  # provide even division of training / batches
val_size = int(int((data_size - train_size) / batch_size) * batch_size)  # provide even division of val / batches
print('Data Size: {}  Train Size: {}   Batch Size: {}'.format(data_size, train_size, batch_size))

将数据拆分为训练集和验证集

train, val = x_samples[0:train_size, 0:-1], x_samples[train_size:train_size + val_size, 0:-1]

答案 2 :(得分:1)

有两种情况可能发生batch_size错误。

  1. model.fit(train_x,train_y, batch_size = n_batch ,shuffle = True,verbose = 2)

  2. trainPredict = model.predict(train_x, batch_size = n_batch ) 或testPredict = model.predict(test_x, batch_size = n_batch

  3. 在这两种情况下,你都要提到不。批次。

    注意:我们需要预测列车和测试两者,所以最佳做法是划分测试和训练,使得批量大小是 stateful = True < / strong>案例

答案 3 :(得分:0)

培训和验证数据都需要按批次大小进行整除。确保使用批处理大小的模型的任何部分都采用相同的数字(例如,在LSTM层中为batch_input_shape,在batch_sizemodel.fit()中为model.predict()。和验证数据(如果需要的话)。

例如

>>> batch_size = 100
>>> print(x_samples_train.shape)
>>> print(x_samples_validation.shape)
    (42028, 24, 14)
    (10451, 24, 14) 

# Down-sample so training and validation are both divisible by batch_size
>>> x_samples_train_ds = x_samples_train[-42000:]
>>> print(x_samples_train_ds.shape)
>>> y_samples_train_ds = y_samples_train[-42000:]
>>> print(y_samples_train_ds.shape)
    (42000, 24, 14)
    (42000,)
>>> x_samples_validation_ds = x_samples_validation[-10000:]
>>> print(x_samples_validation_ds.shape)
>>> y_samples_validation_ds = y_samples_validation[-10000:]
>>> print(y_samples_validation_ds.shape)
    (10000, 24, 14)
    (10000,)