Wednesday, July 3, 2024
HomeLanguagesPythonPython | Tensorflow logical_xor() method

Python | Tensorflow logical_xor() method

Tensorflow is an open-source machine learning library developed by Google. One of its applications is to develop deep neural networks.

The module tensorflow.math provides support for many basic logical operations. Function tf.logical_xor() [alias tf.math.logical_xor] provides support for the logical XOR function in Tensorflow. It expects the inputs of bool type. The input types are tensor and if the tensors contains more than one element, an element-wise logical XOR is computed,  $x XOR y = (x \| \, y) \, \&\& \, ^^21(x \&\& y)$ .

Syntax: tf.logical_xor(x, y, name=None) or tf.math.logical_xor(x, y, name=None)

Parameters:
x: A Tensor of type bool.
y: A Tensor of type bool.
name (optional): The name for the operation.

Return type: A Tensor of bool type with the same size as that of x or y.

Code:




# Importing the Tensorflow library
import tensorflow as tf
  
# A constant vector of size 4
a = tf.constant([True, False, True, False], dtype = tf.bool)
b = tf.constant([True, False, False, True], dtype = tf.bool)
  
# Applying the XOR function and
# storing the result in 'c'
c = tf.logical_xor(a, b, name ='logical_xor')
  
# Initiating a Tensorflow session
with tf.Session() as sess:
    print('Input type:', a)
    print('Input a:', sess.run(a))
    print('Input b:', sess.run(b))
    print('Return type:', c)
    print('Output:', sess.run(c))


Output:

Input type: Tensor("Const:0", shape=(4, ), dtype=bool)
Input a: [ True False  True False]
Input b: [ True False False  True]
Return type: Tensor("logical_xor:0", shape=(4, ), dtype=bool)
Output: [False False  True  True]

 

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments