XNOR_Gate



In [109]:
import tensorflow as tf
import numpy as np

In [110]:
x=tf.placeholder(tf.float32,shape=[None,2])
y=tf.placeholder(tf.float32,shape=[None,1])

In [111]:
weights=tf.Variable(tf.random_normal([2,1]),dtype=tf.float32)
bias=tf.Variable(tf.random_normal([1]),dtype=tf.float32)

In [112]:
multiply1=tf.add(tf.matmul(x,weights),bias)
z=tf.nn.sigmoid(multiply1)

In [113]:
cost=tf.reduce_mean((y*tf.log(z)+(1-y)*tf.log(1-z))*-1)
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

In [115]:
inp=np.array([[0,0],[0,1],[1,0],[1,1]])
op=np.array([[0],[0],[0],[1]])
#print(inp)

In [116]:
inp1=np.array([[0,0],[0,1],[1,0],[1,1]])
op1=np.array([[0],[1],[1],[1]])

In [117]:
with tf.Session() as sess:
    
   
    tf.global_variables_initializer().run()
    for i in range(12001):
        res,_=sess.run([cost,optimizer],feed_dict={x:inp,y:op})
        
        if i%1000==0:
            print ("iteration= ",i,"cost= ",res)
           
    print ("Validating output for AND GATE")
    andresult=sess.run(z,feed_dict={x:inp})
    print (andresult)
        
    print ("...................................................")
    for i in range(12001):
        res1,_=sess.run([cost,optimizer],feed_dict={x:inp1,y:op1})
        
        if i%1000==0:
            print ("iteration= ",i,"cost= ",res1)
           
    print ("Validating output for OR GATE")
    orresult=sess.run(z,feed_dict={x:inp1})
    print (orresult)


iteration=  0 cost=  0.74168
iteration=  1000 cost=  0.507207
iteration=  2000 cost=  0.388883
iteration=  3000 cost=  0.318518
iteration=  4000 cost=  0.271422
iteration=  5000 cost=  0.237252
iteration=  6000 cost=  0.211077
iteration=  7000 cost=  0.190249
iteration=  8000 cost=  0.173212
iteration=  9000 cost=  0.158979
iteration=  10000 cost=  0.14689
iteration=  11000 cost=  0.136486
iteration=  12000 cost=  0.127431
Validating output for AND GATE
[[ 0.00577951]
 [ 0.13512336]
 [ 0.13525444]
 [ 0.80782884]]
...................................................
iteration=  0 cost=  1.05534
iteration=  1000 cost=  0.116811
iteration=  2000 cost=  0.0688706
iteration=  3000 cost=  0.0577891
iteration=  4000 cost=  0.052865
iteration=  5000 cost=  0.0496318
iteration=  6000 cost=  0.0470474
iteration=  7000 cost=  0.0448044
iteration=  8000 cost=  0.0427915
iteration=  9000 cost=  0.0409581
iteration=  10000 cost=  0.0392754
iteration=  11000 cost=  0.0377237
iteration=  12000 cost=  0.0362877
Validating output for OR GATE
[[ 0.07812656]
 [ 0.96864486]
 [ 0.96865016]
 [ 0.99991119]]

In [118]:
x1=tf.placeholder(tf.float32,shape=[None,1])
y1=tf.placeholder(tf.float32,shape=[None,1])

weights=tf.Variable(tf.random_normal([1,1]),dtype=tf.float32)
bias=tf.Variable(tf.random_normal([1]),dtype=tf.float32)



multiply1=tf.add(tf.matmul(x1,weights),bias)
z=tf.nn.sigmoid(multiply1)


cost=tf.reduce_mean((y1*tf.log(z)+(1-y1)*tf.log(1-z))*-1)
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

In [121]:
i=0
inp3=np.array([[0],[0],[0],[0]])
op3=np.array([[0],[0],[0],[0]])

