Support Callback_Early_Stopping in R

Viewed 504

I'm working on a Neural Network Project in R that is using Keras and Tensorflow. I'd really like to utilize the early stopping function, mainly in order to get the best weights, but I'm struggling to bypass the error: "TypeError: '>' not supported between instances of 'EarlyStopping' and 'float'." There was a similar question answered here: TypeError: '>' not supported between instances of 'NoneType' and 'float', but the solutions are all in python coding. Any assistance here using R commands would be great!

Here is my code:

# API model
model <- local({
  input = layer_input(shape = c(44),  name = 'main_input')
  
  layer1 = input %>%
    layer_dense(units = 22, activation = "relu")
  
  layer2 = input %>%
    layer_dense(units = 22, activation = "relu")
  
  output = layer_concatenate(c(layer1, layer2)) %>%
    layer_dense(units = 10, activation = "relu") %>%
    layer_dense(units = 2, activation = "sigmoid")
  
  keras_model(inputs = input, outputs = output)
})

model %>%
  compile(loss = "binary_crossentropy",
          optimizer = 'adam',
          metrics = "accuracy")

history = model %>%
  fit(train,
      trainLabels,
      epoch = 25,
      batch_size = 32,
      validation_split = 0.2)
     callback_early_stopping(monitor = "val_acc", patience = 5, restore_best_weights = TRUE))

#Error in py_compare_impl(a, b, op) : 
  TypeError: '>' not supported between instances of 'EarlyStopping' and 'float'

My data has been converted a numeric structure and is formatted in a matrix to run the Keras/Tensorflow commands. However, I understand that the variables in my model are float32:

str(train)
 num [1:2594, 1:44] 0.0202 -1.9071 0.0202 0.0202 -1.9071 ...

tail(model$variables)

...

[[5]]
<tf.Variable 'dense_21/kernel:0' shape=(10, 2) dtype=float32, numpy=
array([[-0.79153246,  0.20632687],
       [-0.21896149,  0.02037789],
       [-0.04512246,  0.29801697],
       [-0.46573785,  0.3391741 ],
       [-0.71519893,  0.8290489 ],
       [ 0.82080096, -0.13467254],
       [-0.60803676, -0.5283991 ],
       [ 0.8202912 , -0.89913267],
       [ 0.6229674 , -0.9428901 ],
       [ 1.1776545 , -0.8499227 ]], dtype=float32)>

[[6]]
<tf.Variable 'dense_21/bias:0' shape=(2,) dtype=float32, numpy=array([-0.09607901,  0.09400514], dtype=float32)>

I've also tried to build the model with "int32" as opposed to the float32, but it appears I can't build any of the dense layers without the float. Any thoughts/ideas on how I can move forward?

Thank you!

1 Answers

Don't know if you still need help with this problem, but you need to past your early stopping call inside of a list, as shown below.

callbacks = list(callback_early_stopping(monitor = "val_acc", patience = 5, restore_best_weights = TRUE))
Related