Python TensorFlow – A Beginner’s Introduction

TensorFlow is a mathematical library package designed to create models for machine learning. These models can be designed in an efficient way when using TensorFlow than other conventional methods. This software is created in an open-source environment by google for designing ML and Artificial Intelligence systems, along with deep neural networks.

But at first, let’s understand what’s is AI or ML or neural networks before we go further into more complex stuff.

Start with: Machine Learning In Python – An Easy Guide For Beginner’s

What Is Machine Learning?

Machine Learning is a process of making self-learning models for computers by feeding it thousands of real-world examples, just like humans learn. Through machine learning, software systems can learn and evolve by picking up real-world examples and deriving rules and parameters from them. Self-learning is the core of ML and the path to artificial intelligence.

What Is Artificial Intelligence?

Artificial intelligence simply means, computers behaving like humans. In layman’s terms, artificial intelligence systems are programs capable to take on human tasks independently and execute them with the same or greater efficiency.

For example, an AI-controlled speed meter scans for cars running at a speed higher than the speed limit and then snaps their number plates automatically. A human involved in the same process can focus on just 1-2 cars at a time, while an AI can easily handle snapping number plates of a dozen cars at once.

What Is TensorFlow?

While such complex AI software systems exist “How do we train computers to understand and identify cars utilizing a camera feed, and then identify their speed, and snap their plates, all in real-time?”

One way to do it is using TensorFlow.

Its deep learning systems and convolutional neural networks make completing tasks like these possible for computers.

For example, TensorFlow contains large image libraries known as MNIST that contain thousands of 2×2 pixels images of day-to-day things to train computers to identify real-world objects.

Using MNIST, models can be created that train computer systems how a car looks like and how to identify high-speed cars in a camera feed. But to keep things simple we will start with the basics.


At the very beginning, we will understand how TensorFlow is installed, and then further we will create a machine learning model that teaches a computer system to decipher a formula from a collection of data.

Installing And Setting Up TensorFlow

As TensorFlow is a third-party library function, it is not found preinstalled in any python IDE. So, at first, you need to learn to install and configure TensorFlow in order to use it as an import package. In this example, we are using Pycharm IDE to install and use TensorFlow in it, as it is simpler than other IDE’s.

Install the latest version of PyCharm IDE, python, and pip packages into your system. Once installed, follow the below steps to configure it:

  • Open cmd and write the command ‘pip install tensorflow’ Note: The script folder containing pip should be included in #PATH
  • Once installed, head over to PyCharm IDE.
  • Create a new project and make a python file into that project (with .py extension)
  • Then go to File > Settings, on left pane click on the name of the project you created. In drop down menu, you will find an option named Python Interpreter.
Configuring_tenforflow_1.jpg
  • Python Interpreter contains all the necessary interpreters required for that project. You need to find and click on + sign just above ‘Package’ column.
  • Clicking on it opens a new window where a great list of python interpreters are available. You need to search ‘TensorFlow’ and choose the one which is just named ‘TensorFlow’, nothing else
configuring_tensorflow_2.jpg
  • Click on install package at the bottom. This will install TensorFlow in your PyCharm system.
  • Once Installed, now we will see how a model is created. Below is an example of a set of values assigned to X and Y.
Set-Values.jpg
Values 1.1

At first the above set of X, Y values looks like random datasets but If we observe closely, we will find that X is a function of Y, and a relationship can be drawn out, Y = 2X – 1.


But how do we reach there? Because after years of taking mathematical tutoring in school working with data patterns is something we get used to. Or we just try to find patterns out of habit and we might have done it several times before.

Teaching the same thing to a system that uses microchips and bits to calculate numbers is another story. It seems difficult or rather even impossible.

That’s because traditional programming takes data and produces results by following parameters. However traditional programming methods cannot make a program predict the rules by itself, by looking at just data and then the final results.

traditional_programming_vs_ml.jpg
Traditional Programming v/s Machine Learning


But whatever traditional programming cannot solve, machine learning does it very easily. It uses a trial and error approach to a problem, just the way a normal person would do but with a digital touch. Let’s understand how.

equation_explanation.jpg
Values 1.2

Now when the other data sets are run into the equation, our guess becomes more solid. The above way is how a human figures a solution to a problem, and machine learning models are trained in the same way to produce similar results.

Below is a code snippet of how a machine learning program built using TensorFlow learns the problem and finds its solution

import keras
import tensorflow
import numpy as np

model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

model.compile(optimizer='sgd', loss='mean_squared_error')

vx = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
vy = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)

model.fit(vx, vy, epochs=200)

print(model.predict([8.0]))
code_explanation.jpg
Values 1.3

When the process is completed, the system would be trained enough to figure out the formula and guess the Y value for a given X value. Here 8.0 is provided as the X value which should give 15 as the answer but when the program is run, the output shows 14.990.

It’s because, the first set of 6 values of X and Y are a straight line, and the program is only trained for that set of values. But for any given data that is outside that parameter, the system won’t be able to predict that if it will be a straight line and will only give the nearest value.

Conclusion

So, in this article, we learned what is TensorFlow, the basics of machine learning, and AI.

We also learned how to install TensorFlow in your system and create a machine learning model from scratch.

An exhaustive in-depth explanation of the code is also provided so you don’t feel confused at any point in the article.

This article can play as a good anchor if you are starting out in machine learning or TensorFlow as it provides laying stones to your basics of ML.