拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 R中的KerastuneR

R中的KerastuneR

白鹭 - 2022-02-23 2292 0 0

我正在尝试使用 kerastuneR 来实作此代码,以便进行超自变量调整。

library(keras)
library(tensorflow)
library(kerastuneR)
library(dplyr)
    
x_data <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data <-  ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()

x_data2 <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data2 <-  ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()

build_model = function(hp) {
  
  model = keras_model_sequential()
  model %>% layer_dense(units=hp$Int('units',
                                     min_value=32,
                                     max_value=512,
                                     step=32),
                        input_shape = ncol(x_data),
                        activation='relu') %>% 
    layer_dense(units=1, activation='sigmoid') %>% 
    compile(
      optimizer= tf$keras$optimizers$Adam(
        hp$Choice('learning_rate',
                  values=c(1e-2, 1e-3, 1e-4))),
      loss='binary_crossentropy',
      metrics='accuracy') 
  return(model)
}



tuner = RandomSearch(
  build_model,
  objective = 'val_accuracy',
  max_trials = 5,
  executions_per_trial = 3,
  directory = 'my_dir',
  project_name = 'helloworld')
    
tuner %>% search_summary()
 
# Fit   
tuner %>% fit_tuner(x_data,y_data,
                    epochs=5, 
                    validation_data = list(x_data2,y_data2))

所以这段代码运行良好,期望最后一段代码给出这个错误:

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Objective value missing in metrics reported to the Oracle, expected: ['val_accuracy'], found: dict_keys(['loss', 'acc', 'val_loss', 'val_acc']) 

那么任何人都可以帮助如何解决这个错误吗?

uj5u.com热心网友回复:

该错误为您提供了解决问题的关键。您需要将生成的键的名称fit_tuner与提供给RandomSearch函式的键的名称相匹配。尝试在函式中用 'val_accuracy' 代替 'val_acc' RandomSearch

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *