R Keras-模型是用形状(1,16,1)构造的,但它是在输入时用不兼容的形状(1,1,1)调用的

R Keras-模型是用形状(1,16,1)构造的,但它是在输入时用不兼容的形状(1,1,1)调用的,r,keras,lstm-stateful,R,Keras,Lstm Stateful,我试图将其应用于我的单变量数据,以学习R中的LSTM时间序列预测。我认为问题在于我定义模型时的步骤,但我不知道我遗漏了什么 #Data #function to create sliding windows training data with 16 time steps train_sliding = create_dataset(data = kt_train_male_scaled, n_input = 16, n_out = 16) X_train = train_sliding[[1

我试图将其应用于我的单变量数据,以学习R中的LSTM时间序列预测。我认为问题在于我定义模型时的步骤,但我不知道我遗漏了什么

#Data
#function to create sliding windows training data with 16 time steps
train_sliding = create_dataset(data = kt_train_male_scaled, n_input = 16, n_out = 16)

X_train = train_sliding[[1]]; dim(X_train)
[1] 97 16

y_train = train_sliding[[2]]; dim(y_train)
[1] 97 16

#Array transformation to Keras LSTM
dim(X_train) = c(dim(X_train), 1); dim(X_train)
[1] 97 16 1

#Model in Keras - I THINK THE PROBLEM IN THIS STEP
X_shape2 = dim(X_train)[2] #16
X_shape3 = dim(X_train)[3] #1
batch_size = 1 

model <- keras_model_sequential() 
model%>%
  layer_lstm(units = 64, activation = "relu", batch_size = batch_size, input_shape = c(dim(X_train)[2], dim(X_train)[3]),stateful= TRUE)%>%
        layer_dense(units = 5, activation = "relu") %>%
  layer_dense(units = 1, activation = "relu")
summary(model)

model %>% compile(
  loss = 'mse',
  optimizer = optimizer_adam(lr= 0.01, decay = 1e-6 ),  
  metrics = c('mae')
)

Epochs = 100   
for(i in 1:Epochs ){
  model %>% fit(X_train, y_train, epochs=1, batch_size=batch_size, verbose=1, shuffle=FALSE)
  model %>% reset_states()
}

L = length(kt_test_male_scaled); scaler = Scaled$scaler; predictions = numeric(L)
我所有的预测都是一样的

predictions
 [1] 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046
[15] 0.2654046 0.2654046
predictions
 [1] 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046 0.2654046
[15] 0.2654046 0.2654046