# Introduction to TensorFlow.

# What is TensorFlow?

The shortest definition would be, TensorFlow is a general-purpose library for graph-based computation.

But there is a variety of other ways to define TensorFlow, for example, Rodolfo Bonnin in his book – Building Machine Learning Projects with TensorFlow brings up definition like this:

*“TensorFlow is an open source software library for numerical computation using data flow **graphs. Nodes in the graph represent mathematical operations, while the graph edges **represent the multidimensional data arrays (tensors) passed between them.”*

To quote the TensorFlow website, TensorFlow is an “*open source software library for numerical computation using data flow graphs*”. Name TensorFlow derives from the operations which neural networks perform on multidimensional data arrays, often referred to as ‘tensors’. It is using data flow graphs and is capable of building and training variety of different machine learning algorithms including deep neural networks, at the same time, it is general enough to be applicable in a wide variety of other domains as well. Flexible architecture allows deploying computation to one or more CPUs or GPU in a desktop, server, or mobile device with a single API.

TensorFlow is Google Brain’s second generation machine learning system, released as open source software in 2015. TensorFlow is available on 64-bit Linux, macOS, and mobile computing platforms including Android and iOS. TensorFlow provides a Python API, as well as C++, Haskell, Java and Go APIs. Google’s machine learning framework became lately ‘hottest’ in data science world, it is particularly useful for building deep learning systems for predictive models involving natural language processing, audio, and images.

# What is ‘Graph’ or ‘Data Flow Graph’? What is TensorFlow Session?

Trying to define what TensorFlow is, it is hard to avoid using word ‘graph’, or ‘data flow graph’, so what is that? The shortest definition would be, TensorFlow Graph is a description of computations. Deep learning (neural networks with many layers) uses mostly very simple mathematical operations – just many of them, on high dimensional data structures(tensors). Neural networks can have thousands or even millions of weights. Computing them, by interpreting every step (Python) would take forever.

That’s why we create a graph made up of defined tensors and mathematical operations and even initial values for variables. Only after we’ve created this ‘recipe’ we can pass it to what TensorFlow calls a session. To compute anything, a graph must be launched in a Session. The session runs the graph using very efficient and optimized code. Not only that, but many of the operations, such as matrix multiplication, are ones that can be parallelised by supported GPU (Graphics Processing Unit) and the session will do that for you. Also, TensorFlow is built to be able to distribute the processing across multiple machines and/or GPUs.

TensorFlow programs are usually divided into a construction phase, that assembles a graph, and an execution phase that uses a session to execute operations in the graph. To do machine learning in TensorFlow, you want to create tensors, adding operations (that output other tensors), and then executing the computation (running the computational graph). In particular, it’s important to realize that when you add an operation on tensors, it doesn’t execute immediately. TensorFlow waits for you to define all the operations you want to perform and then optimizes the computation graph, ‘deciding’ how to execute the computation, before generating the data. Because of this, tensors in TensorFlow are not so much holding the data as a placeholder for holding the data, waiting for the data to arrive when a computation is executed.

# Prerequisites:

### NEURAL NETWORKS – basics.

Before we move on to create our first model in TensorFlow, we’ll need to get the basics right, talk a bit about the structure of a simple neural network.

A simple neural network has some input units where the input goes. It also has hidden units, so-called because from a user’s perspective they’re hidden. And there are output units, from which we get the results. Off to the side are also bias units, which are there to help control the values emitted from the hidden and output units. Connecting all of these units are a bunch of weights, which are just numbers, each of which is associated with two units. The way we train neural network is to assign values to all those weights. That’s what training a neural network does, find suitable values for those weights. One step in “running” the neural network is to multiply the value of each weight by the value of its input unit, and then to store the result in the associated unit.

There is plenty of resources available online to get more background on the neural networks architectures, few examples below:

- neuralnetworksanddeeplearning.com – by Michael Nielsen
- deeplearningbook.org – by Ian Goodfellow, Yoshua Bengio, Aaron Courville.
- Machine Learning – by AndrewNg via Coursera
- Neural Networks for Machine Learning – by Geoffrey Hinton via Coursera
- Robotics: Vision Intelligence and Machine Learning – edx – University of Pennsylvania
- Make Your Own Neural Network by
- Neural Networks for Complete Beginners: Introduction for Neural Network Programming
- Introduction to the Math of Neural Networks
- Neural Network Programming with Python: Create your own neural network!
- Neural Networks: A Comprehensive Foundation (2nd Edition)

