Keras有状态LSTM错误



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

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

,其中batch size=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整除

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)

有两种情况可能出现batch_size错误。

  1. 模型。fit(train_x, train_y, batch_size= n_batch, shuffle=True,verbose=2)

  2. trainPredict = model。预测(train_x batch_size = n_batch )或者testPredict = model.predict(test_x,batch_size=n_batch)

在这两种情况下,您都必须提到no。批次。

注意:我们需要预测训练和测试两者,所以最佳实践是将测试和训练分开,在有状态=True情况下

动态调整数据大小和批量:

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

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]

训练和验证数据都需要被批大小整除。确保使用批处理大小的模型的任何部分使用相同的数字(例如,在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,)

相关内容

  • 没有找到相关文章

最新更新