【问题标题】:InvalidArgumentError when making a stateful LSTM制作有状态 LSTM 时出现 InvalidArgumentError
【发布时间】:2021-09-04 16:06:46
【问题描述】:

我正在开发一个有状态的 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]

【问题讨论】:

    标签: python tensorflow deep-learning lstm lstm-stateful


    【解决方案1】:

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

    在你的例子中,你有 3697 个不能被 64 整除的样本。

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

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

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

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

    【讨论】:

    • 你的答案和我的有什么不同?现在有 2 个完全相同的答案。
    • 我更新了问题,您提出的建议与其他用户相同,但似乎不起作用
    • @Frightera,我们显然是同时发布的。 2
    • @MGeureka 你用过model.fit(x_train, y_train, batch_size=batch_size)吗?否则,你能告诉我们你如何适合你的模型吗?因为我已经尝试了相同的代码并且一切正常。
    • 顺便说一句,你应该对验证数据的样本数做同样的事情,4401不能除以63。
    【解决方案2】:

    使用有状态 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, ...)
    

    【讨论】:

    • 我已经尝试了这些更改,它们似乎没有改变任何东西。查看已编辑的问题。
    • 对您的测试或验证数据做同样的事情,在拟合时设置验证批量大小。应该没有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2016-11-09
    • 2019-02-14
    • 2018-04-04
    • 2021-06-30
    相关资源
    最近更新 更多