In [1]:
import tensorflow as tf

x=tf.constant([1.5, 2.8, 3.9, 4.1, 5.0, 6])
y=tf.constant([1.2, 1.1, 3.4, 8.5, 4, 3.1])

with tf.Session() as sess:
    for i in range(len(sess.run(x))):
        output = sess.run(x[i])
        print(output)
   

with tf.Session() as sess1:
    for i in range(len(sess1.run(y))):
        output1 = sess1.run(y[i])
        print(output1)


1.5
2.8
3.9
4.1
5.0
6.0
1.2
1.1
3.4
8.5
4.0
3.1

In [2]:
sum1=tf.constant(0)
with tf.Session() as sess:
    for i in range(len(sess.run(x))):
        output =sess.run(x[i])
        sum1=tf.add(sum1,output)
    a=tf.divide(sum1,len(sess.run(x)))
    avgx=sess.run(a)
print(avgx)


sum1=tf.constant(0)
with tf.Session() as sess:
    for i in range(len(sess.run(y))):
        output =sess.run(y[i])
        sum1=tf.add(sum1,output)
    a=tf.divide(sum1,len(sess.run(y)))
    avgy=sess.run(a)
print(avgy)


3.5
3.33333333333

In [3]:
#Calculate variance for x
var=tf.constant(0.0)
with tf.Session() as sess:
    for i in range(len(sess.run(x))):
        a=tf.subtract(sess.run(x[i]),avgx)
        b=tf.square(a)
        var=tf.add(var,b)
    vari=sess.run(var)
print(vari)


13.51

In [4]:
#Calculate covariance of x & y
cov=tf.constant(0.0)
with tf.Session() as sess:
    for i in range(len(sess.run(x))):
        a=tf.subtract(sess.run(x[i]),avgx)
        b=tf.subtract(sess.run(y[i]),avgy)
        c=tf.multiply(a,b)
        cov=tf.add(cov,c)
    covar=sess.run(cov)
print(covar)


9.37333

In [5]:
#Calculate value of c

temp=tf.divide(cov,var)

with tf.Session() as sess:
    const_c=sess.run(temp)
    print(const_c)


0.693807

In [6]:
#Calculate value of m

c=tf.to_float(const_c)
d=tf.to_float(avgx)
e=tf.multiply(c,d)
f=tf.to_float(avgy)
res=tf.subtract(f,e)
with tf.Session() as sess:
    res=sess.run(f)
print('The value of m')
print(res)


The value of m
3.33333

In [7]:
#with scope 2
import tensorflow as tf
v1=tf.constant(4)
v2=tf.constant(2)
with tf.name_scope("MyOperationGroup"):
    with tf.name_scope("Scope_A"):
        a = tf.multiply(v1,v1)
        b = tf.multiply(v2,v2)  
with tf.name_scope("Scope_B"):
    c = tf.add(a, b, name="And_These_ones")
    d = tf.multiply(v1, v2, name="Multiply_these_numbers")

with tf.name_scope("Scope_C"):
    e=tf.subtract(c,d, name="subtrating")
    #e = tf.multiply(2, d, name="B_add")
    f=tf.add(v1,v2)
   
with tf.name_scope("Scope_D"):
    g = tf.multiply(f,e)


with tf.Session() as sess:
    writer = tf.summary.FileWriter("/tmp/tboard/output_la", sess.graph)
    print(sess.run(g))
    writer.close()


72

In [8]:
#with scope 1
import tensorflow as tf
v1=tf.constant(4)
v2=tf.constant(2)
with tf.name_scope("MyOperationGroup"):
    with tf.name_scope("Scope_A"):
        a = tf.multiply(v1,v1)
        b = tf.multiply(v2,v2)
with tf.name_scope("Scope_B"):
    c = tf.add(a, b, name="And_These_ones")
    d = tf.multiply(v1, v2, name="Multiply_these_numbers")

with tf.name_scope("Scope_C"):
    e = tf.multiply(2, d, name="B_add")

g = tf.subtract(c,e)



with tf.Session() as sess:
    writer = tf.summary.FileWriter("/tmp/tboard/output3", sess.graph)
    print(sess.run(g))
    writer.close()


4

In [ ]: