I am using a Convolutional neural Network and after I start running it I get the error stated in my question:
OperatorNotAllowedInGraphError: using a tf.Tensor as a Python bool is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
Traceback (most recent call last):
File "/home/CNN1_FOLD1.py", line 452, in main_dense(0.1, DenseNet, error_anal = True, depth=depth)
File "/home/CNN1_FOLD1.py", line 440, in main_dense train(model, x_train, y_train, x_val, y_val, epochs_list = [500, 5000, 300], name = model_name, learning_rate = learning_rate)
File "/home/CNN1_FOLD1.py", line 320, in train 'AUC'])
File "/home/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(*args, **kwargs)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 222, in compile masks=masks)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 871, in _handle_metrics self._per_output_metrics[i], target, output, output_mask)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training.py", line 842, in _handle_per_output_metrics metric_fn, y_true, y_pred, weights=weights, mask=mask)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_utils.py", line 1033, in call_metric_function update_ops = metric_fn.update_state(y_true, y_pred, sample_weight=weights)
File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 42, in decorated update_op = update_state_fn(*args, **kwargs)
File "/home/.local/lib/python3.7/site-packages/keras/metrics.py", line 318, in update_state matches = self._fn(y_true, y_pred, **self._fn_kwargs)
File "/home/.local/lib/python3.7/site-packages/keras/metrics.py", line 1421, in init thresholds, default_threshold=default_threshold)
File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 107, in parse_init_thresholds assert_thresholds_range(to_list(thresholds))
File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 98, in assert_thresholds_range invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]
File "/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 98, in invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]
File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 765, in bool self._disallow_bool_casting()
File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 534, in _disallow_bool_casting self._disallow_in_graph_mode("using a
tf.Tensoras a Pythonbool")File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 523, in _disallow_in_graph_mode " this function with @tf.function.".format(task))
OperatorNotAllowedInGraphError: using a
tf.Tensoras a Pythonboolis not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
I understand it is related to my metrics. I was initially running it as shown below:
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
But I wanted to add some more metrics, so I changed it to:
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy',
'Precision',
'Recall',
'AUC'])
Or:
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy',
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall(),
'AUC'])
Both did not work and gave me the error.
What could I do to solve the issue?
thank you!