gpt4 book ai didi

python - 图形执行中不允许使用 `tf.Tensor` 作为 Python `bool`。使用 Eager execution 或使用 @tf.function 装饰此函数

转载 作者:行者123 更新时间:2023-12-03 14:40:27 42 4
gpt4 key购买 nike

目前我遇到了这个错误,有人可以帮忙解决吗?

---------------------------------------------------------------------------
OperatorNotAllowedInGraphError Traceback (most recent call last)
<ipython-input-24-0211c82920d0> in <module>
7 warnings.filterwarnings("ignore")
8 model.train(dataset_train,dataset_val, learning_rate=config.LEARNING_RATE,epochs=5,
----> 9 layers='heads')
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources)
2355 log("Checkpoint Path: {}".format(self.checkpoint_path))
2356 self.set_trainable(layers)
-> 2357 self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
2358
2359 # Work-around for Windows: Keras fails on Windows when using
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in compile(self, learning_rate, momentum)
2168 for name in loss_names:
2169 layer = self.keras_model.get_layer(name)
-> 2170 if layer.output in self.keras_model.losses:
2171 continue
2172 loss = (
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __bool__(self)
763 `TypeError`.
764 """
--> 765 self._disallow_bool_casting()
766
767 def __nonzero__(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_bool_casting(self)
532 else:
533 # Default: V1-style Graph execution.
--> 534 self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
535
536 def _disallow_iteration(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_in_graph_mode(self, task)
521 raise errors.OperatorNotAllowedInGraphError(
522 "{} is not allowed in Graph execution. Use Eager execution or decorate"
--> 523 " this function with @tf.function.".format(task))
524
525 def _disallow_bool_casting(self):

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.

最佳答案

正如错误消息所解释的,您尝试使用 tf.Tensor作为 Python bool .这通常发生在预期条件如下:

if layer.output in self.keras_model.losses:

零件 layer.output in self.keras_model.losses应该评估为一个张量,Python 尝试将其用作 bool 来检查 if健康)状况。
这仅在急切执行中允许。

您必须转换 if tf.cond 构建,或依赖 @tf.function 为您完成工作。

没有更多的代码,很难帮助你更多......

关于python - 图形执行中不允许使用 `tf.Tensor` 作为 Python `bool`。使用 Eager execution 或使用 @tf.function 装饰此函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308263/

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