Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

KerastuneR in R

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading