ijustdontget it
ijustdontget it

Reputation: 21

Building a prediction model in R with keras

I am trying to build a Prediction model through Keras in RStudio but I am getting an error as below. How to resolve it?

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'] 

Upvotes: 0

Views: 949

Answers (2)

ad2004
ad2004

Reputation: 809

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.

Upvotes: 0

user236137
user236137

Reputation: 1

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

Upvotes: 0

Related Questions