Tensorflow is an open-source software library for numerical computation using data flow graphs that enables machine learning practitioners to do more data-intensive computing.
It provides a robust implementation of some widely used deep learning algorithms and has a flexible architecture. The main features of TensorFlow are fast computing, flexibility, portability, easy debugging, unified API, extensible.
It was originally developed by researchers and engineers working on the Google brain team within Google’s machine intelligence Research Organisation but was further released under Apache 2.0 open source license on November 9, 2015.
Tensorflow 1.x
- Programs on 0.x may not necessarily work on 1.x .
- Numpy was more closely resemble.
- More stable for array-based computation.
- Two API’s for Java and Go were introduced.
- Tensorflow debugger was introduced.
- A new Android demo for object detection and localization camera based image stylization was introduced.
Computational graphs
A computational graph is a data flow graph which TensorFlow internally represents while performing an operation.
This directed graph has a set of nodes, each of which represents an operation and set of directed arcs, each representing the data on which operations are performed.
It has two types of edges one called Normal, which are only Carriers of data structure between the nodes and the second one is called Special which does not carry any value but shows the controlled dependency between two nodes.
In this graph, we can see two arcs which are to be computed and then an arc where the value computed will be assigned, and also between them is the node where the operation is represented and connecting these all nodes and arcs are the normal edges.
The need for a computational graph is that, TensorFlow schedules the most efficient way while evaluating the complex expressions in the learning phase which distributes the computational load.
Programming with Tensorflow
In this we have three phases first is the construction of the computational graph, then the running session and finally the resulting data collection and analysis.
import tensorflow as tf with tf.Session() as session: a=tf.placeholder(tf.float32,[1],name="a") b=tf.placeholder(tf.float32,[1],name="b") c=tf.constant(1.0) b=a+c a_in=[100] b_out = session.run(b,{a:a_in}) print (b_out)
This is the simplest program of TensorFlow where we will add two numbers with the help of placeholders and constant. Of course, this is not the thing for which TensorFlow is used but still, it is the most basic one to learn.
In the first line of the above code:
import TensorFlow as tf
The TensorFlow library is imported into our code and named as tf
as it will be used many times which we usually always do in python for libraries.
Now in next line:
with tf.session() as session:
here we started the session for our program, this object will contain the computational graph, so the things related to it like nodes and arcs in the form of operations and data will be here.
Next we will insert data or tensor in next three lines:
a=tf.placeholder(tf.float32,[1],name="a") b=tf.placeholder(tf.float32,[1],name="b") c=tf.constant(1.0) b=a+c
In this part, we have defined data or tensor via placeholders or we can simply say that we have inserted two data or tensor whose value will later be given but still we will think that we have that tensor.
Placeholder here has 3 arguments:
- datatype
- shape, here it is 1-D
- name of the tensor
And also we have inserted a constant c (a tensor) with value 1.0.
Now the operation which is to be performed is written with the listed above data and finally, the computational graph will be made which will be somehow similar to the graph in paragraph 3. We can do any operation here and the computational graph will be computed.
Now the last two phases:
a_in=[100] b_out = session.run(b,{a:a_in}) print (b_out)
As ‘a’ was a placeholder, so value of it is given first in x_in
and then in b_output
we will be taking the data in b.
In the line:
b_out = session.run(b,{a:a_in})
We have 2 arguments, the first one is the list of graph element to be evaluated so here only ‘y’ and in the second argument is for what values we are evaluating for, so x:x_in
.
And also the last line to print the output we used print
command.
Data Models
Data models in TensorFlow are represented by tensors, which identifies a multidimensional array.
Rank, Shape, and Datatype are the three parameters of the data structure.
Rank
It is the dimensional unit of the tensor, by which we can say that a rank zero tensor is scalar and rank one is a 1-D tensor.
Shape
It is the number of rows and columns a tensor has.
We can use .getshape()
to know the shape of any tensor.
Datatypes
As by name, Datatypes are type of data to be used. Whenever a variable is to be used, it must be initialised first and it’s type must be defined. In Tensorflow there are many data types available among which some commonly used are:
- Integers : tf.int8, tf.int16, tf.int32, tf.int64
- Unsigned Integers : tf.uint8, tf.unit16
- Floating point : tf.float32, tf.float64
- String : tf.string
- Booleans : tf.bool
Summary
In the article, we tried to know about Tensorflow and it’s properties. We got basic knowledge about computational graphs and it’s parts. We covered basic programming knowledge on Tensorflow. To learn more about programming and classification with TensorFlow one can go through Multiclass classification using Tensorflow.