Skip to main content

Simple Basics of TensorFlow

TensorFlow is a library for numerical calculation where information moves through the chart. Information in TensorFlow is spoken to by n-dimensional clusters called Tensors. Chart is made of data(Tensors) and scientific tasks.
  • Nodes on the graph: represent mathematical operations. 
  • Edges on the graph: represent the Tensors that flow between operations. 
There is one more aspect in which TensorFlow is very different from any other programming language. In TensorFlow, you first need to create a blueprint of whatever you want to create. While you are creating the graph, variables don’t have any value. Later when you have created the complete graph, you have to run it inside a sessiononly then the variables have any values. More on this later. 

How about we begin learning by doing. Run python and import tensorflow:

(i) Graph in TensorFlow:

Chart is the foundation of TensorFlow and each calculation/task/factors live on the diagram. Everything that occurs in the code, dwells on a default diagram gave by TensorFlow. You can access this graph by:
You can get the list of all the operations by typing this:
graph.get_operations()
Currently, the output is empty as shown by [], as there is nothing in the graph.
If you want to print to name of the operations in the graph, do this:
This will again be empty. We shall use this to print names of operations after we have added ops to the graph.
Also, It’s possible to create multiple graphs. But let’s worry about that later. 

(ii) TensorFlow Session:

A graph is used to define operations, but the operations are only run within a session. Graphs and sessions are created independently of each other. You can imagine graph to be similar to a blueprint, and a session to be similar to a construction site.
Graph only defines the computations or builds the blueprint. However, there are no variables, no values unless we run the graph or part of the graph within a session.
You can create a session like this:
Whenever, you open a session, you need to remember to close it. Or you can use ‘with block’ like this.
Advantage of with block is: session closes automatically at the end of the with block. We use with block in most of our code and recommend you to do so too.

iii). Tensors in TensorFlow:

TF holds data in Tensors which are similar to numPy multi-dimensional arrays(although they are different from numPy Arrays):

a) Constants:

are constants whose value can’t be changed. You can declare a constant like this: 
As you can see, this is different from other programming languages like python, you can’t print/access constant a unless you run it inside a session. Let’s do it: 

This will produce 1.0 as output.

b) Variables:

are again Tensors which are like variables in any other language. 

Variables(as you can guess by the name) can hold different values as opposed to constants. However, they need to be separately initialized by an init op. It could be taxing to initialize all the variables individually. However, TensorFlow provides a mechanism to initialize all the variables in one go. Let’s see how to do that:
For tf version 0.11 and earlier, use initialize_all_variables()  
>>>init_op = tf.initialize_all_variables()
Use global_variables_initializer() for tf version 0.12 and later.
>>>init_op = tf.global_variables_initializer()
This will add init_op to our tensorflow default graph.
Now run this init_op before you try to access your variable:
This will output 2.0

Now, try to print the operations on the graph: 
This will now output:
Const
test_var/initial_value
test_var
test_var/Assign
test_var/read
init
As you can see, we have declared ‘a’ as Const so this has been added to the graph. Similarly, for the variable b, many ‘test_var’ states have been added to the TensorFlow graph like test_var/initial_value, test_var/read etc. You can visualize the complete network using TensorBoard, which is a tool to visualize a TensorFlow graph and training process.

c)Placeholders: 

are tensors which are waiting to be initialized/fed. Placeholders are used for training data which is only fed when the code is actually run inside a session. What is fed to Placeholder is called feed_dict. Feed_dict are key value pairs for holding data:

Output will be 6.

iv) Device in TensorFlow:

TensorFlow has very strong in-built capabilites to run your code on a gpu or a cpu or a cluster of gpu etc. It provides you options to select the device you want to run your code. However, this is not something that you need to worry about when you are just getting started. We shall write a separate tutorial on this later. So, here is the complete picture:


Comments

Post a Comment

Popular posts from this blog

Integrating Tensor flow API into ASP.net Web applications for Image detection

Introduction Tensor flow provides advanced Machine Learning API. For the example here we will provide a solution for the Image recognition. The same can be applied to other use cases such as Text recognition or any other data set that needs to solve the problems of AI and Machine Learning such as: Predictive Analytics Cognitive Processes and its Automation e.g. NLP Virtual Agents (Bot’s) Camera Image detection and processing e.g. Industrial Robotics and Drones Text Knowledge Analytics Application Development Steps In the first step we create an ASP.NET application for connecting the camera feed or input data. In the next step Install Tensor flow as per instructions on the website –>  Click here To begin using Tensor flow we need to decide on the model to use for processing input data – There is an option to use either ready-made models or develop on own depending on the problem at hand. For simplicity we will use ready-made model called a deep  convolut...

Machine Learning with ML.NET 1.0

As a person coming from .NET world, it was quite hard to get into  machine learning  right away. One of the main reasons was the fact that I couldn’t start Visual Studio and  try out  these new things in the technologies I am proficient with. I had to solve another obstacle and learn other  programming languages  more fitting for the job like Python and R. You can imagine my happiness when more than a year ago,  Microsoft  announced that as a part of  .NET Core 3 , a new feature will be available –  ML.NET . In fact it made me so happy that this is the third time I write similar  guide . Basically, I wrote one when ML.NET was a  version 0.2  and one when it was  version 0.10 . Both times, guys from Microsoft decided to modify the  API  and make my articles obsolete. That is why I have to do it once again. But hey, third time is the charm, so hopefully I will not have to do this again until ML.N...

Using Tensorflow Object Detection API to build a Toy detector

Here I extend the API to train on a new object that is not part of the COCO dataset. In this case I chose a toy that was lying around. See gif below. So far, I have been impressed by the performance of the API. The steps highlighted here can be extended to any single or multiple object detector that you want to build. Tensorflow Toy Detector~ You can find the code on my  Github  repo Collecting data The first step is collecting images for your project. You could download them from google ensuring you have a wide variation in angles, brightness, scale etc. In my case I created a video of the little aeroplane toy and used  Opencv  to extract images from the video. This saved me a lot of time. I ensured that images were taken from multiple angles. You can also randomly change brightness for some of the images so that the detector can work under different conditions of lightning. Overall 100–150 pics will suffice. See some sample images below: ...