Installing TensorFlow
Visit the offical TensorFlow webpage for installation guide
Simple installation steps are:
- Go to Command Prompt
- Execute the the command:
pip3 install --upgrade tensorflow
(Right now we are not going into the complexity of installing Tersorflow with GPU Support. We will be using Tensorflow with CPU)
I am assuming that you have installed Jupyter Notebook already. Once Tensorflow is installed with the related dependencies, we will move to the next steps.
- Open Jupyter Notebook
- Import Tensorflow by executing
import tensorflow as tf
- Create symbolic variables called placeholders. We can manipulate them during program execution
a =tf.placeholder("float")
b=tf.placeholder("float")
- Now we will try to implement the below Mathematical Operations:
Operations | Description |
---|---|
tf.multiply | Multiply |
tf.add | Sum |
tf.subtract | Substraction |
tf.divide | Division |
tf.mod | Module |
tf.abs | Returns Absolute Value |
tf.negative | Returns Negative Value |
tf.sign | Reeturns Sign |
tf.reciprocal | Returns Reciprocal/Inverse |
tf.square | Returns Squared Value |
tf.round | Returns the Nearest Integer |
tf.sqrt | Returns the Square Root |
tf.pow | Calculates the power |
tf.exp | Calculates the Exponential Value |
tf.log | Calculate the Logarithm |
tf.maximum | Compares and Returns the Maximum value |
tf.minimum | Compares and Returns the Minimum value |
tf.cos | Calculates the cosine |
tf.sin | Calculates the Sine |
Example-
d= tf.add(a,b)
- Refer to the file Tensorflow_Basics.ipynb for the complete code
- Once you have executed the functions for mathematical operations, Tensors for the respective functions are created e.g.
Tensor("Add_7:0", dtype=float32)
- Now we have to create Session and display the final output by assigning values to the symbolic variables
Example-
sess = tf.Session()
print(sess.run(d, feed_dict={a: 3.5, b: 2.5})
Using TensorBoard with TensorFlow
TensorBoard is a graph visualisation software which is included by default while installing TensorFlow.
For this part we will use Tensorflow_n_Tensorboard_Basic.py where we are creating two constants a and b and assigining them value of 10 and 30 respectively.
We are performing a quick addition as we did in the first code.
Now we want to visualize it using TensorBroad. If we want to visualize using TensorBoard for a code with TensorFlow running in backend, we have to create log file where we export the operations. TensorBoard creates visualizations of the graph that we created and also shares certain runtime details of the same.
Uisng TensorBoard while working on Machine Learning or Deep Learning problems is immensey helpful as it makes it easier to understand.
In order to visualize the addition on TensorBoard, we have to add the below line to the code
writer = tf.summary.FileWriter([logdir], [graph])
logdir is the path where the log files for the event will be created and the graph is the one that we are using for our code. The graph can be either user defined or created by default. Inour case it is the default one. For the default graph, we will use,
tf.get_default_graph()
Now we have to open Terminal and go the folder path and execute the below code:
python Tensorflow_n_Tensorboard_Basic.py
Now to visualize using TensorBoard, we have to execute the below code from the same terminal:
tensorboard --logdir="./graphs" --port 6006
Once executed, we will get a link like this http:https://DIN19001082:6006. On opening the link, we will get to see the graphs as below:
This is how the graph looks. Now if we hover over the graphs and click on the elements of it we will get the details as below:
We can notice that thought we have assigned constants to a and b in the code, the TensorBoard shows them as Const and Const_1
In order to change them on the TensorBoard, we need to edit our code a bit while assigning the constants by:
a = tf.constant(2, name="a")
b = tf.constant(3, name="b")
c = tf.add(a, b, name="sum")
Now, this is how it looks like:
We will learn more about TensorBoard later
Constant Operations (Refer this file) :
-
Create constant:
tf.constant(value, dtype=None, shape=None, name="constant", verify_shape=False)
-
Constant of 1D Tensor i.e. Vector
a= tf.constant([10,20], name='Vector')
- Constant of 2X2 tensor i.e. Matrix
b= tf.constant([[10,20],[30,40]], name='Matrix')
-
Create Tensor with specific dimension and specific values:
tf.zeros([2,3], dtype=tf.int32, name=None)
-
Create a Tensor 2x3 i.e. Matrix with zero as all elements
c= tf.zeros([2,3], dtype=tf.float32, name="Zero")
- Create a tensor of shape and type (unless specified) as tensor_shape but all the elements are zeros
tensor_shape= [[0,1],[2,3],[3,4],[4,5]]
d=tf.zeros_like(tensor_shape)
- Create a tensor of any shape and all the elements are 1s:
tf.ones(shape, dtype=tf.float32, name=None)
e= tf.ones([3,5], dtype=tf.int32, name='Ones')
- Create a tensor of shape and type (unless specified) as tensor_shape but all the elements are 1s:
tf.ones_like(shape, name=None)
tensor_shape= [[0,1],[2,3],[3,4],[4,5]]
f=tf.ones_like(tensor_shape)
- Create a Tensor and fill it with any scalar value:
tf.fill(dims, value, name=None)
g= tf.fill([5,4], 999)
- Create Tensor with sequence of constants:
tf.lin_space(start, stop, num, name=None)
h= tf.lin_space(100.0, 200.0, 10, name="sequence")
- Create a Tensor sequence that increments by delta but does not include the limits:
tf.range([start], limit=None, delta=delta, dtype=None, name=None)
i= tf.range(10, limit=20, delta=1, dtype=tf.float32, name="seq1")
j= tf.range(50, limit=10, delta=-10, dtype=tf.float32, name="seq2")
limit=5
k= tf.range(limit)
- Generate Random constants from certain distributions:
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
- Returns a tensor of the specified shape filled with random normal values
l= tf.random_normal([2,3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1, name="norm_dist")
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
- Returns A tensor of the specified shape filled with random truncated normal values
m=tf.truncated_normal([3,4], mean=1.5, stddev=1.2, dtype=tf.float32, seed=123, name="trunc_norm")
tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)
- Returns A tensor of the specified shape filled with random uniform values
n= tf.random_uniform([5,5], minval=10, maxval=100, dtype=tf.float32, seed=123, name="rand_uni")
Mathematical Operations (Refer this file):
We have demonstrated few mathematical operations above. Here's few more:
- Computes Python style division of a by b
c= tf.divide(a,b)
- Returns a / b returns the quotient of a and b
d= tf.div(a,b)
- Returns a / b evaluated in floating point. Will return error if both have different data types
e= tf.truediv(a,b)
- Returns a / b rounded down (except possibly towards zero for negative integers). Returns error if inputs are complex
f= tf.floordiv(a,b)
- Returns a Tensor. Has the same type as a
g= tf.truncatediv(a,b)
- Returns a Tensor. Has the same type as a
h= tf.floor_div(a,b)
- Add n number of tensors mentioned as a list
k= tf.add_n([a,i,j])
- Dot Product(Refer this site to know about axes selection)
m= tf.tensordot(a,i,1)
Data Types (Refer this file):
In TensorFlow all the data are in the form of Tensors with Rank. If it is a scalar value, it is known as a Tensor of Rank 0. If it is a vector, in TensorFlow it is called as Tensor of Rank 1. In case of a matrix, it is known as Tensor of Rank 2 and so on.
sclr=999
zero_0D= tf.zeros_like(sclr)
one_0D= tf.ones_like(sclr)
with tf.Session() as sess:
print(sess.run(zero_0D))
print("\n",sess.run(one_0D))
Output:
0
1
- Various Data Types used in tensorFlow:
- We can use some of the Numpy datatypes in TensorFlow
tf.ones([5,10], np.float32)
with tf.Session() as sess:
print(sess.run(tf.ones([5,10], np.float32)))
Output:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
Variables (Refer this file):
- We need variables at certain cases where a constant won't work. For example, while creating models, we we to update the weights and biases while training the data. It is not possible for a constant to do. Hence, we require a variable. We have to create instance for the class
tf.Variable()
const= tf.constant([2,3], name="constant")
print(tf.get_default_graph().as_graph_def())
- Old way to create variables
n= tf.Variable(2, name="Scalar")
o= tf.Variable([[1,2],[3,4]], name="Matrix")
p= tf.Variable(tf.ones([20,10]))
- Recommended way to create variables
tf.get_variabels(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, chaching_device=None, validate_shape=True, use_resource=None, custom_getter=None, constraint=None)
example-
scl1= tf.get_variable("Scalar1", initializer=tf.constant(100))
matrx1= tf.get_variable("Matrix1", initializer=tf.constant([[1,0],[0,1]]))
massv_matrx1= tf.get_variable("Massive_matrix1", shape=(1000,50), initializer=tf.ones_initializer())
Initialize variables:
- We have to initialize variables before initializing them else we will get an error
FailedPreconditionError: Attempting to use uninitialized value
- To get the list of uninitialized variables we have to execute the below codes
with tf.Session() as sess:
print(sess.run(tf.report_uninitialized_variables()))
- To initialize variables we have to write
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
- To initialize a subset of variables
with tf.Session() as sess:
sess.run(tf.variables_initializer([scl, matrx1]))
- To initialize each variables independently
with tf.Session() as sess:
sess.run(massv_matrx1.initializer)
Evaluate values of variables:
- To obtain the value of any variable, we have to do it within a Session(just as we do with the Tensors for any rank)
massv_matrx2= tf.get_variable("Massive_matrix2", shape=(1000,50), initializer=tf.glorot_normal_initializer())
with tf.Session() as sess:
sess.run(massv_matrx2.initializer)
print(sess.run(massv_matrx2))
- We can also fetch variables value using the below code
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(massv_matrx2.eval())
Assign values to variables:
q= tf.Variable(20)
q.assign(200)
with tf.Session() as sess:
sess.run(q.initializer)
print(q.eval())
Output:
20
The above code creates assigns value of 20 to q instead of 200
In order to assign the value of 200, we have to do it within session
with tf.Session() as sess:
sess.run(q.assign(200))
print(q.eval())
Output:
200
assign()
itself initializes the variable *q* for us. So we do not need to do initialize it
- Create a variable with value 5
five= tf.get_variable("scalar5", initializer=tf.constant(5))
five_times_five= five.assign(five*5)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(five_times_five))
print(sess.run(five_times_five))
print(sess.run(five_times_five))
print(sess.run(five_times_five))
Output:
25
125
625
3125
- Different Sessions in Tensorflow store different values of the variables as defined in the graph
u = tf.Variable(10)
with tf.Session() as sess:
sess.run(u.initializer)
print(sess.run(u.assign_add(10)))
print(sess.run(u.assign_sub(2)))
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(u.initializer)
sess2.run(u.initializer)
print(sess1.run(u.assign_add(10)))
print(sess2.run(u.assign_sub(2)))
print(sess1.run(u.assign_add(100)))
print(sess2.run(u.assign_sub(50)))
sess1.close()
sess2.close()
Output:
20
18
20
8
120
-42
- Variable dependent on another variable
v= tf.Variable(tf.truncated_normal([100,20]))
w= tf.Variable(v*5)
with tf.Session() as sess:
sess.run(w.initializer)
print(sess.run(w))
- We should always use initialized_value() on the independent varaible before it is used to initialize the dependent variable
w= tf.Variable(v.initialized_value()*5)
with tf.Session() as sess:
sess.run(w.initializer)
print(sess.run(w))
Interactive Sessions (Refer this file):
-
It is created as a default session where we can call the run() and eval() whithout explicitly calling the session everytime
-
Though this looks convenient and easey but it creates problem when we have to work on multiple sessions
sess=tf.InteractiveSession()
a=tf.constant(10)
b=tf.constant(30)
c=a*b
print(c.eval())
sess.close()
Output:
300
Importing Data (Refer this file):
As mentioned earlier a TensorFlow program has two parts:
-
Step1: Create a graph
-
Step2: Evaluate variables and execute operations using a session
a= tf.placeholder(tf.float32, shape=[5])
b=tf.constant([12,13,1,23,5], tf.float32)
summation= a+b
with tf.Session() as sess:
print(sess.run(summation, {a: [1,2,3,4,5]})) #declare the values of the placeholder
Output:
[13. 15. 4. 27. 10.]
Eager Execution (Refer this file):
Please visit the other folders for more