Deep Learning

A beginner's guide to Tensorflow for Deep Learning

January 4, 2020

On Nov. 9, 2015, Google released TensorFlow under Apache 2.0 open source license, since then, TensorFlow has emerged as the major framework development library in the domain of deep learning. This beginner's guide to Tensorflow will provide a base for all deep learning enthusiast.

We will discuss what TensorFlow does and when to use it with which language. We will also be comparing and contrasting TensorFlow against its contemporary libraries used for developing deep learning frameworks. 

Content:

  • What is TensorFlow
  • Prerequisites for using TensorFlow
    • TensorFlow with Python
    • TensorFlow with R
  • How does TensorFlow Work?
  • TensorFlow Vs. Others
  • Step by Step Guide to Build a project in TensorFlow
    • Step 1: Set the Stage
    • Step 2: Installing and Gearing Up TensorFlow
    • Step 3: Importing Libraries and Modules
    • Step 4: Project Using TensorFlow
  • TensorFlow Cheat sheet and Sources
  • Way Ahead
  • Endnote

What is TensorFlow

TensorFlow, launched under the Apache Open Source Product License 2.0 by Google on november 19,2015; is the increasingly becoming the most widely used development framework used to build deep learning solutions and applications. 

Generally considered overkill library for general Machine Learning applications, TensorFlow is more useful for deep learning. It offers support for various other libraries on top of it, for example, keras, NumPy, Pandas etc and both Python and R. 

Prerequisites for Using TensorFlow

To begin with TensorFlow, one needs to have intermediate or advanced level proficiency in Machine learning, Deep Learning, basic concepts of mathematics like algebra, probability, statistics etc. and a good understanding of programming using Python or R whichever suits the need of the project in-hand. 

Developing models with TensorFlow will require a good idea about what neural networks are, what is  the underlying logic behind neural networks and how can which type of neural networks be used to solve problems. 

For an absolute beginner on the scene, we recommend that you to learn Python, basics of advanced mathematics and basic concepts of machine learning before beginning to build models in TensorFlow. After these steps, you can begin with deep learning concepts like neural networks, convolutional neural networks, layering, various algorithms etc. to further strengthen your abilities. 

Using various libraries on top of TensorFlow is a good way to reduce the amount of code that one may have to write. Keras is one such library which is widely used with TensorFlow to mask the extensive amount of code that underlies each model built henceforth.

TensorFlow With Python

Python with Keras on top of Tensorflow is a combination which is becoming the standard of framework development in deep learning

One could use Python directly on top of TensorFlow but the code gets tedious and quite complex very quickly. With Keras adding the abstraction layer, development gets easier and quicker. To know more about Keras for deep learning, click here.

TensorFlow With R

TensorFlow provides support for R. As a general rule, one uses R with Keras on top of TensorFlow as it makes TensorFlow easier to use and coding becomes easier. 

While using R with Tensorflow, projects like Recurrent Neural Networks, Multi-Layer Perceptrons, Skip-Gram Models, Fine-tune the pre-trained models, Convoluted Neural Networks, Use pre-trained models like VGG16, RESNET etc. can be developed. 

Here you will find a basic project on deep learning developed using R with Keras on top of TensorFlow. It is also compared with the same program written using Python further in the document. 

How does TensorFlow Work?

TensorFlow is a computing library for deep learning projects. 

In TensorFlow, each computation is represented as a graph. The nodes in these graphs are called ops (abbreviation for operations). An op takes zero, one or more tensors. 

Tensors are typed, multi-dimensional arrays which represent factors or variables that are interrelated, all these variables have the same type. 

Tensors have ranks. A rank defines the structure of a tensor. 

For example, mammal = tf.Variable("Kangaroo", tf.string)defines a tensor mammal with data type string and rank 0. 

mymat = tf.Variable([[7],[11]], tf.int16), on the other hand, defines a tensor with data type integer and rank 2. 

Here, you will find a descriptive guide to build the computation graphs, launch them and employ them for specific goals. Various operations on these computation graphs are also explained in the said document.

TensorFlow Vs. Others

FeatureTensorFlowTheanoPyTorchDeepLearning4j
LicenseApache Open Source 2.0No license yetNo license yetApache Open Source 2.0
Written inPythonPythonPythonJava
StrengthsEnsured continued support, Visualization suit available, debugging potential, easy to spin session without having to restart programsOptimized for both CPU and GPU, well adapted for numerical tasks, can convert just-in-time compilation to CPU or GPU code.Easy to switch between CPU and GPU, easy to combine modular code blocksSupports JVM languages like Java and Scala, runs parallely on CPUs and GPUs. 
DrawbacksNot completely open sourceRelies on the mathematical side of deep learning more often and has lower number of followers today even though it was the first set of libraries launched.Not accessible to the academic communityBecoming obsolete

