【问题标题】:Correct input_shape for an LSTM in kerasRkerasR 中 LSTM 的正确 input_shape
【发布时间】:2017-11-29 21:56:12
【问题描述】:

我在 python 中看到了很多类似主题的帮助,但我使用的是 R 实现,似乎无法复制任何建议的解决方案。

我正在尝试像这样设置 LSTM,

mod <- Sequential()

mod$add(LSTM(50, activation = 'relu', dropout = 0.25, input_shape = c(dim(X_train_scaled)[1], dim(X_train_scaled)[2]), return_sequences = TRUE))

mod$add(Dense(1))

keras_compile(mod,  loss = 'mean_squared_error', optimizer = 'adam')

keras_fit(mod, X_train_scaled, Y_train, batch_size = 72, epochs = 10, verbose = 1, validation_split = 0.1)

但是,当我运行 keras_fit 时,出现以下错误,

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Error when checking input: expected lstm_36_input to have 3 dimensions, but got array with shape (2000, 44)

X_train 是一个数值矩阵,有 2000 行 44 列,代表 2000 个时间步长和每个时间步长 44 个特征的值

Y_train 是一个长度为 2000 的数值向量

我应该补充一点,当我尝试为 input_shape 使用 3 维值以指定遵循 (samples, timesteps, features) 结构的输入形状时,当我将 LSTM 层添加到型号,

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Input 0 is incompatible with layer lstm_37: expected ndim=3, found ndim=4

【问题讨论】:

  • 我遇到了完全相同的问题。从昨天开始,我一直在寻找解决方案……唉!
  • link 你看到像input_shape = list(NULL, dim(data)[[-1]]) 这样的符号,它会抛出错误尝试从获取真实索引中获取多个值,但是,input_shape = c(NULL,dim(train_norm_arr)[-1]) 有效,并且在 Andres 回答时给出 ndim=3 . HTH,我自己在 LSTM 中辗转反侧。

标签: r keras lstm


【解决方案1】:

您的训练矩阵应该是 3 维的 (samples, timesteps, features)。然后你必须为input_shape 使用第二和第三维度:

input_shape = c(dim(X_train_scaled)[2], dim(X_train_scaled)[3])

此外,数据集中的行数是samples,而不是timesteps。你可以阅读更多关于samplestimestepsfeatureshere的信息。

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 2020-01-22
    • 1970-01-01
    • 2019-06-09
    • 2020-12-10
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多