DEV Community

Cover image for Unlocking the Magic: My First ML Project – Handwritten Digit Recognition with MNIST ✨
Aryan Chauhan
Aryan Chauhan

Posted on

Unlocking the Magic: My First ML Project – Handwritten Digit Recognition with MNIST ✨

Ever felt that swirl of intimidation and excitement looking at Machine Learning? That feeling of "I really want to get into this, but where do I even begin?"

Well, I've been there, and I just crossed a major milestone: building my first machine learning model! And let me tell you, watching it "learn" to read handwritten digits was nothing short of magical. If you're looking for the perfect entry point into ML, strap in, because I'm about to share my journey with the legendary MNIST dataset.


What's This "MNIST" Everyone's Talking About?

Imagine a vast collection of tiny, grayscale images, each showing a single handwritten digit from 0 to 9. That's MNIST!

It's the "Hello World" of image classification datasets, and for good reason:

  • Size: 60,000 training images, 10,000 test images. Just enough to be meaningful, not overwhelming.
  • Simplicity: All images are a neat 28x28 pixels.
  • Cleanliness: Hardly any messy data to wrestle with, so you can focus on the ML concepts.

It's small, clean, and absolutely perfect for beginners who want to see quick results.


My Humble Goal: Pixel to Prediction

My objective was clear:

  1. Feed the model an image of a handwritten digit.
  2. Have the model figure out what features define each digit (e.g., a loop for '0', a vertical line for '1').
  3. Get it to confidently tell me the correct number.

Building My First Neural Network: A Simple Keras Setup

I wanted to keep things approachable, so I opted for TensorFlow with Keras. Keras is a high-level API that makes building neural networks feel almost like stacking Lego blocks.

My model was deliberately simple, but incredibly effective:

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    # Step 1: Flatten the 28x28 image into a 784-length vector
    layers.Flatten(input_shape=(28, 28)),

    # Step 2: A 'Dense' (fully connected) hidden layer with ReLU activation
    # ReLU helps the model learn complex, non-linear relationships
    layers.Dense(128, activation='relu'), # You can experiment with this number!

    # Step 3: The output layer - 10 neurons for 0-9, with Softmax for probabilities
    layers.Dense(10, activation='softmax')
])

model.summary()
Enter fullscreen mode Exit fullscreen mode

Quick breakdown of those layers:

  • Flatten: Our 2D (28x28) image needs to be "unrolled" into a single, long list of numbers (784 pixels) for the next layer. Think of it like taking a grid of numbers and laying them out in a single line.
  • Dense (with ReLU): This is a "hidden" layer. Every input pixel connects to every neuron in this layer. The ReLU (Rectified Linear Unit) activation function introduces non-linearity, which is crucial for the network to learn anything interesting.
  • Dense (Output with Softmax): This is the final decision-making layer. It has 10 neurons, one for each digit. Softmax takes the raw outputs and turns them into probabilities that sum up to 1. The highest probability tells us the model's prediction!

Feeding the Brain: Training My Model

With the architecture set, it was time for the actual "learning":

  • Dataset: MNIST (pre-loaded in Keras, making life easy!)
  • Epochs: 5
  • Batch Size: 32

These terms can be a bit opaque at first, right? Here's my beginner-friendly take:

  • Epochs: How many times our model "sees" the entire training dataset. Each epoch is a full pass. So, 5 epochs means it went through all 60,000 images five times.
  • Batch Size: Instead of showing the model one image at a time, or all 60,000 at once (which would kill your memory!), we feed it images in small groups. My model processed 32 images at a time, updated its internal "knowledge" (weights) based on those 32, then moved to the next batch. This balances speed and stability.

The "Aha!" Moment: My Results!

After just those 5 epochs, I ran the model on the unseen test set (those 10,000 images it had never encountered). The accuracy shot up to an astounding 97-98%!

Honestly, watching the accuracy climb with each epoch during training was incredibly satisfying. It genuinely felt like my code was coming alive and "understanding" those squiggly numbers. That's the magic, right there! ✨


My Top Takeaways for Aspiring ML Enthusiasts

If you're just starting, here's what I learned that might save you some headaches:

  1. Start with the "Hello Worlds": Don't jump straight into massive, complex datasets. Small, clean datasets like MNIST let you grasp core concepts without drowning in data preprocessing.
  2. Don't Obsess Over Hyperparameters (Yet): It's tempting to tweak everything, but for your first few projects, common defaults or small numbers for epochs/batch size are usually fine. Get it working, then optimize!
  3. Embrace the Learning Curve (and the Wins!): ML can feel daunting, but celebrate every small victory. Watching that accuracy metric improve? Pure dopamine!
  4. A Little Code Goes a Long Way: Even understanding the basic structure of a model in Keras or PyTorch is a huge step. You don't need to write a million lines of code to get started.

What's Next on My ML Adventure?

This project has officially hooked me! My next steps include:

  • Convolutional Neural Networks (CNNs): These are the true kings of image recognition. I'm excited to see how much more accurate I can get with a CNN on MNIST, and then move to more complex image tasks.
  • Data Augmentation: Making my model more robust by artificially creating more training data (e.g., rotating, zooming, or shifting existing images).
  • Harder Datasets: Time to tackle something like CIFAR-10 (which has 10 classes of real-world objects like cars, planes, and animals) to push my skills further.

👉 My biggest piece of advice: If you're curious about ML, dive into MNIST. It's accessible, fun, and incredibly rewarding. You'll go from pixel-perfect confusion to a confident predictor in no time!

Have you done the MNIST project? What was your first ML "aha!" moment? Share your experiences, tips, or even links to your code in the comments below! Let's learn together! 👇

Top comments (0)