創建案例操作。
用法
tf.compat.v1.case(
pred_fn_pairs, default=None, exclusive=False, strict=False,
name='case'
)
參數
-
pred_fn_pairs
布爾標量張量和返回張量列表的可調用對象的字典或列表。 -
default
返回張量列表的可選可調用對象。 -
exclusive
如果最多允許一個謂詞計算為True
,則為真。 -
strict
啟用/禁用 'strict' 模式的布爾值;往上看。 -
name
此操作的名稱(可選)。
返回
-
由謂詞評估為 True 的第一對返回的張量,如果沒有,則由
default
返回的張量。
拋出
-
TypeError
如果pred_fn_pairs
不是列表/字典。 -
TypeError
如果pred_fn_pairs
是一個列表但不包含 2 元組。 -
TypeError
如果fns[i]
對於任何 i 都不可調用,或者default
不可調用。
pred_fn_pairs
參數是大小為 N 的字典或對列表。每對包含一個布爾標量張量和一個 python 可調用函數,如果布爾值計算為 True,則創建要返回的張量。 default
是一個可調用的生成張量列表。 pred_fn_pairs
和 default
(如果提供)中的所有可調用對象都應返回相同數量和類型的張量。
如果 exclusive==True
,則評估所有謂詞,如果多個謂詞評估為 True
,則會引發異常。如果 exclusive==False
,則執行在第一個計算結果為 True 的謂詞處停止,並立即返回相應函數生成的張量。如果沒有任何謂詞評估為 True,則此操作返回由 default
生成的張量。
tf.case
支持 tf.nest
中實現的嵌套結構。所有可調用對象必須返回相同的(可能是嵌套的)列表、元組和/或命名元組的值結構。單例列表和元組是唯一的異常:當由可調用對象返回時,它們被隱式解包為單個值。通過傳遞 strict=True
禁用此行為。
如果 pred_fn_pairs
使用無序字典,則無法保證條件測試的順序。但是,順序保證是確定性的,因此在條件分支中創建的變量在運行中以固定順序創建。
示例 1:
偽代碼:
if (x < y) return 17;
else return 23;
表達式:
f1 = lambda:tf.constant(17)
f2 = lambda:tf.constant(23)
r = tf.case([(tf.less(x, y), f1)], default=f2)
示例 2:
偽代碼:
if (x < y && x > z) raise OpError("Only one predicate may evaluate to True");
if (x < y) return 17;
else if (x > z) return 23;
else return -1;
表達式:
def f1():return tf.constant(17)
def f2():return tf.constant(23)
def f3():return tf.constant(-1)
r = tf.case({tf.less(x, y):f1, tf.greater(x, z):f2},
default=f3, exclusive=True)
eager模式兼容性
當 exclusive=False
時,Eager 模式不支持無序字典。改為使用元組列表。
相關用法
- Python tf.compat.v1.confusion_matrix用法及代碼示例
- Python tf.compat.v1.convert_to_tensor用法及代碼示例
- Python tf.compat.v1.count_nonzero用法及代碼示例
- Python tf.compat.v1.cond用法及代碼示例
- Python tf.compat.v1.constant用法及代碼示例
- 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.case。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。