在kerasR中更正LSTM的input_shape

时间:2017-11-29 21:56:13

标签: r keras lstm

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

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

Select Cast(DatePart(Month, GR_Date) As Varchar(2)) + ' ' + Cast(DatePart(Year, GR_Date) As varchar(4))

然而,当我运行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

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

Y_train是长度为2000的数字向量

我应该补充一点,当我尝试使用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)的3维值来指定跟随input_shape结构的输入形状时,我在添加时会出现这样的错误LSTM层到模型,

(samples, timesteps, features)

1 个答案:

答案 0 :(得分:0)

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

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

此外,数据集中的行数为samples,而不是timesteps。您可以详细了解samplestimestepsfeatures here