0

我正在研究一个有状态的 LSTM 来预测股票价格。

这些是我的输入数据的形状:(更新)

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

这是我的模型初始化:

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

但是当我适合这个时,我得到了错误:

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]

据我所知,我已经正确定义了 batch_input_shape 并且看不到我做错了什么。

编辑:

一些人建议我尝试让我的样本大小可以被我的批量大小整除。我试过了,得到了同样的错误。

(如上所示,我更新了我的训练和测试大小)

我的新批量大小为 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]
4

2 回答 2

0

这个问题与stateful论点有关。使用时,样本数应能被样本数整除。

在您的情况下,您有 3697 个样本,不能被 64 整除。

因此,您可以做的是删除 49 个样本并仅获取 3648 个样本,因为 3648 可以被 64 整除。

验证数据的样本数量也是如此。您必须将其更改为可被批量大小整除的数字。

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

如果您不想从数据集中删除任何样本,您可以使用数据生成器,如下所示

于 2021-06-20T22:44:52.720 回答
0

使用有状态 LSTM 时,您的输入必须能被批量大小整除。

在您的情况下3697 // 64不是整数。

由于 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, ...)
于 2021-06-20T22:36:41.013 回答