在 Graph 执行中不允许使用 \`tf.Tensor\` 作为 Python \`bool\`。使用 Eager execution 或使用 @tf.function 修饰此函数

新手上路,请多包涵

目前我遇到这个错误,任何人都可以帮助解决吗?

 ---------------------------------------------------------------------------
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.

原文由 Vincent 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
1 个回答

我也偶然发现了这个问题,因此我将解决这个问题的方法留给任何人。

自从 tf 升级到 2.x 以来,当您处于急切执行模式时会遇到一个问题,如果您使用的是 keras API 损失和指标,则应实例化它们以便编译。

请参见下面的示例:

 model.compile(optimizer="...",
              loss=keras.losses.AnyLoss,
              metrics=[keras.metrics.AnyMetric])

上面的代码将给出 OperatorNotAllowedInGraphError 。要克服这个;

 my_loss = keras.losses.AnyLoss(lr, *args, **kwargs)
my_metric = keras.metrics.AnyMetric(*args, **kwargs)

model.compile(optimizer,
              loss = my_loss
              metrics = [my_metric_1, my_metric_2...]

这应该够了吧

原文由 Mert Beşiktepe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