1
votes

I am trying to build a Prediction model through Keras tensor flow in R studio but I am getting an error as below. Pls does anyone have a clue? this is my first time using Keras or deep learning and R as well. Please help me out, and suggest the changes. Thanks, mate!

library(keras)

train_data <-read.csv(file="trialtrainfinal.csv",head=FALSE)
test_data <-read.csv(file="trialtest.csv",head=FALSE)
train_targets <-read.csv(file="traintarget.csv",head=FALSE)

mean <- apply(train_data, 2, mean)
std <- apply(train_data, 2, sd)
train_data <- scale(train_data, center = mean, scale = std)
test_data <- scale(test_data, center = mean, scale = std)

build_model <- function() {
+ model <- keras_model_sequential() %>%
+ layer_dense(units = 64, activation = "relu",
+ input_shape = dim(train_data)[[2]]) %>%
+ layer_dense(units = 64, activation = "relu") %>%
+ layer_dense(units = 1)
+
+ model %>% compile(
+ optimizer = "rmsprop",
+ loss = "mse",
+ metrics = c("mae")
+ )
+ }

k <- 4
indices <- sample(1:nrow(train_data))
folds <- cut(1:length(indices), breaks = k, labels = FALSE)
num_epochs <- 100
all_scores <- c()
for (i in 1:k) {
+ cat("processing fold #", i, "\n")
+ val_indices <- which(folds == i, arr.ind = TRUE)
+ val_data <- train_data[val_indices,]
+ val_targets <- train_targets[val_indices,]
+
+
+ partial_train_data <- train_data[-val_indices,]
+ partial_train_targets <- train_targets[-val_indices]
+
+
+ model <- build_model()
+
+
+ model %>% fit(partial_train_data, partial_train_targets,
+ epochs = num_epochs, batch_size = 1, verbose = 0)
+
+
+ results <- model %>% evaluate(val_data, val_targets, verbose = 0)
+ all_scores <- c(all_scores, results$mean_absolute_error) + }
processing fold # 1
Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: No data provided for "dense_5". Need data for each key in: ['dense_5']

Detailed traceback: File "E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit use_multiprocessing=use_multiprocessing) File "E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 224, in fit distribution_strategy=strategy) File "E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 547, in _process_training_inputs use_multiprocessing=use_multiprocessing) File "E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 594, in _process_inputs steps=steps) File "E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2519, in _standardize_user_data exception_prefix='target') File "E:\Anaconda\envs\r-reticul

When I use the command "summary(model)", I get the following results: Model: "sequential"


Layer (type) Output Shape Param #

dense (Dense) (None, 64) 896


dense_1 (Dense) (None, 64) 4160


dense_2 (Dense) (None, 1) 65

Total params: 5,121 Trainable params: 5,121 Non-trainable params: 0


Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: No data provided for "dense_2". Need data for each key in: ['dense_2']

2

2 Answers

0
votes

Given the error message, it suggests that the data that is being fed into the model is not correctly populated. You may want to confirm that the partial_train_data and partial_train_targets actually have values in them, and are consistent in terms of shape etc with each other and what the network is expecting based on the design. There should be a command you can use like model.summary() right after the model build step which returns the network architecture in terms of data shapes/dimensions. I hope this helps.

0
votes

Try to convert train_data and test_data to a matrix. Use data.matrix or as.matrix