Keras有状态的LSTM错误

4

我想在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个回答

13

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
然后,您应该使用最小的工作示例更新您的问题以复制错误。 - nemo
1
如果您正在使用validation_data,那么样本数量也必须能够被batch_size整除。 - Renel Chesak

5

有两种情况可能会出现批处理大小错误。

  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)

在这两种情况下,您必须提到批次的数量。

注意: 我们需要预测测试和训练两者,因此最好将测试和训练数据分为多个批次,在stateful=True的情况下,批量大小是两者的倍数。


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

训练和验证数据都需要被批量大小整除。确保模型的任何部分使用相同数量的批量大小(例如,您LSTM层中的batch_input_shape,以及model.fit()model.predict()中的batch_size)。如果需要,请对训练和验证数据进行下采样以使其正常工作。

例如:

>>> 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,)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接