#inp3[0]=4;
#print(orresult) 
for i in range (4):
    if(orresult[i]<0.5):
        inp3[i]=0
        op3[i]=1
    else:
        inp3[i]=1
        op3[i]=0
print("input  for nor")    
print(inp3)
print("ouput  for nor")    
print(op3)


#print(orresult)


input  for nor
[[0]
 [1]
 [1]
 [1]]
ouput  for nor
[[1]
 [0]
 [0]
 [0]]

In [122]:
with tf.Session() as sess:
    
   
    tf.global_variables_initializer().run()
    for i in range(12001):
        res3,_=sess.run([cost,optimizer],feed_dict={x1:inp3,y1:op3})
        
        if i%1000==0:
            print ("iteration= ",i,"cost= ",res3)
           
    print ("Validating output for NOR GATE")
    norresult=sess.run(z,feed_dict={x1:inp3})
    print (norresult)


iteration=  0 cost=  0.637177
iteration=  1000 cost=  0.257041
iteration=  2000 cost=  0.189845
iteration=  3000 cost=  0.147848
iteration=  4000 cost=  0.119681
iteration=  5000 cost=  0.0998441
iteration=  6000 cost=  0.0852809
iteration=  7000 cost=  0.0742147
iteration=  8000 cost=  0.0655624
iteration=  9000 cost=  0.0586349
iteration=  10000 cost=  0.052977
iteration=  11000 cost=  0.0482775
iteration=  12000 cost=  0.0443173
Validating output for NOR GATE
[[ 0.89683402]
 [ 0.02253229]
 [ 0.02253229]
 [ 0.02253229]]

In [123]:
x=tf.placeholder(tf.float32,shape=[None,2])
y=tf.placeholder(tf.float32,shape=[None,1])

weights=tf.Variable(tf.random_normal([2,1]),dtype=tf.float32)
bias=tf.Variable(tf.random_normal([1]),dtype=tf.float32)

multiply1=tf.add(tf.matmul(x,weights),bias)
z=tf.nn.sigmoid(multiply1)

cost=tf.reduce_mean((y*tf.log(z)+(1-y)*tf.log(1-z))*-1)
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

In [124]:
i=0
inp4=np.array([[0,0],[0,0],[0,0],[0,0]])
#op4=np.array([[0],[0],[0],[0]])
inp4[3,0]=8
#print(inp4)
#print(norresult)
for i in range (4):
    #print(norresult[i])
    if(norresult[i] <  0.5):
        inp4[i,0]=0
    else:
        inp4[i,0]=1
    if(andresult[i] < 0.5):
        inp4[i,1]=0
    else:
        inp4[i,1]=1        
        #op3[i]=0
#print(inp4)
#print(andresult)
op4 = np.array([[1],[0],[0],[1]])
print("Output of Xnor")
print(op4)


Output of Xnor
[[1]
 [0]
 [0]
 [1]]

In [126]:
with tf.Session() as sess:
    
   
    tf.global_variables_initializer().run()
    for i in range(12001):
        res4,_=sess.run([cost,optimizer],feed_dict={x:inp4,y:op4})
        
        if i%1000==0:
            print ("iteration= ",i,"cost= ",res4)
           
    print ("Validating output for XNOR GATE")
    xnoresult=sess.run(z,feed_dict={x:inp4})
    print (xnoresult)


iteration=  0 cost=  0.637078
iteration=  1000 cost=  0.417861
iteration=  2000 cost=  0.310198
iteration=  3000 cost=  0.244028
iteration=  4000 cost=  0.199773
iteration=  5000 cost=  0.168345
iteration=  6000 cost=  0.145008
iteration=  7000 cost=  0.127072
iteration=  8000 cost=  0.112902
iteration=  9000 cost=  0.101453
iteration=  10000 cost=  0.0920282
iteration=  11000 cost=  0.0841462
iteration=  12000 cost=  0.0774646
Validating output for XNOR GATE
[[ 0.93541968]
 [ 0.08285387]
 [ 0.08285387]
 [ 0.93230373]]