## MATHEMATICS

Deep learning uses very simple mathematical operations, it would be recommended to get/refresh at least basics of them. I recommend starting from one of the following:

- Vector Algebra- simpleNeasyBook by WAGmob
- Vector Calculus, Linear Algebra and Differential Forms: A Unified Approach by Barbara Burke Hubbard, John H. Hubbard
- deeplearningbook.org – by Ian Goodfellow, Yoshua Bengio, Aaron Courville.
- Linear algebra cheat sheet for deep learning by Brendan Fortuner
- Data Science Math Skills – by Coursera
- Linear Algebra – Foundations to Frontiers – by edx

## PYTHON

It would be advised to have basics Python programming before moving forward, few available resources:

- A Byte of Python by Swaroop C H
- Python for Everybody – Prof.Charles Severance
- Python Programming by Wikibooks
- Think Python: How would you Think Like a Computer Scientist by Allen Downey
- Dive Into Python 3 by Mark Pilgrim
- Python Machine Learning
- Python Programming: An Introduction to Computer Science
- Introduction to Data Science in Python – by Coursera
- Python for Data Science by edx

# Let’s do it… TensorFlow first example code.

To keep things simple let’s start with ‘Halo World’ example.

importing TensorFlow

import tensorflow as tf

Declaring constants/variables, TensorFlow constants can be declared using the *tf.constant* function, and variables with the *tf.Variable* function. The first element in both is the value to be assigned the constant/variable when it is initialised. TensorFlow will infer the type of the constant/variable initialised value, but it can also be set explicitly using the optional *dtype *argument. It’s important to note that, as the Python code runs through these commands, the variables haven’t actually been declared as they would have been if you just had a standard Python declaration.

x = tf.constant(2.0) y = tf.Variable(3.0)

Lets make our code compute something, simple multiplication.

z = y * x

Now comes the time when we would like to see the outcome, except nothing, has been computed yet… welcome to the TensorFlow. To make use of TensorFlow variables and perform calculations, Session must be created and all variables must be initialized. We can do it using the following statements.

sess = tf.Session() init = tf.global_variables_initializer() sess.run(init)

We have Session and even all constants/variables in place. Let’s see the outcome.

print("z = y * x = ", sess.run(z))

If you see something like this:

‘z = y * x = 6.0’

Congratulations, you have just coded you first TensorFlow ‘model’.

Below whole code in one piece:

import tensorflow as tf x = tf.constant(2.0) y = tf.Variable(3.0) z = y * x sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print("z = y * x = ", sess.run(z))

This tutorial, of course, will not end up like this and will be continued soon… in next part, we will code our first neural network in TensorFlow.

Was the above useful? Please share with others on social media.

If you want to look for more information, check some free online courses available at coursera.org, edx.org or udemy.com.

Recommended reading list:

Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems Through a series of recent breakthroughs, deep learning has boosted the entire field of machine learning. Now, even programmers who know close to nothing about this technology can use simple, efficient tools to implement programs capable of learning from data. This practical book shows you how. By using concrete examples, minimal theory, and two production-ready Python frameworks—scikit-learn and TensorFlow—author Aurélien Géron helps you gain an intuitive understanding of the concepts and tools for building intelligent systems. You’ll learn a range of techniques, starting with simple linear regression and progressing to deep neural networks. With exercises in each chapter to help you apply what you’ve learned, all you need is programming experience to get started. Explore the machine learning landscape, particularly neural nets Use scikit-learn to track an example machine-learning project end-to-end Explore several training models, including support vector machines, decision trees, random forests, and ensemble methods Use the TensorFlow library to build and train neural nets Dive into neural net architectures, including convolutional nets, recurrent nets, and deep reinforcement learning Learn techniques for training and scaling deep neural nets Apply practical code examples without acquiring excessive machine learning theory or algorithm details |