In [ ]:
# Write a program using tensorflow to calculate : 
    $$y=mx+c$$

Part 1

  1. Read 2 arrays x,y containing floating point values
  2. Calculate mean of x & y
  3. Calculate variance for x $$variance(x)=sum((x-mean(x))^2)$$
  4. Calculate covariance of x & y $$covariance = sum((x(i) - mean(x)) * (y(i) - mean(y)))$$
  5. Calculate value of c $$c = covariance(x,y)/variance(x)$$
  6. Calculate value of m $$m = mean(y) -c* mean(x)$$

In [68]:
import tensorflow as tf

with tf.name_scope("var"):
    with tf.name_scope("mean_x"):
        a=tf.constant([5.0,7.0,20.2,17.32],shape=[1,4],name='a')
        b=tf.constant([7.0,9.0,19.0,18.0],shape=[1,4],name='b')
        x=tf.reduce_mean(a)
        sess=tf.Session()
print("mean",sess.run(x))


#mean x


mean 12.38

In [69]:
#mean of y

with tf.name_scope("mean_y"):
    y=tf.reduce_mean(b)
    sess=tf.Session()
print("mean",sess.run(y))


mean 13.25

In [70]:
#variance of x
d=tf.subtract(a,x)
sess=tf.Session()
print(sess.run(d ))

e=tf.square(d)
f=tf.reduce_sum(e)
sess=tf.Session()
print(sess.run(f))


[[-7.38000011 -5.38000011  7.82000065  4.93999958]]
168.965

In [71]:
#covariance
with tf.name_scope("covariance"):
    g=tf.subtract(b,y)
    sess=tf.Session()
    g=tf.multiply(d,g)
    h=tf.reduce_sum(g)
print(sess.run(h))


137.42

In [72]:
#value of c
with tf.name_scope("value_of_c"):
    j=tf.divide(h,f)
print(sess.run(j))


0.813305

In [73]:
#m value
with tf.name_scope("value_m"):
    i=tf.multiply(j,x)
    k=tf.subtract(y,i)
print(sess.run(j))


0.813305

Part 2

  1. Plot graph for actual values against predicted value
  2. Calculate root mean square error.

In [76]:
with tf.Session() as lb:
    Writer =tf.summary.FileWriter("/tmp/tboard/output3",lb.graph)
    Writer.close()

In [ ]:


In [ ]: