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

2eafrhcq  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(131)

我试图通过R studio中的Kerastensorflow 构建预测模型,但我得到了如下错误。请问有没有人有线索?这是我第一次使用Keras或深度学习和R。请帮助我,并提出修改意见。谢了伙计
库(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)
public 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)
  • optimizer ="rmsprop",
  • loss ="mse",
  • metrics = c("mae")
  • }
    k <-4
    indices <-sample(1:nrow(train_data))
    folds <-cut(1:长度(索引),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_index]
  • model <-build_model()
  • model %> % fit(partial_train_data,partial_train_targets,
  • epochs = num_epochs,batch_size = 1,verbose = 0)
  • results % evaluate(val_data,val_targets,verbose = 0)<- model %>% evaluate(val_data, val_targets, verbose = 0)
  • 所有分数<-c(所有分数,结果$mean_absolute_error)+}
    处理第1个折叠
    py_call_impl(callable,dots $args,dots $keywords)中的错误:ValueError:未提供“dense_5”的数据。需要每个键的数据:['dense_5']
    详细追溯:文件“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”,第224行,在fit distribution_strategy = strategy中)文件“E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training_v2. py ",第547行,in_process_training_inputs use_multiprocessing = use_multiprocessing)文件" E:\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training_v2. py”,第594行,in_process_inputs steps = steps)\Anaconda\envs\r-reticulate\lib\site-packages\tensorflow_core\python\keras\engine\training.py",line 2519,in_standardize_user_data exception_prefix ='target')文件" E:\anaconda\envs\r-reticul
    当我使用命令"summary(model)"时,我得到以下结果:型号:“sequential”

图层(类型)输出形状参数#

密集(密集)(无,64)896
dense_1(密集)(无,64)4160

dense_2(Dense)(None,1)65

总参数:5,121个可训练参数:5,121个不可训练的参数:0
py_call_impl(callable,dots $args,dots $keywords)中的错误:ValueError:未提供“dense_2”的数据。需要每个键的数据:['dense_2']

mutmk8jj

mutmk8jj1#

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

5f0d552i

5f0d552i2#

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

相关问题