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.


No comments:

Post a Comment

Featured Post

Solution MongoDB document Modelling Assignment

 Here's the complete solution to your MongoDB document modeling assignment, including: ✅ Sample documents for students and courses ...

Popular Posts