Diving into Deep Learning with a Simple Example(with step by step code)

Naethreepremnath
Python’s Gurus
Published in
7 min readMay 7, 2024

To all those data science enthusiasts who are just starting off, this deep learning example will be a good starting point to dip your toes into the world of deep learning.

Deep learning is a subset of machine learning and artificial intelligence that focuses on building and training neural networks to learn from data representations.

Photo by Pietro Jeng on Unsplash

My objective for this article is to apply deep learning to the famous Iris Species dataset which you can find on Kaggle. At the end you will understand on how to build a deep learning model to classify a flower into one of its species Setosa, Versicolor or Virginica.

Make sure you install the necessary libraries first, if you haven’t already. This might take some time.

!pip install pandas
!pip install tensorflow
!pip install scikit-learn
!pip install matplotlib

Now import the packages that we will be using.

import sklearn
import pandas as pd
import os
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow import keras

Prepare Input Data

To start off, the basic step is to import your dataset. You can find the dataset here. Make sure you download the dataset into the same folder as your notebook.

iris_data = pd.read_csv("iris.csv")

Take a look at the data now

print(iris_data.head(5))

You can see the first 5 records.

Now note that the target variable species is in text and we want to convert it to numeric. So let’s encode it.

from sklearn import preprocessing
label_encoder = preprocessing.LabelEncoder()
iris_data['Species'] = label_encoder.fit_transform(iris_data['Species'])

Now this is what the data frame looks like:

From the above you can see setosa is encoded as 0. Note that versicolor is coded as 1 and virginica is coded as 2.

Now, we will convert the dataframe into a numpy array since it the preferred input format for Keras.

np_iris = iris_data.to_numpy()

Next, we will separate the feature variables and the target variables. We will also print the first 5 rows of the feature variables to see how they look.

X_data = np_iris[:,0:4]
Y_data=np_iris[:,4]

print(X_data[:5,:])

The output below shows that the values of the 4 features Sepal.Length, Sepal.Width, Petal.Length and Petal.Width contains values in different scales.

So let’s standardize the scale now.

#Create a scaler model that is fit on the input data.
scaler = StandardScaler().fit(X_data)

#Scale the numeric feature variables
X_data = scaler.transform(X_data)

Now, let’s consider our target variable. It has 3 classes/species. Hence let’s use one hot encoding to create a column for each of the species

If Y_data contains the integer 0, it will be converted to [1, 0, 0].
If Y_data contains the integer 1, it will be converted to [0, 1, 0].
If Y_data contains the integer 2, it will be converted to [0, 0, 1].

Y_data = tf.keras.utils.to_categorical(Y_data,3)

Now, time to split the data. Generally we will be splitting to train, validation and test set. But Keras has a feature to create a validation set from training set which you will see later in this article. Hence, we shall split to train and test set in the ratio of 9:1.

X_train,X_test,Y_train,Y_test = train_test_split( X_data, Y_data, test_size=0.10)

Let’s check the dimensions now:

print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)

The output is:

Now time to create the model

Creating a model

Creating a Deep learning model requires you to define some hyperparameters like No. of layers, No. of nodes, the activation function, cost function, learning rate, optimizer, batch size, epoch etc.

In this example, some parameters are defined, others which are not defined are assumed to take the default value.

Now let’s initialize a sequential model, which is a linear stack of layers where we can add the layer one by one.

model = tf.keras.models.Sequential()

Next, I want to add hidden layers. The no. of hidden layers depend on different factors and usually is in the count of 2^n. Here I am choosing to add 2 hidden layers.

#Add the first hidden layer
model.add(keras.layers.Dense(128, #Number of nodes
input_shape=(4,), #Number of inputs
name='Hidden-Layer-1', #Logical name
activation='relu')) #activation function

#Add a second hidden layer
model.add(keras.layers.Dense(128, #Number of nodes
name='Hidden-Layer-2', #Logical name
activation='relu')) #activation function

You might wonder how the above parameters are chosen. Usually it is chosen by intuition or by past references or by best practices. However one can choose to do hyperparameter tuning to choose the best parameters, but in this article we won’t be considering it.

Now let’s add the output layer.

model.add(keras.layers.Dense(3,               #Number of classes in target
name='Output-Layer', #Logical name
activation='softmax')) #activation function

Time to compile the model. Since this is a multi-class classification we will use categorical cross entropy as the loss function and the metric as accuracy.

model.compile(loss='categorical_crossentropy', metrics=['accuracy'])

Shall we go ahead look at the summary of the model?

model.summary()

Training and evaluating the Model

Let’s fit the model. Note that here history contains information about the training process, such as the loss and accuracy metrics recorded at each epoch.

history=model.fit(X_train,
Y_train,
batch_size=16,
epochs=10,
verbose=1,
validation_split=0.2)

This will perform the entire training cycle, including forward propagation, loss computation, backward propagation and gradient descent.
It will execute for the specified batch sizes and epoch and perform validation after each epoch.

NOTE: These parameters are chosen intuitively, however you can tune them and change them accordingly. Also note usually we would like to do the splitting of validation to be 0.1. However here since the total no. of records is just 150, we chose 0.2.

Now let’s visualize and see how the accuracy changes with epochs.

pd.DataFrame(history.history)["accuracy"].plot(figsize=(8, 5))
plt.title("Accuracy improvements with Epoch")
plt.show()

From the above graph we can see that the accuracy is close to 0.9 which is good. However if we get lower accuracy, we can increase the epochs to help improve the accuracy in some cases.

Now let’s evaluate our model against the test set.

model.evaluate(X_test,Y_test)

The output is:

We can see that the model is performing well considering the high accuracy value.

Finally, let’s save our model. Remember that the training and inference environments are usually separate. Models need to be saved after they are validated. Note that Hierarchical Data Format (HDF5) format is a common format for storing deep learning models in Keras.

model.save("iris_save.h5")

Predictions using the Model

Let’s give an arbitrary set of values for the 4 features Sepal.Length, Sepal.Width, Petal.Length and Petal.Width.

Make sure that the values undergo standardizing just like how the training data did.

prediction_input = [[7.4, 3.2 , 4.5, 1.4]]
scaled_input = scaler.transform(prediction_input)

Now, lets get the raw prediction probabilities

raw_prediction = model.predict(scaled_input)
print("Raw Prediction Output (Probabilities):" , raw_prediction)

The output is:

From the above, we can see that 2nd probability is the highest. Let’s now use the reversing of the label encoding to get the species of the flower.

prediction = np.argmax(raw_prediction) #gives index of max probability
print("Prediction is ", label_encoder.inverse_transform([prediction]))

The output is:

Hurray! We did it! We have now predicted the species of a flower based on their 4 features Sepal.Length, Sepal.Width, Petal.Length and Petal.Width.

We have now come to the end of this simple project. We’ve taken a thrilling dive into the world of deep learning, crafting our very own model and unleashing its predictive power. But guess what? This journey has only just begun! Keep riding this wave of curiosity and exploration and stay tuned for more fun adventures!

Python’s Gurus🚀

Thank you for being a part of the Python’s Gurus community!

Before you go:

  • Be sure to clap x50 time and follow the writer ️👏️️
  • Follow us: Newsletter
  • Do you aspire to become a Guru too? Submit your best article or draft to reach our audience.

--

--

Naethreepremnath
Python’s Gurus

BSc(Hons) in Data Science, University of Colombo (Reading) | Public Speaker | Writer