如果謂詞 pred
為真,則返回 true_fn()
否則 false_fn()
。 (不推薦使用的參數)
用法
tf.compat.v1.cond(
pred, true_fn=None, false_fn=None, strict=False, name=None, fn1=None, fn2=None
)
參數
-
pred
一個標量,確定是否返回true_fn
或false_fn
的結果。 -
true_fn
如果 pred 為真,則要執行的調用。 -
false_fn
如果 pred 為假,則要執行的可調用。 -
strict
啟用/禁用 'strict' 模式的布爾值;往上看。 -
name
返回的張量的可選名稱前綴。
返回
-
調用
true_fn
或false_fn
返回的張量。如果可調用對象返回單例列表,則從列表中提取元素。
拋出
-
TypeError
如果true_fn
或false_fn
不可調用。 -
ValueError
如果true_fn
和false_fn
不返回相同數量的張量,或者返回不同類型的張量。
警告:不推薦使用某些參數:(fn1, fn2)
。它們將在未來的版本中被刪除。更新說明:不推薦使用 fn1/fn2 以支持 true_fn/false_fn 參數。
true_fn
和 false_fn
都返回輸出張量的列表。 true_fn
和 false_fn
必須具有相同的非零數量和類型的輸出。
警告:無論在運行時選擇哪個分支,在 true_fn
和 false_fn
之外創建的任何張量或操作都將被執行。
盡管這種行為與 TensorFlow 的數據流模型是一致的,但它經常讓那些期待更惰性語義的用戶感到驚訝。考慮以下簡單程序:
z = tf.multiply(a, b)
result = tf.cond(x < y, lambda:tf.add(x, z), lambda:tf.square(y))
如果 x < y
,將執行 tf.add
操作,而不會執行 tf.square
操作。由於cond
的至少一個分支需要z
,因此始終無條件地執行tf.multiply
操作。
注意cond
調用true_fn
和false_fn
恰好一次(在調用內cond
, 並且在Session.run()
)。cond
將在創建過程中創建的圖形片段縫合在一起true_fn
和false_fn
調用一些額外的圖節點,以確保根據值執行正確的分支pred
.
tf.cond
支持在 tensorflow.python.util.nest
中實現的嵌套結構。 true_fn
和 false_fn
都必須返回相同的(可能是嵌套的)列表、元組和/或命名元組的值結構。單例列表和元組是唯一的異常:當 true_fn
和/或 false_fn
返回時,它們被隱式解包為單個值。通過傳遞 strict=True
禁用此行為。
例子:
x = tf.constant(2)
y = tf.constant(5)
def f1():return tf.multiply(x, 17)
def f2():return tf.add(y, 23)
r = tf.cond(tf.less(x, y), f1, f2)
# r is set to f1().
# Operations in f2 (e.g., tf.add) are not executed.
相關用法
- Python tf.compat.v1.confusion_matrix用法及代碼示例
- Python tf.compat.v1.convert_to_tensor用法及代碼示例
- Python tf.compat.v1.constant用法及代碼示例
- Python tf.compat.v1.count_nonzero用法及代碼示例
- Python tf.compat.v1.case用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.layers.conv3d用法及代碼示例
- Python tf.compat.v1.strings.length用法及代碼示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代碼示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代碼示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代碼示例
- Python tf.compat.v1.variable_scope用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.as_numpy_iterator用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.covariance用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.cond。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。