# basics of TensorFlow with examples

TensorFlow is a Machine Learning library released in 2015 by Google Brain Team to provide ease in implementing Machine Learning Algorithms. I will not delve much inside it’s about. Visit the official website of TensorFlow – here

Let’s start with the concept and implementation of TensorFlow basics.

**What is a Tensor?**

An n-dimensional array of elements is simply a Tensor. A scalar quantity is a Rank-0 tensor, a two-dimensional vector is a Rank-1 tensor, a matrix is a Rank-2 tensor and so on. In fact, scalars and vectors are the simplest forms of Tensors.

**Session in Tensorflow**

A Session object encapsulates (encloses/contains) the environment in which Operation objects are executed, and Tensor objects are evaluated.

**Constants, Variables and Placeholders in ****TensorFlow**

**TensorFlow**

**Constants** are those whose values remain constant. While initializing a constant we must keep in our mind that we have to specify the type of constant in Tensorflow as in Tensorflow implicit type casting returns error. So for example, if you want to declare a = 5, then you need to mention that you are storing an integer value in a.

# If you have not already installed Tensorflow then # open the terminal and type - pip3 install tensorflow # and hit enter import tensorflow as tf sess = tf.Session() #Note that tensorflow will not perform implicit type casting. # For int64 => dtype = tf.int64 # For int32 => dtype = tf.int32 # For float64 => dtype = tf.float64 # For float32 => dtype = tf.float32 a = tf.constant(5.6, dtype=tf.float32) a.eval(session=sess)

Output:

5.6

**Variables** are those whose values can be modified.

import tensorflow as tf sess = tf.Session() # Let's construct a 2X2 variable matrix. a = tf.Variable(tf.zeros((2,2))) sess.run(tf.global_variables_initializer()) # All the variables in session 'sess' will be initialized. # Note that evaluating an uninitialized variable will return error. a.eval(session=sess)

Output:

array([[0., 0.] [0., 0.]], dtype=float32)

**Placeholders** are “spaces” to feed data when needed.

import tensorflow as tf # Placeholders of 2X2 matrix. a = tf.placeholder(tf.float32, shape=(2,2)) b = tf.placeholder(tf.float32, shape=(2,2)) # addition operation c = a + b x = [[2,4],[5,3]] y = [[1,3],[6,4]] with tf.Session() as sess: #values of x is dumped into a and values of y is dumped into b to calculate c. print(sess.run(c, feed_dict = {a:x, b:y}))

Output:

[[ 3. 7.] [11. 7.]]

**Graphs**

A Graph is a blueprint of the computations and operations in a session. Tensorflow first creates computational graph where the nodes are operations and edges are tensors and then executes in a session.

import tensorflow as tf # Placeholders of 2X2 matrix. a = tf.placeholder(tf.float32, shape=(2,2)) b = tf.placeholder(tf.float32, shape=(2,2)) # addition operation c = a + b x = [[2,4],[5,3]] y = [[1,3],[6,4]] with tf.Session() as sess: # We can save the computational graph for further visualization. path='/home/mrityunjay/Documents/' # This will be the location where summary of the computation graph is saved. tensor_graph=tf.summary.FileWriter(path, sess.graph) #values of x is dumped into a and values of y is dumped into b to calculate c. print(sess.run(c, feed_dict = {a:x, b:y}))

Now you can visualize the computational graph saved in your local memory using the following method. Note that the path mentioned here needs to be modified according to you. Open the terminal and type –

$tensorboard --logdir=/home/mrityunjay/Documents/

This will open up the tensorboard host which you can open in the browser by clicking on the link. Soon we will talk about tensorboard also.

I hope you got the core concepts of Tensorflow such as Sessions, Constants, Variables, Placeholders and Graphs. Feel free to comment.

You may also read,

- Why Python Is The Most Popular Language For Machine Learning
- Prepare your own data set for image classification in Machine learning

## Leave a Reply