In [1]:
import tensorflow as tf
import matplotlib.pyplot as plt
a1 = tf.constant([10.0,12.0,14.0,6.0,8.0,15.0,18.0],name ='x')
b1 = tf.constant([7.0,11.0,11.0,4.0,7.0,13.0,19.0],name='y')
training_epoch = 500
learning_rate = 0.005
stp=50
t1=0.0
t0=0.0
h0=tf.divide(1.0,tf.add(1.0,tf.exp(-tf.add(t0,tf.reduce_sum(tf.multiply(t1,a1))))))
model=tf.global_variables_initializer()
sess = tf.Session()
print("h0 values:",sess.run(h0))
c=tf.cond(h0<0.5,lambda:0.00,lambda:1.00)
print(sess.run(c))
#cost function
v=tf.multiply(b1,tf.log(c))
n=tf.subtract(1.0,b1)
m=tf.log(tf.subtract(1.0,c))
m1=tf.multiply(n,m)
m2=tf.reduce_sum(tf.subtract(-v,m1))
print("Cost function:",sess.run(m2))
sess = tf.Session()
for epoch in range(training_epoch):
#if error is greater than threshold value then stop the iteration
s1=tf.reduce_sum(tf.multiply(tf.subtract(tf.add(tf.multiply(a1,t1),t0),b1),a1))
s0=tf.reduce_sum(tf.subtract(tf.add(tf.multiply(a1,t1),t0),b1))
tn1 = tf.subtract(t1,tf.multiply(learning_rate,tf.divide(s1,tf.cast(tf.size(a1),tf.float32))))
tn0= tf.subtract(t0,tf.multiply(learning_rate,tf.divide(s0,tf.cast(tf.size(a1),tf.float32))))
t1=tn1
t0=tn0
model=tf.global_variables_initializer()
if (epoch%stp==0):
print("W : ",sess.run(tn1)," b : ",sess.run(tn0))
print("After 500 epoch values:")
print("W : ",sess.run(tn1))
print("b : ",sess.run(tn0))
In [ ]: