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


Python tensorflow.cond()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

如果謂詞pred為true,則cond()返回true_fn(),否則返回false_fn()。

用法:tensorflow.cond( pred, true_fn, false_fn, name )

參數:

  • pred:它是一個標量,確定可調用的返回
  • true_fn(可選):當pred為true時返回。
  • false_fn(可選):如果pred為假,則返回該值。
  • name(optional):它定義了操作的名稱。

返回:它返回由callable評估的結果。



範例1:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input 
x = 5
y = 10
  
  
# Printing the input 
print('x:', x) 
print('y:', y) 
  
# Calculating result 
res = tf.cond(x < y, lambda:tf.add(x, y), lambda:tf.square(y)) 
  
# Printing the result 
print('Result:', res)

輸出:

x: 5
y: 10
Result: tf.Tensor(15, shape=(), dtype=int32)

範例2:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input 
x = 5
y = 10
  
  
# Printing the input 
print('x:', x) 
print('y:', y) 
  
# Calculating result 
res = tf.cond(x > y, lambda:tf.add(x, y), lambda:tf.square(y)) 
  
# Printing the result 
print('Result:', res)

輸出:

x: 5
y: 10
Result: tf.Tensor(100, shape=(), dtype=int32)



相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.cond()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。