## Introduction to TensorFlow Session

TensorFlow Session is a session object which encapsulates the environment in which Operation objects are executed, and data objects are evaluated. TensorFlow requires a session to execute an operation and retrieve its calculated value. A session may own several resources, for example, tf.QueueBase, tf.Variable, tf.ReaderBase. It is important to make these resources free when their job is done they are not required anymore. For closing the open session, you can either call the tf.Session. close method on the session, or you can use the session as a context manager.

### What is TensorFlow?

- TensorFlow is an open-source library for Deep Learning.
- It was developed by the Google Brain Team and released in November 2015.
- TensorFlow is mainly used for Classification, Perception, Understanding, Discovering, Prediction and Creation.
- TensorFlow consists of two words Tensor + Flow which means Data + Flow.

### What is Tensor?

Tensor is defined as an n-dimensional array:

**0-d tensor:**scalar(number)**1-d tensor:**vector**2-d tensor:**matrix

### Graphs and Sessions in TensorFlow

TensorFlow graph execution separates the definition of computation from their execution.

**Phase 1:**Assemble a graph.**Phase 2:**It uses a session to execute operations in the graph.

TensorFlow has some benefits mentioned as:

- Save computation. It only runs subgraphs that generate the input values you need to execute.
- It breaks big calculations into small calculations, differential pieces to facilitate auto-differentiation.
- Facilitate distributed computation, it distributes the work across various CPUs, graphics, TPUs, and other processing units.
- Many models of machine learning are visualized as TensorFlow graphs.

### TensorFlow Calculation Graph

**Placeholders:**Placeholders are defined as variables that are used instead of given inputs to feed to the TensorFlow graphs.**Variables:**Models are the variables that are going to be enhanced to make the model work better way.**Model:**Model is a mathematical function that computes output which is based on placeholder value and model variables.**Loss Measure:**It is a pioneer for the optimization of variables of the model.**Optimization Method:**It updates the method for tuning variables of the model.**Session:**It is a runtime where working nodes are executed and tensors are estimated.

### TensorFlow Session and its Implementation

A TensorFlow session is a pointer to point to the part that in your big picture or in that big structure only rounded parts of the structural ones.

It is a Python library so the first thing we need to do to use it is imported it into our Python environment as:

**Code:**

`#Import TensorFlow Library:`

import tensorflow as tf

This line will print the installed version of TensorFlow on your computer.

**Code:**

`#Print TensorFlow version:`

print(‘TensorFlow Version: ‘ + tf.VERSION)

### Example of TensorFlow Session

Given below is the example mentioned:

Here, we are going to calculate two matrices and use match multiplication. We will use the constant value of that matrix 1 so it’s a two-dimensional matrix for one row and two columns. If you know the NumPy in python, you know what is the use of this matrix. Now define matrix 2 with 2 rows and 1 column.

4.5 (7,727 ratings)

View Course

If we use matrix multiplication operation with just basically to calculate metrics, it will calculate as simply in maths, three times two plus another three times plus two means the result should be twelve. So the product for this matrix multiplication is to calculate the matrix 1 multiply by matrix 2. If you ever use the NumPy library in Python then you would already know that it’s similar to np.dots. So the method 1 to use the session is like defined a session as tf and result because two sessions that round that points or that part because here in this example, we don’t have any variable to define it. So we don’t have to run the initial all variables operation. We just directly to run this product.

In method 2 it’s using the Python “with” and run the other lines of codes. In this method, we just open this session assess and store this session in the variable of the session. In this way, we are going to calculate result 2, choose the session that’s run the product and print the result. Here, we don’t have to close this session because once we run this sess, so after this session has been closed.

Code for matrix multiplication with both methods:

**Code:**

`import tensorflow as tf`

matrix1 = tf.constant([[3, 3]])

matrix2 = tf.constant([[2],

[2]])

product = tf.matmul(matrix1, matrix2) #np.dot(m1, m2)

#method 1

sess = tf.session()

result = sess.run(product)

print(result)

sess.close()

#method 2

with tf.Session() as sess:

result2 = sess.run(product)

print(result2)

**Output:**

This will be the output for both the method mentioned above in the code.

### Why TensorFlow?

Given below shows why TensorFlow:

#### 1. Runs Everywhere

- The first point to use TensorFlow is that it runs everywhere.
- Like it runs on desktop and mobile devices such as Linux, macOS, iOS, Android, Raspberry Pi and Windows.

#### 2. Flexibility

- Python APIs provide flexibility to create all types of calculations (including any neural network architecture we can think of).
- Includes highly efficient C++ implementations of many ML operations.

#### 3. Large Community

- TensorFlow is one of the most popular open-source projects on the platform of GitHub.
- Growing community contributing to improving it.

#### 4. Google Products

It is also used in many of Google’s large-scale and awesome services, such as:

- Google Cloud Speech
- Google Photos
- Google Search

#### 5. Big Companies Using TensorFlow

- OpenAI
- DeepMind
- Uber
- eBay
- DropBox
- A bunch of Startups

#### 6. Cool Projects Using TensorFlow

**Project 1:**Image classification**Project 2:**Object detection**Project 3:**Speech recognition**Project 4:**Deep learning-driven jazz generation**Project 5:**Reset colors in black & white photos and videos**Project 6:**Transferring style from famous paintings

### Conclusion

There are a lot of and awesome things that software engineers and scientists have done using Machine Learning, some of which include applications relating to the healthcare department, recommendation engines for movies, music, ads according to the user interest and social media sentiment mining to name a few. With these enhancements in Machine Learning, Deep Learning and Artificial Intelligence that appear mind-boggling, TensorFlow is one of the tools that is helping to achieve these types of aims.

### Recommended Articles

This is a guide to TensorFlow Session. Here we discuss the introduction, graphs and sessions, calculation graph implementation & example. You may also have a look at the following articles to learn more –