Friday, October 24, 2025
HomeLanguagesPython – tensorflow.cond()

Python – tensorflow.cond()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.

cond() return true_fn() if the predicate pred is true otherwise it returns false_fn().

Syntax: tensorflow.cond( pred, true_fn, false_fn, name )

Parameters:

  • pred: It is a scalar which determines the callable to return
  • true_fn(optional): It is returned when pred is true.
  • false_fn(optional): It is returned when pred is false.
  • name(optional): It defines the name for the operation.

Return: It returns the result evaluated by callable.

Example 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)


Output:

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

Example 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)


Output:

x:  5
y:  10
Result:  tf.Tensor(100, shape=(), dtype=int32)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS