建立用于库存预测的LSTM

时间:2019-04-12 18:44:30

标签: r keras lstm

我正在R中使用keras构建我的第一个LSTM模型。我相信我的问题出在我的input_shape上,感谢您的帮助。

我正在使用时间t的期末股票收益来预测t + 1的收益 所以我认为我的输入形状应该等于1。

这是我的代码:

#LSTM Model 
model <- keras::keras_model_sequential() %>% 
  layer_lstm(units = 50, return_sequences = TRUE, dropout = 0.2, weights = 0.01, input_shape = 1
             )%>% 
  layer_lstm(units = 100, return_sequences = FALSE, dropout = 0.2, weights = 0.01
            )%>%
  layer_dense(units = 1, activation = 'linear')

model %>% compile(
  optimizer = optimizer_rmsprop(),
  loss = "mse"
)

但是,当我运行它时,出现以下错误消息:

error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Input 0 is incompatible with layer lstm_12: expected ndim=3, found ndim=2

关于我在做什么错的任何想法以及我应该做什么的建议?

谢谢您的时间。

我相信我提供的信息足以回答我的问题。如果没有,这是我获取数据和结果的完整代码

#Downloading Stock Price and normalize to returns    
BRI_Price <-quantmod::getSymbols("BBRI.JK", src = "yahoo", from = as.Date("2015-01-01"), to = as.Date("2017-02-17"), by = "day", auto.assign = FALSE) 
Returns_Closing <-quantmod::dailyReturn(BRI_Price$BBRI.JK.Close) 

#Training and Testing Data 
train_size <- round(.9*nrow(Returns_Closing)) 
test_size <- nrow(Returns_Closing) - train_size 
train <- Returns_Closing[0:train_size,] 
test <- Returns_Closing[train_size:nrow(Returns_Closing),] 

#Reshape into X=t and Y=t+1 
x_train <- Returns_Closing[1:train_size,] 
y_train <- Returns_Closing[2:train_size+1] 
x_test <- Returns_Closing[train_size:nrow(Returns_Closing),] 
y_test <- Returns_Closing[468:nrow(Returns_Closing),] 

#LSTM Model 
model <- keras::keras_model_sequential() %>% 
layer_lstm(units = 50, return_sequences = TRUE, dropout = 0.2, weights = 0.01, input_shape = 1 )%>% 
layer_lstm(units = 100, return_sequences = FALSE, dropout = 0.2, weights = 0.01 )%>% 
layer_dense(units = 1, activation = 'linear') 

model %>% 
compile( optimizer = optimizer_rmsprop(), loss = "mse" ) 
#train the model 
Train_Model <- model %>% fit( x = x_train, batch_size = 100, epochs = 1, validation_data = list(x_test, y_test) )

0 个答案:

没有答案