Here is my code:
used_time_period = "2009-01-01::2017-04-01"
data_used = data_input[used_time_period,]
split_coefficient = 0.8
train_set_rate = round(nrow(data_used) * split_coefficient)
data_train = data_used[1:train_set_rate,]
data_test = data_used[(train_set_rate + 1):nrow(data_used),]
model = keras_model_sequential() %>%
layer_simple_rnn(units = 75, input_shape = dim(data_train[,1:3]), activation = "relu", return_sequences = TRUE) %>%
layer_dense(units = 2, activation = "relu")
model %>% compile(optimizer = "adam", loss = "binary_crossentropy", metrics = "binary_accuracy")
history = model %>% fit(x = data_train[,1:3], y = data_train[,4:5], epochs = 40, batch_size = 20)
the error i'm getting is this:
ValueError: Error when checking input: expected simple_rnn_input to have 3 dimensions, but got array with shape (1661, 3)
dim(data_train[,1:3]) = (1661, 3)
dim(data_train[,4:5]) = (1661, 2)
What am I doing wrong?
As the error message says, layer_simple_rnn
requires a 3D array but you are using a data.frame
, which is a 2D array (a table with rows and columns).
According to the Keras documentation, the recurrent layer needs an array with shape (batch_size, timesteps, input_dim)
. Assuming each column corresponds to a different date (correct me if I'm wrong), this should probably work:
dim(data_train[, 1:3]) # [1] 10 3
X <- as.matrix(data_train[, 1:3]) # Convert to an array
dim(X) # [1] 10 3
dim(X) <- c(dim(X), 1)
dim(X) # [1] 10 3 1
# The same for Y
Y <- as.matrix(data_train[, 4:5])
dim(Y) <- c(dim(Y), 1)
Now X
and Y
have 3 dimensions and you can feed them to your model:
history = model %>% fit(x = X, y = Y, epochs = 40, batch_size = 20)