使用 keras 在 R 中构建预测模型

问题描述 投票:0回答:2

我正在尝试通过 RStudio 中的 Keras 构建预测模型,但收到如下错误。怎么解决?

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'] 
r tensorflow keras
2个回答
0
投票

尝试将train_data和test_data转换为矩阵。 使用 data.matrix 或 as.matrix


0
投票

鉴于错误消息,这表明输入模型的数据未正确填充。 您可能想确认partial_train_data和partial_train_targets实际上具有值,并且在形状等方面彼此一致以及网络基于设计的期望。 在模型构建步骤之后应该有一个可以使用的命令,例如 model.summary(),它以数据形状/维度的形式返回网络架构。

© www.soinside.com 2019 - 2024. All rights reserved.