Training a Deep Network in TensorFlow

Learn how to build and train your first deep neural network using TensorFlow and Keras with clear, step-by-step guidance on the MNIST dataset.

🔰 beginner
⏱️ 60 minutes
👤 SuperML Team

· Deep Learning · 2 min read

📋 Prerequisites

  • Basic Python knowledge
  • Basic understanding of neural networks

🎯 What You'll Learn

  • Set up TensorFlow and Keras for deep learning
  • Build and train a deep neural network on MNIST
  • Monitor loss and accuracy during training
  • Evaluate your model effectively

Introduction

In this tutorial, you will learn how to build and train your first deep neural network using TensorFlow and Keras to classify handwritten digits using the MNIST dataset.


1️⃣ Setting Up TensorFlow

If you haven’t installed TensorFlow yet, run:

pip install tensorflow

2️⃣ Import Libraries

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

3️⃣ Load and Prepare the MNIST Dataset

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

# Normalize pixel values to [0, 1]
x_train, x_test = x_train / 255.0, x_test / 255.0

4️⃣ Build Your Deep 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)

You will see the loss and accuracy printed for each epoch, allowing you to monitor progress.


7️⃣ Evaluate the Model

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

Conclusion

✅ You have successfully built and trained your first deep neural network in TensorFlow.
✅ You learned how to preprocess data, build the model, train, and evaluate it.
✅ You now have the foundation to explore more complex architectures confidently.


What’s Next?

✅ Try adding more layers or neurons to observe accuracy changes.
✅ Experiment with different activation functions and optimizers.
✅ Continue learning with Convolutional Neural Networks (CNNs) to enhance image classification.


Join the SuperML Community to share your progress and get help while learning.


Happy Learning with TensorFlow! 🚀

Part of a structured course

Beginner Deep Learning

Start your deep learning journey with fundamentals, basic neural networks, and your first deep learning models

Lesson 18 of 19 ⏱ 10 weeks beginner Free

Related Tutorials

🔰beginner ⏱️ 45 minutes

Your First Deep Learning Implementation

Build your first deep learning model to classify handwritten digits using TensorFlow and Keras, explained step-by-step for beginners.

Deep Learning2 min read
deep learningbeginnerkeras +2
🔰beginner ⏱️ 60 minutes

Training a Deep Network in PyTorch

Learn how to build and train your first deep neural network using PyTorch with a clear, step-by-step example on the MNIST dataset.

Deep Learning2 min read
deep learningpytorchbeginner +1
🔰beginner ⏱️ 50 minutes

Artificial Neural Networks

Learn what artificial neural networks are, how they work, and why they form the foundation of modern deep learning.

Deep Learning2 min read
deep learningartificial neural networksmachine learning +1
🔰beginner ⏱️ 30 minutes

Activation Functions in Deep Learning

Learn what activation functions are, why they are important in deep learning, and explore commonly used activation functions with clear, beginner-friendly explanations

Deep Learning2 min read
deep learningactivation functionsbeginner +1