当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.Tensor.__bool__用法及代码示例


用法

__bool__()

抛出

  • TypeError

防止张量被用作 Python bool 的虚拟方法。

当用户在 AutoGraph 未转换的代码中无意中将 Tensor 视为布尔值(最常见于 ifwhile 语句中)时,此重载会引发 TypeError。例如:

if tf.constant(True): # Will raise.
  # ...

if tf.constant(5) < tf.constant(7): # Will raise.
  # ...

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.Tensor.__bool__。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。