📜  Python – tensorflow.cond()

📅  最后修改于: 2022-05-13 01:55:26.172000             🧑  作者: Mango

Python – tensorflow.cond()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

cond( ) 如果谓词 pred 为真,则返回 true_fn(),否则返回 false_fn()。

示例 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)


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)