“Deep Learning Basics: A Step-by-Step Guide with MNIST Dataset”

Hey there, fellow tech enthusiasts! Today we’re diving into an exciting topic that’s been shaking up the world of AI—deep learning. If you’ve ever wondered how machines can recognize handwritten digits like a pro, stick around because we’ll break it all down step-by-step. Grab your favorite beverage, and let’s get started!

So, what exactly is deep learning? Picture it as a turbocharged version of machine learning. Deep learning models mimic the way our brains work by processing information through layers of artificial neurons. These models can learn intricate patterns by adjusting themselves over time, kind of like how we get better at a skill with practice.

Understanding the MNIST Dataset
Before we dive into building our neural network, let’s first acquaint ourselves with the MNIST dataset. Think of it as the “Hello, World!” of deep learning. This dataset contains 70,000 images of handwritten digits from 0 to 9. Each image is 28×28 pixels in grayscale. We’ll be using 60,000 images for training and 10,000 for testing.

Step 1: Importing the Required Libraries
First things first, let’s import the libraries we need. These will help us manipulate data, visualize it, and build our neural network.

“`python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
“`
In a nutshell:
numpy and pandas are our go-to for data manipulation.
matplotlib and seaborn make our data visuals pop.
tensorflow and keras are the real MVPs for our deep learning model.

Step 2: Loading the Dataset
Loading the MNIST dataset is a breeze with Keras.

“`python
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
“`
This command fetches the MNIST data and splits it into training and test sets.

Step 3: Inspecting the Dataset
Let’s take a sneak peek at the data.

“`python
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
“`
And how about visualizing one of those handwritten digits?

“`python
plt.imshow(X_train[2], cmap=’gray’)
plt.show()
print(y_train[2])
“`
Visualizing data helps us understand it better. The third training image and its label, anyone?

Step 4: Rescaling the Dataset
Neural networks love normalized data, so we’ll rescale the pixel values from 0-255 to 0-1.

“`python
X_train = X_train / 255
X_test = X_test / 255
“`
This helps our model learn more efficiently.

Step 5: Reshaping the Dataset
Our neural net expects input as a flat vector, not a 2D image, so let’s reshape the data.

“`python
X_train = X_train.reshape(len(X_train), 28 * 28)
X_test = X_test.reshape(len(X_test), 28 * 28)
“`
Step 6: Building Our First ANN Model
Here’s where the magic happens! We’ll start with a simple neural network: one input layer and one output layer.

“`python
ANN1 = keras.Sequential([
keras.layers.Dense(10, input_shape=(784,), activation=’sigmoid’)
])
“`
Next, let’s compile our model.

“`python
ANN1.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])
“`
Now we’re ready to train our model!

“`python
ANN1.fit(X_train, y_train, epochs=5)
“`
Step 7: Evaluating the Model
How well does our model perform on the test data?

“`python
ANN1.evaluate(X_test, y_test)
“`
Step 8: Making Predictions
Let’s put our model to the test with some predictions.

“`python
y_predicted = ANN1.predict(X_test)
print(np.argmax(y_predicted[10]))
print(y_test[10])
“`
And voila! Compare the predicted digit to the actual label.

Step 9: Building a More Complex ANN Model
Why stop there? Let’s beef up our model with a hidden layer of 150 neurons using the ReLU activation function.

“`python
ANN2 = keras.Sequential([
keras.layers.Dense(150, input_shape=(784,), activation=’relu’),
keras.layers.Dense(10, activation=’sigmoid’)
])
“`
Compile and train the improved model in the same way:

“`python
ANN2.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])
ANN2.fit(X_train, y_train, epochs=5)
“`
Step 10: Evaluating the Improved Model
Let’s see how our enhanced model performs on the test set.

“`python
ANN2.evaluate(X_test, y_test)
“`
Step 11: Confusion Matrix
Finally, a confusion matrix helps us dig deeper into our model’s performance.

“`python
y_predicted2 = ANN2.predict(X_test)
y_predicted_labels2 = [np.argmax(i) for i in y_predicted2]

cm = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels2)

plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt=’d’)
plt.xlabel(“Predicted”)
plt.ylabel(“Actual”)
plt.show()
“`
And there you have it! We’ve constructed, trained, and evaluated a neural network model using the MNIST dataset. Plus, we improved it by adding a hidden layer and using a more effective activation function.

I hope you enjoyed this journey through the basics of deep learning. Eager to experiment? Try tweaking parameters or even exploring other datasets.

Feel free to share your experiences and ideas in the comments below. And don’t forget to check out my GitHub for more AI goodies or connect with me on LinkedIn!

Happy coding! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *