gpt4 book ai didi

python - OperatorNotAllowedInGraphError : using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. 添加指标到编译函数

转载 作者:行者123 更新时间:2023-12-05 06:15:28 24 4
gpt4 key购买 nike

我正在使用卷积神经网络,在我开始运行它之后,我得到了问题中所述的错误:

OperatorNotAllowedInGraphError:在图形执行中不允许使用 tf.Tensor 作为 Python bool。使用 Eager execution 或使用 @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_densetrain(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_wrapperreturn func(*args, **kwargs)

File"/home/.local/lib/python3.7/site-packages/keras/engine/training.py",line 222, in compilemasks=masks)

File"/home/.local/lib/python3.7/site-packages/keras/engine/training.py",line 871, in _handle_metricsself._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_metricsmetric_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_functionupdate_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 decoratedupdate_op = update_state_fn(*args, **kwargs)

File"/home/.local/lib/python3.7/site-packages/keras/metrics.py",line 318, in update_statematches = self._fn(y_true, y_pred, **self._fn_kwargs)

File"/home/.local/lib/python3.7/site-packages/keras/metrics.py",line 1421, in initthresholds, default_threshold=default_threshold)

File"/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py",line 107, in parse_init_thresholdsassert_thresholds_range(to_list(thresholds))

File"/home/.local/lib/python3.7/site-packages/keras/utils/metrics_utils.py",line 98, in assert_thresholds_rangeinvalid_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 boolself._disallow_bool_casting()

File"/home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py",line 534, in _disallow_bool_castingself._disallow_in_graph_mode("using a tf.Tensor as a Python bool")

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.Tensor as a Python boolis not allowed in Graph execution. Use Eager execution or decoratethis function with @tf.function.

我知道这与我的指标有关。我最初是按如下所示运行它的:

   model.compile(loss='categorical_crossentropy', 
optimizer=opt,
metrics=['accuracy'])

但我想添加更多指标,所以我将其更改为:

    model.compile(loss='categorical_crossentropy', 
optimizer=opt,
metrics=['accuracy',
'Precision',
'Recall',
'AUC'])

或者:

    model.compile(loss='categorical_crossentropy', 
optimizer=opt,
metrics=['accuracy',
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall(),
'AUC'])

两者都不起作用并给我错误。

我能做些什么来解决这个问题?

谢谢!

最佳答案

我也遇到了类似的问题。由于 TensorFlow 升级到 2.x,要使用 keras API 指标,您应该实例化所有这些指标以便像这样编译:

model.compile(loss='categorical_crossentropy', 
optimizer=opt,
metrics=[tf.keras.metrics.Accuracy(),
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall(),
tf.keras.metrics.AUC()])

希望这有帮助:)

关于python - OperatorNotAllowedInGraphError : using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. 添加指标到编译函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62513422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com