I am trying to run a LSTM model on predicting the temperature, following the example on p193 of Deep Learning with R by Francois Chollet and J.J.Allaire. I keep encounting this problem but could not figure out how to solve it. Could you give me some suggestions please? Thank you very much.
Please find the code and the error below:
library(keras)
# make a fake data
dat=data.frame(y=rnorm(2000),x1=rnorm(2000),x2=rnorm(2000),x3=rnorm(2000),x4=rnorm(2000),x5=rnorm(2000))
head(dat)
names(dat)
dim(dat)
features=dat[,2:6]
target=dat[,1]
features=data.matrix(features) # convert data.frame to floating point matrix
target=data.matrix(target)
str(features)
str(target)
train_features=features[1:500,1:5]
test_features=features[501:1000,1:5]
train_target=target[1:500]
test_target=target[501:1000]
model <- keras_model_sequential() %>%
layer_embedding(input_dim=5,output_dim=32) %>%layer_lstm(units = 32) %>%layer_dense(units = 1)
model %>% compile(optimizer_rmsprop(),loss = "mae",metrics = c("acc"))
history <- model %>% fit(train_features, train_target,epochs = 10,batch_size = 50,validation_split = 0.2)
Error in py_call_impl(callable, dots$args, dots$keywords) :
InvalidArgumentError: indices[0,3] = -1 is not in [0, 5)
[[node sequential_5/embedding_5/embedding_lookup (defined at /site-packages/tensorflow/python/keras/engine/training.py:1100) ]] [Op:__inference_train_function_16869]
Errors may have originated from an input operation.
Input Source operations connected to node sequential_5/embedding_5/embedding_lookup:
sequential_5/embedding_5/embedding_lookup/15918 (defined at /contextlib.py:112)
Function call stack:
train_function
Detailed traceback:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 828, in call
result = self._call(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/pytho