[英]keras input shape for multivariate LSTM
I'm trying to fit a LSTM model in keras where I have two inputs 我正在尝试在有两个输入的喀拉拉邦中拟合LSTM模型
y
is the output with shape (100,10) x
is the input with shape (100,20) y
是形状为(100,10)的输出x
是形状为(100,20)的输入
library(keras)
x_train_vec <- matrix(rnorm(2000), ncol = 20, nrow = 100)
x_train_arr <- array(data = x_train_vec, dim = c(nrow(x_train_vec), 1, 20))
y_train_vec <- matrix(rnorm(1000), ncol = 10, nrow = 100)
y_train_arr <- array(data = y_train_vec, dim = c(nrow(x_train_vec), 1, 10))
> dim(x_train_arr)
[1] 100 1 20
> dim(y_train_arr)
[1] 100 1 10
Now I want to fit the LSTM model 现在我要拟合LSTM模型
model <- keras_model_sequential()
model %>%
layer_lstm(units = 50,
input_shape = c(1,10),
batch_size = 1) %>%
layer_dense(units = 1)
model %>%
compile(loss = 'mae', optimizer = 'adam')
model %>% fit(x = x_train_arr,
y = y_train_arr,
batch_size = 1,
epochs = 10,
verbose = 1,
shuffle = FALSE)
But I get this error: 但是我得到这个错误:
Error in py_call_impl(callable, dots$args, dots$keywords) :
py_call_impl中的错误(可调用,dots $ args,dots $ keywords):
ValueError: Error when checking input: expected lstm_21_input to have shape (1, 10) but got array with shape (1, 20)ValueError:检查输入时出错:预期lstm_21_input具有形状(1,10),但具有形状(1,20)的数组
If I change input size to c(1,20), I get: 如果将输入大小更改为c(1,20),则会得到:
Error in py_call_impl(callable, dots$args, dots$keywords) :
py_call_impl中的错误(可调用,dots $ args,dots $ keywords):
ValueError: Error when checking target: expected dense_13 to have 2 dimensions, but got array with shape (100, 1, 10)ValueError:检查目标时出错:预期density_13具有2维,但数组的形状为(100,1,10)
I also played with different setting but it never works. 我也使用了不同的设置,但从未奏效。
IF your Keras version is < 2.0 you need to use model.add(TimeDistributed(Dense(1))). 如果您的Keras版本小于2.0,则需要使用model.add(TimeDistributed(Dense(1)))。
NOTE that syntax is for python, you need to find the R equivealent. 注意该语法是针对python的,您需要找到R等值。
I figured out how to make it work: 我想出了使它工作的方法:
x_train_vec <- matrix(rnorm(2000), ncol = 20, nrow = 100)
x_train_arr <- array(data = x_train_vec, dim = c(nrow(x_train_vec), 20, 1))
y_train_vec <- matrix(rnorm(1000), ncol = 10, nrow = 100)
y_train_arr <- array(data = y_train_vec, dim = c(nrow(x_train_vec), 10))
model <- keras_model_sequential()
model %>%
layer_lstm(units = 50,
input_shape = c(20,1),
batch_size = 1) %>%
layer_dense(units = 10)
model %>%
compile(loss = 'mae', optimizer = 'adam')
model %>% fit(x = x_train_arr,
y = y_train_arr,
batch_size = 1,
epochs = 10,
verbose = 1,
shuffle = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.