简体   繁体   English

制作有状态 LSTM 时出现 InvalidArgumentError

[英]InvalidArgumentError when making a stateful LSTM

I'm working on a stateful LSTM to predict stock prices.我正在研究一个有状态的 LSTM 来预测股票价格。

These are the shapes of my input data: (updated)这些是我输入数据的形状:(更新)

x_train = (10269, 300, 89)
y_train = (10269, 1)
x_test = (4401, 300, 89)
y_test = (4401, 1)

This is my model initialisation:这是我的模型初始化:

batch_size = 63
timesteps = x_train.shape[1]
data_dim = x_train.shape[2]

model = Sequential()

model.add(LSTM(32, return_sequences=True, batch_input_shape=(batch_size, timesteps, data_dim), stateful=True))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))

model.add(Dense(1))

model.compile(optimizer = 'adam', loss = 'mean_squared_error')

But when I fit this I get the error:但是当我适应这个时,我得到了错误:

InvalidArgumentError:    Specified a list with shape [64,89] from a tensor with shape [29,89]
 [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
 [[sequential/lstm/PartitionedCall]] [Op:__inference_train_function_6536]

To my knowledge, I have defined batch_input_shape correctly and don't see what I have done wrong.据我所知,我已经正确定义了 batch_input_shape 并且没有看到我做错了什么。

Edit:编辑:

Some suggested that I try making my sample size divisible by my batch size.有人建议我尝试让我的样本量除以我的批量大小。 I tried that and got the same error.我试过了,得到了同样的错误。

(I updated my train and test sizes as seen above) (我更新了我的火车和测试尺寸,如上所示)

My new batch size is 63 and my data size 10269. 10269/63 = 163. This is the error:我的新批次大小是 63,我的数据大小是 10269。10269/63 = 163。这是错误:

InvalidArgumentError:    Specified a list with shape [63,89] from a tensor with shape [54,89]
 [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
 [[sequential_1/lstm_3/PartitionedCall]] [Op:__inference_test_function_20179]

When using stateful LSTMs your input must be evenly divisible by batch size.使用有状态 LSTM 时,您的输入必须能被批量大小整除。

In your case 3697 // 64 is not an integer.在您的情况下3697 // 64不是整数。

Since 3697 is a prime number, you need to drop a sample make your input size 3696. When you have 3696 samples, change to code according to (model definition stays same):由于 3697 是质数,因此您需要删除一个样本使您的输入大小为 3696。当您有 3696 个样本时,根据(模型定义保持不变)更改代码:

batch_size = 33 # some number that divides your samples evenly.
timesteps = x_train.shape[1]
data_dim = x_train.shape[2]

model.fit(x_train, y_train, batch_size = batch_size, ...)

This problem has to do with the stateful argument.这个问题与stateful参数有关。 when it is used, the number of samples should be divisible by the number of samples.使用时,样本数应能被样本数整除。

In your case, you have 3697 samples which is no divisible by 64.在您的情况下,您有 3697 个不能被 64 整除的样本。

So what you can do is remove 49 samples and take 3648 samples only since 3648 is divisible by 64.因此,您可以做的是删除 49 个样本并仅取 3648 个样本,因为 3648 可被 64 整除。

The same thing for the number of samples of validation data .验证数据的样本数量也是如此。 you have to change it to a number divisible by the batch size.您必须将其更改为可被批量大小整除的数字。

Secondly, use: model.fit(x_train, y_train, batch_size=batch_size,validation_data=(x_val,y_val))其次,使用: model.fit(x_train, y_train, batch_size=batch_size,validation_data=(x_val,y_val))

If you don't want to remove any samples from your dataset, you can work with a data generator as shown here如果你不想从数据集中删除任何样本,您可以用数据发生器工作,如图这里

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

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