You are passing KerasTensor an intermediate Keras symbolic input/output

Viewed 2346

I'm triying to run the code of section 5.4.2 of the F. Chollet's book "Deep Learning with R", on Visualizing convnet filters. The code is as follows:

library(keras)
    
model <- application_vgg16(
  weights = 'imagenet',
  include_top = FALSE
)

layer_name <- 'block3_conv1'
filter_index <- 1

layer_output <- get_layer(model, layer_name)$output
loss <- k_mean(layer_output[,,,filter_index])

grads <- k_gradients(loss, model$input)[[1]]

but throws Error in py_call_impl(): ! RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

According to this github issue this can easily be resolved adding two lines of code at the beginning of the script:

library(tensorflow)
tf$compat$v1$disable_eager_execution()

and effectively, the tf$executing_eagerly() call transitions from TRUE to FALSE. Now, k_gradients() throws another error:

Error in `py_call_impl()`:
! TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(), dtype=tf.float32, name=None), name='tf.math.reduce_mean/Mean:0', description="created by layer 'tf.math.reduce_mean'"), an intermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. Keras Functional model construction only supports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the operation in a custom Keras layer `call` and calling that layer on this symbolic input/output.

How to overcome this issue and visualize convnet filters?

0 Answers
Related