Getting started with TensorFlow

Adminixtrator
Analytics Vidhya
Published in
4 min readJan 10, 2020

Since the upgrade from version 1 to version 2, I decided to write extensively on TensorFlow. This article is channeled towards Deep Learning with TensorFlow using the version two of the TensorFlow library.

Environment Setup

Runtime: Python 3.7.4 amd64
Tensorflow == 2.0.0

Selecting your IDLE

A notebook such as Jupyter notebook, Zeppelin notebook, Colab notebook or any suitable notebook is recommended for running Tensorflow.

Jupyter Notebook
Colab Notebook

You can also decide to run Tensorflow from your Python console or the Ipython shell. Perhaps you can decide to run it from PyCharm or any other IDLE you deem fit, provided you have everything installed correctly.

Python Console
Ipython Shell

01. Getting Started with TensorFlow

It is recommended that you follow the repository above as all code examples can be found there.

TensorFlow defines computations as graphs, therefore, it is the same as defining a series of operations as a graph, which is launched into a session, then the session translates and passes the operations in the graph to the devices you want to execute them on, either CPU or GPU.

To create a graph, you call the graph() attribute of TensorFlow.
Example:

Creating a graph

Now let’s run a simple operation within our graph and execute it with the session() attribute. It is important to know that the conventions used in TensorFlow 1x (version 1) do not work in TensorFlow 2x, so it is better to declare TensorFlow 2x as tf.compat.v1 to be able to use the attributes from TensorFlow 1x.

The session is closed to release resources

Refer to the repository above to attain a deeper understanding of the introductory concepts.

1.1. Tensors

For the sake of proper understanding, I will divide Tensors into three for a start, namely:
0-tensors
1-tensors
2-tensors

Consider 0-tensors as scalars, 1-tensors as vectors and 2-tensors as matrices. The scalars are addressed as zero tensors because they are assumed to be single numbers, therefore we need nothing to determine them since they are straightforward. The vectors on the other hand, are assumed to be a list, in which you need an index to select a member, hence the name 1-tensors.
To select a member from a matrix, it requires two index elements — the row and the column hence the name 2-tensors.

What are Tensors?

The illustration above gives a vision of a 3-tensor as a rectangular prism of numbers of shape (N, N, N).
In a simple sentence, Tensors are multidimensional arrays.

02. Basic Computations in TensorFlow

TensorFlow concepts are easiest to understand after experimenting with them directly. When we are experimenting with TensorFlow we use the InteractiveSession().

To start the InteractiveSession(), we use tf.InteractiveSession() — which does not work in 2x, therefore, we use tf.compat.v1.InteractiveSession().

Here it has been declared as Tf2x

2.1. Tensor Operations and Scaling

TensorFlow uses python’s operator overloading to make basic tensor arithmetic straightforward with the standard python operators.
Tensors can be either be added directly or added using the tf.add() attribute.

Adding a tensor
Adding a tensor in Interactive Session
tf.add()

Tensors can also be subtracted directly or by using the tf.subtract() attribute.

Subtracting tensors

Also, tensors can also be multiplied directly or by using the tf.multiply() attribute.

Tensor multiplication

Tensors can be divided also, directly or by using the tf.divide() attribute. The tensor.div() attribute uses the Python2 operator conventions.

Tensor division

End Notes

The TensorFlow library has more capabilities compared to what we have just seen, ranging from Computer vision to Artificial Intelligence. The next post will be on Matrix Operations using TensorFlow.
This article is targeted towards TensorFlow for Deep Learning. Thanks for your time, see you in the next article.

--

--