In [1]:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

Generating data

Numpy vs Tensorflow

Numpy Tensorflow
shape = .shape shape = .get_shape(), values are computed and sent over
output output has to be computed as the expression is stored as an TF operation

In [2]:
n = np.linspace(-5,5,100)
print(n.shape)
print(n.dtype)

t = tf.linspace(-5.0,5.0,100)

print("_____________________________________________________________________")
print("Printing the output for n and x")
print(t)
print(n)


(100,)
float64
_____________________________________________________________________
Printing the output for n and x
Tensor("LinSpace:0", shape=(100,), dtype=float32)
[-5.         -4.8989899  -4.7979798  -4.6969697  -4.5959596  -4.49494949
 -4.39393939 -4.29292929 -4.19191919 -4.09090909 -3.98989899 -3.88888889
 -3.78787879 -3.68686869 -3.58585859 -3.48484848 -3.38383838 -3.28282828
 -3.18181818 -3.08080808 -2.97979798 -2.87878788 -2.77777778 -2.67676768
 -2.57575758 -2.47474747 -2.37373737 -2.27272727 -2.17171717 -2.07070707
 -1.96969697 -1.86868687 -1.76767677 -1.66666667 -1.56565657 -1.46464646
 -1.36363636 -1.26262626 -1.16161616 -1.06060606 -0.95959596 -0.85858586
 -0.75757576 -0.65656566 -0.55555556 -0.45454545 -0.35353535 -0.25252525
 -0.15151515 -0.05050505  0.05050505  0.15151515  0.25252525  0.35353535
  0.45454545  0.55555556  0.65656566  0.75757576  0.85858586  0.95959596
  1.06060606  1.16161616  1.26262626  1.36363636  1.46464646  1.56565657
  1.66666667  1.76767677  1.86868687  1.96969697  2.07070707  2.17171717
  2.27272727  2.37373737  2.47474747  2.57575758  2.67676768  2.77777778
  2.87878788  2.97979798  3.08080808  3.18181818  3.28282828  3.38383838
  3.48484848  3.58585859  3.68686869  3.78787879  3.88888889  3.98989899
  4.09090909  4.19191919  4.29292929  4.39393939  4.49494949  4.5959596
  4.6969697   4.7979798   4.8989899   5.        ]

As we can see, there is no output for the tensorflow operation here.

Numpy and Tensorflow being almost the same we can see that tensorflow plays the game a little differently.

What tensorflow does is, it takes the given expression and stores it as a TF operation which is executed only when the tensor is in a session.

Default Graphs


In [3]:
g = tf.get_default_graph()
print(g)


<tensorflow.python.framework.ops.Graph object at 0x0000028844E9E5C0>

In [4]:
[op.name for op in g.get_operations()]


Out[4]:
['LinSpace/start', 'LinSpace/stop', 'LinSpace/num', 'LinSpace']

Now we return all the operations which have been added to the graph.

Each of these operations is a Tensor in itself which performs a particular tast.

The last operation takes in the first three operations. We can request the output of any operation which is a tensor, by asking the graph for the Tensor's name.


In [5]:
g.get_tensor_by_name("LinSpace:0")


Out[5]:
<tf.Tensor 'LinSpace:0' shape=(100,) dtype=float32>

As we can see here, the result of the TF operation is a TF Tensor.

In order to compute anything in TensorFlow we need to create a TF session. The session is responsible for evaluating the TF graph.


In [6]:
sess = tf.Session()
sess


Out[6]:
<tensorflow.python.client.session.Session at 0x2884723b470>

In [7]:
computedT = sess.run(t)
print(computedT)
print(t)
sess.close()


