當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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__。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。