Step By Step Guide to Build a Project in TensorFlow: 

  • Step 1: Set the Stage

To begin, we will need to install TensorFlow and set up the environment on your computer. Here, you will find an elaborate guide to downloading and installing your TensorFlow library and setting up the complete package. 

You will need to choose the Python/R option (TensorFlow with X) if you are focussed on developing deep learning frameworks using TensorFlow. 

  • Step 2: Importing Libraries and Modules

Using TensorFlow as the sole library might need you to reinvent the wheel every now and then. 

Importing libraries is an easier and more efficient option. Keras is the most widely used library and adds a nice layer of abstraction, making framework development easy as pie. 

Here you will find a detailed description of which libraries would serve which function and how can you download, install and import these in your TensorFlow environment. 

The Anaconda install and the pip install are the two most common installation procedures used. Anaconda takes the top place as it has most of the standard libraries rolled into one nice package. Pip is popular for its flexibility. 

Anaconda DistributionPip installer
Installs TensorFlow in its own environment, for those running the Anaconda Python distribution. Installs TensorFlow on your machine, possibly upgrading previous packages of Python
Doesn’t affect already existing Python programsMay impact Python programs already existing in the system
  • Step 4: Project Using TensorFlow

In this directory, you will find a list of interesting projects which have ready datasets and a skeletal framework which you can load and work upon. 

You can also start from the scratch once you get the good grip on the coding part. You will also be dealing with the underlying algorithm and logic thereof, hence it is better to have a fine knowledge of deep learning relevant mathematics handy. 

TensorFlow Cheat-sheet and Resources:

Resources:

To begin learning development on TensorFlow, you will need to take an organized approach towards it. If you try and binge brute force through all that is there to know about it, you just might find out that it wasn’t a great idea to begin with.

We could divide the usage of TensorFlow in four function-derived sections, which are: 

  • TensorFlow Basics

In this section, you will learn the basic blocks of programming in TensorFlow. 

For example, you will learn what are variables, constants and operations in TensorFlow, what are computational graphs, what are nodes etc. 

These building blocks will help you enact your full-fledged model further as you work on TensorFlow.

Here, we are assuming you have gotten through the prerequisites already and are aware of at least one out of Python and R.

  • TensorFlow Linear Algebra

TensorFlow uses linear algebra extensively, it is like the core of logic that TensorFlow uses. In this stage, you should study advanced mathematics concepts like linear algebra, calculus, probability, statistical analytics etc. 

  • Machine Learning using TensorFlow

After getting through matrices and tensors, you should take up a hands-on beginner’s project on simple applications of machine learning, using TensorFlow. TensorFlow is far more powerful a tool but this is just a beginning and you can increase the complexity of your projects as you start gaining traction.

  • Distributed Computing using TensorFlow

For the most complex deep learning models that we are working on today or one that you might want to take upon, you will need extensive computing power. 

TensorFlow can run on multiple CPUs/GNUs in a parallel and distributed fashion. Once you are well versed with developing models, distributed computing should be the next milestone.

This division could be used as a step-by-step procedure to follow while beginning with TensorFlow. Here, you will find a course which is developed on this functional division basis and will help you develop your expertise in TensorFlow. 

Cheat-sheets

TensorFlow has a huge community backing and developing resources for it everyday.

Here, you will find cheat sheets listed in the order of their usage which contain direct code blocks to implement various functionalities like computational graphs, fine-tuning performances, matrix loading (inline and otherwise), using various libraries with TensorFlow (like Keras, Pandas, Numpy etc) and importing and loading from various data sources etc. 

These are available in a pdf format for offline usage as well.

Way Ahead:

Currently, the TensorFlow community is working on providing better support for R on TensorFlow because currently it has limited support. 

TensorFlow is widely used due to its sheer ability to run a model right away. For example, if a outmodel came out today in a white paper, you could simply take the model and deploy it using TensorFlow. 

In the enterprise level machine learning scene, deep learning is not yet a major factor. While for academic research and development purposes TensorFlow is doing well, when we look at using it for these enterprises, we will need systems which are supported by cloud infrastructure. 

Hopefully, TensorFlow will adapt to all these changes owing to the ongoing research and development. 

Endnote:

TensorFlow is an up and coming deep learning library whose nature leans more towards a mathematical computational deep learning framework library. With the continual development and research, TensorFlow should adapt well to the demands which will soon enter the scene. 

While learning to use a library like TensorFlow, the best approach is always learning by doing it. TensorFlow is a complex library and will take some getting used to before you can be proficient in using it. We’d recommend a consistent and focussed approach. 

Don’t wait any more, launch that library and build that project!