[-5.         -4.89898968 -4.79797983 -4.69696951 -4.59595966 -4.49494934
 -4.3939395  -4.29292917 -4.19191933 -4.090909   -3.98989916 -3.88888884
 -3.78787875 -3.68686867 -3.58585858 -3.4848485  -3.38383842 -3.28282833
 -3.18181825 -3.08080816 -2.97979808 -2.87878799 -2.77777791 -2.67676783
 -2.5757575  -2.47474742 -2.37373734 -2.27272725 -2.17171717 -2.07070708
 -1.969697   -1.86868691 -1.76767683 -1.66666675 -1.56565666 -1.46464658
 -1.36363649 -1.26262641 -1.16161633 -1.06060624 -0.95959616 -0.85858583
 -0.75757599 -0.65656567 -0.55555582 -0.4545455  -0.35353565 -0.25252533
 -0.15151501 -0.05050516  0.05050516  0.15151501  0.25252533  0.35353518
  0.4545455   0.55555534  0.65656567  0.75757551  0.85858583  0.95959568
  1.060606    1.16161585  1.26262617  1.36363602  1.46464634  1.56565666
  1.66666651  1.76767683  1.86868668  1.969697    2.07070684  2.17171717
  2.27272701  2.37373734  2.47474718  2.5757575   2.67676735  2.77777767
  2.87878752  2.97979784  3.08080769  3.18181801  3.28282833  3.38383865
  3.48484802  3.58585835  3.68686867  3.78787899  3.88888836  3.98989868
  4.090909    4.19191933  4.2929287   4.39393902  4.49494934  4.59595966
  4.69696999  4.79797935  4.89898968  5.        ]
Tensor("LinSpace:0", shape=(100,), dtype=float32)

By default TensorFlow gets the default graph to run in the session.

sess = tf.Session(graph = tf.get_default_graph())

But one can change this by selecting which graph they want to import by saying.

sess = tf.Session(graph = g)

Create Graphs


In [8]:
g1 = tf.Graph()
sess = tf.Session(graph=g1)
sess.close()

Creating an Interactive Session


In [9]:
sess = tf.InteractiveSession()

Running the operation without specifying the session for the evaluation.


In [10]:
t.eval()


Out[10]:
array([-5.        , -4.89898968, -4.79797983, -4.69696951, -4.59595966,
       -4.49494934, -4.3939395 , -4.29292917, -4.19191933, -4.090909  ,
       -3.98989916, -3.88888884, -3.78787875, -3.68686867, -3.58585858,
       -3.4848485 , -3.38383842, -3.28282833, -3.18181825, -3.08080816,
       -2.97979808, -2.87878799, -2.77777791, -2.67676783, -2.5757575 ,
       -2.47474742, -2.37373734, -2.27272725, -2.17171717, -2.07070708,
       -1.969697  , -1.86868691, -1.76767683, -1.66666675, -1.56565666,
       -1.46464658, -1.36363649, -1.26262641, -1.16161633, -1.06060624,
       -0.95959616, -0.85858583, -0.75757599, -0.65656567, -0.55555582,
       -0.4545455 , -0.35353565, -0.25252533, -0.15151501, -0.05050516,
        0.05050516,  0.15151501,  0.25252533,  0.35353518,  0.4545455 ,
        0.55555534,  0.65656567,  0.75757551,  0.85858583,  0.95959568,
        1.060606  ,  1.16161585,  1.26262617,  1.36363602,  1.46464634,
        1.56565666,  1.66666651,  1.76767683,  1.86868668,  1.969697  ,
        2.07070684,  2.17171717,  2.27272701,  2.37373734,  2.47474718,
        2.5757575 ,  2.67676735,  2.77777767,  2.87878752,  2.97979784,
        3.08080769,  3.18181801,  3.28282833,  3.38383865,  3.48484802,
        3.58585835,  3.68686867,  3.78787899,  3.88888836,  3.98989868,
        4.090909  ,  4.19191933,  4.2929287 ,  4.39393902,  4.49494934,
        4.59595966,  4.69696999,  4.79797935,  4.89898968,  5.        ], dtype=float32)