Developing the first Neural Network with TensorFlow

Luca Barone
Mar 2 · 4 min read

Introduction

In this article I’ll explain what a Neural Network is, and how to implement it using the TensorFlow library through Google Colab.

What you need to know

In order to be able to understand this article it would be better if you have some knowledge in Python (Numpy library) and in machine learning.

Machine Learning

Machine Learninng is the field of study that gives computers ability to automatically learn and improve from experience without being explicitly programmed.

In other words, this branch of artificial intelligence uses data to answer questions. While in the past we have manually analyzed data, generated by people, computers, phones and other devices, in the recent years the amount of data has grown exponentially, making it difficult to be handled with traditional methods.

That’s where machine learning comes into play, it can be used to automatically learn from this data, so that we can make sense out of this immense amount of information.

Neural Networks

A neural network is a type of model that can be trained to recognize patterns. It is composed of layers, including input and output layers, and at least one hidden layer, with deep neural network containing more than one hidden layer. Neurons in each layer learn increasingly abstract representions and features of the data making it possible to classify it.

Training the Neural Network

Neural networks are trained by gradient descent. The weights in each layer begin with random values, and these are iteratively improved over time to make the network more accurate. A loss function is used to quantify how inaccurate the network is, and a procedure called backpropagation is used to determine whether each weight should be increased, or decreased, to reduce the loss.

Developing a Neural Network

Python is one of the most popular programming languages for creating Machine learning algorithms. Today Neural networks can be used without knowing precisely how training works. Most modern machine learning libraries, in fact, have greatly automated the training process. The library used I am going to introduce is TensorFlow by Google.

Tensorflow

TensorFlow is an open-source library developed by Google primarily for machine learning applications making it easy to build and deploy ML models.

The name TensorFlow derives from the operations that such neural networks perform on multidimensional data arrays, which are referred to as tensors and are handy in handling large amounts of data.

Colab

Colaboratory is a Google product, similar to Jupyter notebook, allows to write and execute python code through the browser. It is well suited for machine learning as it provides free access to computing resources including GPUs.

Normally the TensorFlow library needs to be installed and imported in order to be used, but on Google Colab it is “ready to use”, we just need to import it!

# Import TensorFlow
import tensorflow as tf

Model Definition

The first model we will develop is going to be really easy but it will be useful to understand how to create, configure, train the model and finally use it to give us a prediction.

Suppose we have the following sets of numbers. We want to create a Neural -Network that can understand the relationship between them.

As you may have noticed yourself that relationship is defined by the following function: y=3x-7.

As for every machine learning application, the first step is to gather the data: so let’s save these the values for the x and y in two different numpy arrays.

# Import numpy
import numpy as np
# y = 3x -7
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
y = np.array([-10.0, -7.0, -4.0, -1.0, 2.0, 5.0, 7.0], dtype=float)

Creating the simplest Neural Network:

The simplest NN we can create has just 1 input, 1 output and 1 hidden layer with just one node.

In order to create such model using TensorFlow, we first define the Neural Network as Sequential, meaning that it will have a sequence of layers, then we just add to it a dense layer which will represent our hidden layer

from tensorflow.keras.layers import Densemodel = tf.keras.Sequential([Dense(units=1, input_shape=[1])])

Once the model is created, losses and metrics can be configured with model.compile(), the model can be trained with model.fit(), evaluated with model.evaluate() and used to do prediction with model.predict().

model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=500)
print(model.predict([25.0]))

The value printed is not 68 but 67.997734, why so? Because the Neural networks deal with probabilities, given the seven point fed into the model, it understood that the relation between x and y is, with high probbility, y =3x-7, but seven points are not enough to be sure, so it predicts that the point 25 is very close to 68, but not necessarily 68.

Full code

Here you can see the full code or try it through Google Colab:

What’s next?

In the next article I’ll show how to develop a more complex Neural Network for image classfication and how to use Google Colab to speed-up the training process.

MLearning.ai

Data Scientists must think like an artist when finding a solution

Sign up for AI & ART

By MLearning.ai

A weekly collection of the best news and resources on AI & ART Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

MLearning.ai

Data Scientists must think like an artist when finding a solution, when creating a piece of code.Artists enjoy working on interesting problems, even if there is no obvious answer.

Luca Barone

Written by

APPLE Develope Academy Student, love travelling, love learning, love languages (Ita, Eng, Spa, Jap)

MLearning.ai

Data Scientists must think like an artist when finding a solution, when creating a piece of code.Artists enjoy working on interesting problems, even if there is no obvious answer.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store