简体   繁体   English

正确的kerasR中LSTM的input_shape

[英]Correct input_shape for an LSTM in kerasR

I see a lot of help for similar topics in python but I was using the R implementation and can't seem to replicate any of the suggested solutions. 我在python中的类似主题上看到了很多帮助,但是我使用的是R实现,似乎无法复制任何建议的解决方案。

I am attempting to setup an LSTM like so, 我试图像这样设置一个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)

However, when I run the keras_fit I get the following error, 但是,当我运行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)

The X_train is a numeric matrix with 2000 rows and 44 columns that represent 2000 timesteps and the values of 44 features at each timestep X_train是一个数字矩阵,具有2000行和44列,分别代表2000个时间步和每个时间步的44个要素的值

The Y_train is a numeric vector of length 2000 Y_train是长度为2000的数值向量

I should add that when I attempt to use a 3 dimensional value for the input_shape so as to specify an input shape that follows the (samples, timesteps, features) structure, I get an error like this when I add the LSTM layer to the model, 我应该补充一点,当我尝试对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

Your train matrix should be 3-dimensional (samples, timesteps, features) . 您的训练矩阵应该是3维的(samples, timesteps, features) Then you have to use 2nd and 3rd dimensions for input_shape : 然后,您必须将2nd和3rd维度用于input_shape

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

Also, number of rows in your dataset is samples , not timesteps . 此外,数据集中的行数是samples ,而不是timesteps You can read more about samples , timesteps and features here . 您可以在此处阅读有关samplestimestepsfeatures更多信息。

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

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