1

The fit_tuner function in KerasTuneR, when used in Rstudio, only prints its progress for the first trial. The simplified code below

library(tidyverse)
library(tensorflow)
library(keras)
library(kerastuneR)
library(ggplot2)

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

hp = HyperParameters()
hp$Int('num_layers', 5L, 20L, 2L)
hp$Int('units', 60L, 160L, 10L)

build_model <- function(hp) { 
  
  model <- keras_model_sequential() %>%
    layer_dense(units = hp$get('units'), activation = 'relu', input_shape = 5)
  
  for (i in 1:hp$get('num_layers')) { 
    model %>% 
      layer_dense(units = hp$get('units'), activation = 'relu')
  }
  
  model %>% layer_dense(units = 1)
  
  model %>% compile(optimizer = 'adam', 
                    loss = 'binary_crossentropy', 
                    metrics = c('accuracy'))
  
  return(model)
  
}

tuner = RandomSearch(
  hypermodel =  build_model,
  max_trials = 3,
  executions_per_trial = 2,
  hyperparameters = hp,
  tune_new_entries = T,
  objective = 'val_accuracy',
  #directory = 'C:/Users/Penelope/Desktop/NN_models/',
  #project_name = 'BigFoot2', 
  overwrite = TRUE
)

tuner %>% fit_tuner(x = x_data, y = y_data, 
                    epochs = 2, 
                    validation_data = list(x_data2, y_data2))

print("Completed")

Should print the output for every trial, but instead it prints only this:

Search: Running Trial #1

Value             |Best Value So Far |Hyperparameter
13                |?                 |num_layers
110               |?                 |units

Epoch 1/2
2/2 [==============================] - 1s 220ms/step - loss: 4.5302 - accuracy: 0.6000 - val_loss: 1.5124 - val_accuracy: 0.6000
Epoch 2/2
2/2 [==============================] - 0s 26ms/step - loss: 1.4636 - accuracy: 0.6000 - val_loss: 1.1391 - val_accuracy: 0.6000
Epoch 1/2
2/2 [==============================] - 1s 166ms/step - loss: 2.3739 - accuracy: 0.6000 - val_loss: 1.3020 - val_accuracy: 0.6000
Epoch 2/2
2/2 [==============================] - 0s 21ms/step - loss: 1.2651 - accuracy: 0.6000 - val_loss: 0.9689 - val_accuracy: 0.6000

Interestingly, it never prints "Completed", even though that's the final line of my code.

If I run this directly in R (Rx64 4.1.2), it does print all the output (just all as one giant dump, rather than line by line).

Is this a known issue and how can I fix it? Thanks in advance.

Mas
  • 329
  • 2
  • 13
  • What do you mean with directly in R? Just in Rstudio console? – Quinten Aug 22 '22 at 11:41
  • 1
    I mean the Rgui, which is part of R, but separate to Rstudio. (Rgui is at C:\Program Files\R\R-4.1.2\bin\x64\Rgui.exe; Rstudio is at C:\Program Files\RStudio\bin\rstudio.exe). The fact that it works in Rgui.exe makes me think that Rstudio might have a problem displaying the output from Python. – Mas Aug 22 '22 at 12:07
  • That is strange. Maybe you can create an issue on GitHub? It seems to be a bug on Rstudio. – Quinten Aug 22 '22 at 12:32

1 Answers1

0

R studio is not able to interact with Python - they're two different languages, so it's not what @Quinten suggested. The problem must be with the R-version of the Keras code itself.

DrAI
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 01:43