在执行图形时,不允许将"tf.Tensor"用作Python"bool".使用急切执行或用@ tf.function装饰此功能 [英] using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function
问题描述
当前我遇到此错误,有人可以解决吗?
Currently I faced this error, can anyone help solve it?
---------------------------------------------------------------------------
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
.这种情况通常发生在需要条件的地方,例如:
As the error message explain, you try to use a tf.Tensor
as a Python bool
. This happens generally where condition are expected like in:
if layer.output in self.keras_model.losses:
layer.output in self.keras_model.losses
部分应评估为一个张量,Python会尝试使用该张量来检查if
条件.
仅在急切执行时才允许这样做.
The part layer.output in self.keras_model.losses
should evaluate to a tensor that Python try to use as a bool to check the if
condition.
This is allowed in eager execution only.
您必须使用 tf.cond
转换if
构造,或依靠 @tf.function
为您完成工作.
You must either convert the if
construct with tf.cond
, or rely on @tf.function
to make the job for you.
没有更多的代码,很难帮助您更多...
Without more code, it is hard to help you more...
这篇关于在执行图形时,不允许将"tf.Tensor"用作Python"bool".使用急切执行或用@ tf.function装饰此功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!