I am trying to implement this code with using kerastuneR in order to make hyperparameter tuning.

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))

So this code functioning well expect last piece of code which give this error:

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

So can anybody help how to solve this error ?


The error is giving you the key to solve the problem. You need to match the names of the keys produced by fit_tuner with the ones provided to the RandomSearch function. Try substituting 'val_accuracy' for 'val_acc' in the RandomSearch function.