Get Started With Neural Networks Practically Using TensorFlow; | by Abbas Ali | Apr, 2024


Written for Beginners.

Photo by Growtika on Unsplash

Hey there!

Today, I started reading a new book called, “AI and ML for Coders” by Laurence Moroney.

I am writing this article to share with you what I learned in the first chapter of the book. It’s mind-blowing. The author explained how neural networks work in a very simple way.

After reading this article you will have a good beginner-level understanding to get started with Deep Learning.

Let’s get started.

Open your Google Colab and code along.

First import TensorFlow.

import tensorflow as tf
tf.__version__ # 2.15.0

Now, I am going to create two simple lists one with feature values and the other with label values.

X = [-1, 0, 1, 2, 3, 4]
y = [-3, -1, 1, 3, 5, 7]

From seeing the above two lists can you find any pattern?

If you look carefully you will see that y = 2X-1.

For example, see the index 0: -3 = 2(-1) -1

see index 1: -1 = 2(0) -1

and so on.

After looking carefully we were able to find the pattern between the feature(X) and the label(y). Now let’s create a model that will find this pattern then try different values and see how it predicts (for example, if X = 10 then y should be y = 2(10)-1 which is 19)

We are going to create a neural network with just one neuron using TensorFlow and then we will feed the data to the neural network before that we need to convert the data into an NumPy array.

Let’s start by importing the necessary libraries. If you are not able to understand any part of the code, don’t worry you will understand it little by little. The aim of this article is to give you a basic knowledge of neural networks.

import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

Now we are going to create the neural network(Model).

model = Sequential([Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')

Let me explain the above two lines.

We need Sequential to define our layers. You might have seen the diagram of neural networks many times which looks something like the below one.

ANN

You can imagine Sequential as a cupboard. Inside the cupboard, we have many stands they are the layers, and on stands, we will keep our cups you can think of them as neurons, let’s say in the first stand there are 3 cups(3 neurons see the above diagram) in the second stand there are 5 cups and in the last stand there is 1 cup.

There are many types of layers we can create but in this example, we have created a Dense layer. When the neurons in one layer get connected to all the neurons in the next layers it is known as a Dense Layer.

In the above diagram, the first layer is a dense layer because all the neurons in that layer are connected to all the neurons in the second layer.

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

The above code says we created a Dense layer with one neuron (because the parameter units=1, if you give units=4 then we will have 4 neurons).

We also specified the type of input the neural network will receive using the input_shape parameter.

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

Now, we compiled our model using an optimizer and a loss function.

Let me explain how these two work.

First, the model makes a random prediction. For example, it may say the pattern the model randomly chooses is y = 12X-3 (But we know it is y=2X-1).

Then it compares its prediction with the original prediction and finds the loss using the loss function which is “mean_squared_error” in our case. For example, for X = -1 the model will predict y as y = 12(-1)-1 which is -13 but the original value is -3.

Now the loss function will find the mean squared error between -13 and -3 and then using the loss our optimizer will optimize the pattern from y = 12X -3 to something else let’s say y = 9X-2.

This process will go on and on until the loss is so small.

The optimizer we used in this example is Stochastic Gradient Descent (SGD).

Let’s make a prediction using our model.

Before that, we need to convert our data into an NumPy array.

X = np.array(X)
y = np.array(y)

Fit the model to the data.

model.fit(X, y, epochs=500)

epochs = 500 means the model will go through 500 iterations in each iteration the loss will keep on decreasing.

Now, I am going to pass the X=12, and let’s see what our model predicts. The output should be y = 2(12)-1 = 23

model.predict([12])
# OUTPUT:
array([[22.976109]], dtype=float32)

Nice, our model has learned the pattern correctly. It says for X=12, y is 22.9761.

We should not expect 23 because we have just trained the model with 5 instances.

That’s it.

I hope it was useful.

P.S. You can connect with me on X and YouTube.

X: AbbasAli_X

YouTube: Abbas_Ali





Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*