In [1]:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
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)
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.
In [3]:
g = tf.get_default_graph()
print(g)
In [4]:
[op.name for op in g.get_operations()]
Out[4]:
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]:
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]:
In [7]:
computedT = sess.run(t)
print(computedT)
print(t)
sess.close()
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)
In [8]:
g1 = tf.Graph()
sess = tf.Session(graph=g1)
sess.close()
In [9]:
sess = tf.InteractiveSession()
Running the operation without specifying the session for the evaluation.
In [10]:
t.eval()
Out[10]: