Deep Learning with Python: Build Neural Networks Using TensorFlow and Keras

← Back to Home

Part 5: Deep Learning and Neural Networks with TensorFlow and Keras



What Is Deep Learning?

Deep Learning is a subfield of machine learning that uses artificial neural networks—inspired by the human brain—to recognize patterns and make decisions.

It's especially effective in handling:

  • Images
  • Audio
  • Text
  • Complex data with high dimensionality


What Is a Neural Network?

A neural network is made up of layers of interconnected "neurons":

  • Input Layer – takes in raw data (e.g., pixels)
  • Hidden Layers – extract patterns using weights and activation functions
  • Output Layer – makes predictions (e.g., class label)


Setting Up TensorFlow and Keras

Install TensorFlow (Keras is included):

pip install tensorflow

Project: Image Classification with MNIST Dataset

The MNIST dataset is a set of 70,000 handwritten digits (0–9), perfect for beginners.



✅ Step 1: Load Data

import tensorflow as tf
from tensorflow.keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()


Step 2: Preprocess Data

# Normalize pixel values to [0, 1]
X_train = X_train / 255.0
X_test = X_test / 255.0


Step 3: Build the Neural Network

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),   # Input layer
    tf.keras.layers.Dense(128, activation='relu'),   # Hidden layer
    tf.keras.layers.Dense(10, activation='softmax')  # Output layer
])


Step 4: Compile the Model

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


Step 5: Train the Model

model.fit(X_train, y_train, epochs=5)


Step 6: Evaluate Performance

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


Step 7: Make Predictions

predictions = model.predict(X_test)
import numpy as np

# Predict and show the first test digit
print("Predicted digit:", np.argmax(predictions[0]))


💡 Practice Challenge

Try changing the network architecture:

  • Add another hidden layer
  • Use different activation functions (sigmoid, tanh)
  • Increase or decrease the number of neurons
# Add more layers and experiment


🎓 What You’ve Learned:

  • What neural networks are and how they work
  • How to build, train, and evaluate a deep learning model using Keras
  • How to classify images with high accuracy


🧭 What’s Next?

In Part 6, we’ll explore Natural Language Processing (NLP) using Python. You’ll learn how to process text, analyze sentiment, and even build a basic chatbot.