Press ESC to exit fullscreen
πŸ“– Lesson ⏱️ 150 minutes

Your First Deep Learning Model

Build and train your first complete neural network

Introduction

In this tutorial, you will build your first deep learning model to classify handwritten digits using the MNIST dataset with TensorFlow and Keras.


What is MNIST?

MNIST is a dataset of 28x28 grayscale images of handwritten digits (0-9), commonly used for getting started with image classification.


Step-by-Step Guide

1️⃣ Install Required Libraries

If you don’t have TensorFlow installed:

pip install tensorflow

2️⃣ Import Libraries

import tensorflow as tf
from tensorflow.keras import layers, models

3️⃣ Load and Prepare the Dataset

# Load dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize pixel values
x_train, x_test = x_train / 255.0, x_test / 255.0

4️⃣ Build Your First Neural Network

model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

5️⃣ Compile the Model

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

6️⃣ Train the Model

model.fit(x_train, y_train, epochs=5, validation_split=0.1)

7️⃣ Evaluate on Test Data

test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"Test accuracy: {test_acc}")

Summary

βœ… You built and trained your first deep learning model using Keras.
βœ… You learned how to preprocess data, build, compile, train, and evaluate a neural network.
βœ… You can now confidently experiment with different datasets and models.


What’s Next?

βœ… Try adjusting the number of layers and neurons to see the impact on accuracy.
βœ… Experiment with different activation functions and optimizers.
βœ… Move on to Convolutional Neural Networks (CNNs) for improved image classification.


Join our SuperML Community to share your first project, get feedback, and continue your deep learning journey.


Happy Building! πŸš€