Introduction to tensorflow Part 2

If you are new to tensorflow, we recommend to start this series from part 1

This is part 2 of tensorflow series. The goal of every single article is to make you understand one of the most popular deep learning library out there. This series is entirely focused on beginners, who are either starting to learn deep learning and want to built their own neural nets or want to build state-of-the-art neural network’s in tensorflow. This series of article will specifically covers basics of tensorflow for python, there is another version of tensorflow.js for javascript developers but that will be covered in next series.In last part of the series we will be building a MLP  for MNIST dataset using tensorflow.

After reading this article you will know,

  • installing tensorflow
  • basic construct of tensorflow model
  • some popular functions of tensorflow
Installing tensorflow

Before even begin installing tensorflow, I am assuming you have already installed anaconda with python 3.6, if not then comment below. Setting up tensorflow, is quite simple, follow these steps

Making base environment as a tensorflow environment

  • Open anaconda prompt
  • type following command for cpu version of tf

pip install --ignore-installed --upgrade tensorflow

  • type following command for gpu version of tf

pip install --ignore-installed --upgrade tensorflow-gpu

though above method works well but, It is recommend to create a separate tensorflow environment. For this follow following steps.

  • Open anaconda prompt
  • create a environment in anaconda named tensorflow

conda create -n tensorflow pip python=3.6

  • activate the tenorflow environment by issuing following command

activate tensorflow

then install it by running same above commands for cpu or gpu. you can think of  environment as workspace in anaconda.To check if tensorflow properly installed type following in ipython notebook.


import tensorflow as tf 
string = tf.constant('Hello world')
print(tf.Session().run(string))

Basic Construct of tensorflow model

tensorflow model consist of mainly following parts defined in a sequence,

  • Defining placeholders

tf.placeholder(tf.float32) #this creates a placeholder of type float, it can also be created as matrix or array

  • we can also create weight and bias matrix

Weight_matrix = tf.Variable(tf.zeros([x_dimension, y_dimension])) #creates weight matrix
bias_matrix  = tf.Variable(tf.zeros(['total no of bias needed'])) #create a bias matrix

  • define loss function for e.g Mean squared loss

loss_function=tf.reduce_mean(tf.square(tf.sub(predicted_y,actual_y)))

  • if you remembered how we calculate predicted_y in simple logistic unit

predicted_y = tf.nn.sigmoid(tf.matmul(x, W) + b)


optimization = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

The following parts will change according to problem statement but for a simple logistic unit this is the parts we construct.

Popular functions of tensorflow

There are many function in whole tensorflow framework covering all is beyond scope of this article, so we will cover only those which we will be using

tf.nn.sigmoid : It produce element wise sigmoid activated value of a matrix vector or scaler.

tf.matmul : It is a efficient implementation of dot product in tensorflow.

tf.train : train is itself not a function but a class which contains implementation of almost all popular optimizers like GD, SGD, rms_prop, adadelta etc.

rest of the functions will be covered as we go in more detail, In next article we will cover how you can build a complete MLP for MNIST using tensorflow.

For part 3 click here.

Send a Message