OR Model Implementation using tensorflow Library in Machine Learning.

What does OR Gate mean?


An OR gate is a logic gate that produces inclusive disjunction. The function of an OR gate is to find the maximum between the inputs which are binary in nature. It is one of the basic gates used in Boolean algebra and electronic circuits like transistor-transistor logic, and complementary metal-oxide semiconductors make use of it.

Here we are using a machine learning approach to find it's output.

Program:-

import matplotlib.pyplot as plt
import tensorflow as tf
w1=tf.Variable([.1],tf.float32)
w2=tf.Variable([-.1],tf.float32)
x1=tf.placeholder(tf.float32)
x2=tf.placeholder(tf.float32)
or_model=w1*x1+w2*x2
y=tf.placeholder(tf.float32)
sq_delta=tf.square(or_model-y)
loss=tf.reduce_sum(sq_delta)
init=tf.global_variables_initializer()
optimizer=tf.train.GradientDescentOptimizer(0.01)
train =optimizer.minimize(loss)
sess=tf.Session()
sess.run(init)
for i in range(1000):
    sess.run(train,{x1:[0,1,0,1],x2:[0,0,1,1],y:[0,1,1,1]})
l=sess.run(or_model,{x1:[0,1,0,1],x2:[0,0,1,1]})
print(w1.value)
print(w2.value)
print(l)
l1=[]
for i in l:
    if(i>0.5):
        l1.extend([1])
    else:
        l1.extend([0])
print(l1)
Output:


Comments