In [1]:
import tensorflow as tf
a = tf.constant([15.0,16.0,17.0,12.0,11.0,10.0,9.0],name ='x')
b = tf.constant([7.0,1.0,2.0,5.0,4.0,9.0,6.0],name='y')
training_epoch = 500
learning_rate = 0.005
t1=0.0
t0=0.0
##---------------------------finding the value of h0------------
h0=tf.divide(1.0,tf.add(1.0,tf.exp(-tf.add(t0,tf.reduce_sum(tf.multiply(t1,a))))))
model=tf.global_variables_initializer()
sess = tf.Session()
print("h0 values:",sess.run(h0))
result=tf.cond(h0<0.5,lambda:0.00,lambda:1.00)
print("after rounding off, h0 value:")
print(sess.run(result))
############################################################
##-----------------------cost function-----------
c1 = tf.multiply(tf.subtract(1.0,b),tf.log(tf.subtract(1.0,result)))
c2 = tf.multiply(-b,tf.log(result))
cost = tf.reduce_sum(tf.subtract(c2,c1))
print("cost function:",sess.run(cost))
####----------------finding the value of theta 1 and theta 0-----------
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(a,t1),t0),b),a))
s0=tf.reduce_sum(tf.subtract(tf.add(tf.multiply(a,t1),t0),b))
tn1 = tf.subtract(t1,tf.multiply(learning_rate,tf.divide(s1,tf.cast(tf.size(a),tf.float32))))
tn0= tf.subtract(t0,tf.multiply(learning_rate,tf.divide(s0,tf.cast(tf.size(a),tf.float32))))
t1=tn1
t0=tn0
model=tf.global_variables_initializer()
sess = tf.Session()
print("After 500 epoch values:")
print("theta 1 : ",sess.run(tn1))
print("theta 0 : ",sess.run(tn0))
In [ ]: