Logical Gates using TensorFlow – OR, AND, NOR, XOR, XNOR
Last Updated :
23 Jan, 2023
TensorFlow provides mathematical operations and logical gates with tensorflow.math module. As input, it requires a tensor. For boolean operations, we can use boolean values True, and False in Tensor.
Parameters:
A: A Tensor of boolean value.
B: A Tensor of boolean value.
name (optional): The name for the operation.
For OR GATE
Syntax: tensorflow.logical_or(A, B, name=None) or tensorflow.math.logical_or(A, B, name=None)
For AND GATE
Syntax: tensorflow.logical_and(A, B, name=None) or tensorflow.math.logical_and(A, B, name=None)
For NOT GATE
Syntax: tensorflow.logical_not(A, name=None) or tensorflow.math.logical_not(A, name=None)
For XOR GATE
Syntax: tensorflow.math.logical_xor(A, B, name=None)
OR Gate using TensorFlow:
In OR Gate, If one or both the inputs are High(True) then the output will be high(True) else low(False).
OR Gate
A
|
B
|
Z = A + B
|
False
|
False
|
False
|
False
|
True
|
True
|
True
|
False
|
True
|
True
|
True
|
True
|
Python3
import tensorflow as tf
A = tf.constant([ False , False , True , True ], dtype = tf. bool )
B = tf.constant([ False , True , False , True ], dtype = tf. bool )
Z = tf.math.logical_or(A, B)
print (Z)
|
Output:
tf.Tensor([False True True True], shape=(4,), dtype=bool)
Here, we have used tf.math.logical_or() function to implement the logical OR gate. It takes A and B tensors as input and produces output tensor Z.
AND Gate using TensorFlow:
In AND Gate, If both the inputs are High(True) then the output will be high(True) else low(False).
AND Gate
A
|
B
|
Z = A.B
|
False
|
False
|
False
|
False
|
True
|
False
|
True
|
False
|
False
|
True
|
True
|
True
|
Python3
import tensorflow as tf
A = tf.constant([ False , False , True , True ], dtype = tf. bool )
B = tf.constant([ False , True , False , True ], dtype = tf. bool )
Z = tf.math.logical_and(A, B)
print (Z)
|
Output:
tf.Tensor([False False False True], shape=(4,), dtype=bool)
Here, we have used tf.math.logical_and() function to implement the logical AND gate. It takes A and B tensors as input and produces output tensor Z.
NOT Gate using TensorFlow:
In NOT Gate, The output will be opposite to the input value. i.e If the input is High then the output will be Low and vice versa.
NOT Gate
A
|
Z = NOT A
|
False
|
True
|
True
|
False
|
Python3
import tensorflow as tf
A = tf.constant([ False , True ], dtype = tf. bool )
Z = tf.math.logical_not(A)
print (Z)
|
Output:
tf.Tensor([ True False], shape=(2,), dtype=bool)
Here, we have used tf.math.logical_not() function to implement the logical NOT gate. It takes A tensor as input and produces output tensor Z.
XOR Gate using TensorFlow:
In XOR Gate, If any one of the inputs is high then the output will be high and if both the inputs are high then the output will be low.
XOR Gate
X
|
Y
|
Z = A XOR B
|
False
|
False
|
False
|
False
|
True
|
True
|
True
|
False
|
True
|
True
|
True
|
False
|
Python3
import tensorflow as tf
A = tf.constant([ False , False , True , True ], dtype = tf. bool )
B = tf.constant([ False , True , False , True ], dtype = tf. bool )
Z = tf.math.logical_xor(A, B)
print (Z)
|
Output:
tf.Tensor([False True True False], shape=(4,), dtype=bool)
Here, we have used tf.math.logical_xor() function to implement the logical XOR gate. It takes A and B tensors as input and produces output tensor Z.
NOR Gate using TensorFlow:
In NOR Gate, It provides high output only if all the inputs are low otherwise low output.
Tensorflow does not have a pre-defined logical function for the NOR gate. Therefore, we will be using the logical OR function and logical NOT function in combination to construct a NOR gate.
NOR Gate
A
|
B
|
Z= A+B
|
C = NOT Z
|
NOR GATE = C
|
False
|
False
|
False
|
True
|
True
|
False
|
True
|
True
|
False
|
False
|
True
|
False
|
True
|
False
|
False
|
True
|
True
|
True
|
False
|
False
|
Python3
import tensorflow as tf
A = tf.constant([ False , False , True , True ], dtype = tf. bool )
B = tf.constant([ False , True , False , True ], dtype = tf. bool )
Z = tf.math.logical_or(A, B)
C = tf.math.logical_not(Z)
print (C)
|
Output:
tf.Tensor([ True False False False], shape=(4,), dtype=bool)
Here, we have used tf.math.logical_or() function to implement the logical OR gate. It takes A and B tensors as input and produces output tensor Z. and the we have used tf.math.logical_not() function to implement the logical NOT gate. It takes output tensor of OR gate as input and produces output tensor C. This combined result is output of NOR Gate
XNOR Gate using TensorFlow:
In XNOR Gate, The output will be high only if both the inputs will be the same i.e either High or Low.
Similar to the NOR gate, Tensorflow does not have a function for the XNOR gate. Thus, we will combine logical XOR and logical NOT functions to construct the XNOR gate.
XNOR Gate
A
|
B
|
Z = A XOR B
|
Y = NOT Z
|
XNOR GATE = Y
|
False
|
False
|
False
|
True
|
True
|
False
|
True
|
True
|
False
|
False
|
True
|
False
|
True
|
False
|
False
|
True
|
True
|
False
|
True
|
True
|
Python3
import tensorflow as tf
A = tf.constant([ False , False , True , True ], dtype = tf. bool )
B = tf.constant([ False , True , False , True ], dtype = tf. bool )
Z = tf.math.logical_xor(A, B)
Y = tf.math.logical_not(Z)
print (Y)
|
Output:
tf.Tensor([ True False False True], shape=(4,), dtype=bool)
Here, we have used tf.math.logical_xor() function to implement the logical OR gate. It takes A and B tensors as input and produces output tensor Z. and we have used tf.math.logical_not() function to implement the logical NOT gate. It takes the output tensor of XOR gate as input and produces the output tensor Y. This combined result is the output of XNOR Gate.
To learn more about Logic Gate. Click here
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...