Building Your First Deep Learning Model: A Step-by-Step Guide

Introduction to Deep Learning

Deep learning is a subset of machine learning, which itself is a subset of artificial intelligence (AI). Deep learning models are inspired by the structure and function of the human brain and are composed of layers of artificial neurons. These models are capable of learning complex patterns in data through a process called training, where the model is iteratively adjusted to minimize errors in its predictions.

In this blog post, we will walk through the process of building a simple artificial neural network (ANN) to classify handwritten digits using the MNIST dataset.

Understanding the MNIST Dataset

The MNIST dataset (Modified National Institute of Standards and Technology dataset) is one of the most famous datasets in the field of machine learning and computer vision. It consists of 70,000 grayscale images of handwritten digits from 0 to 9, each of size 28×28 pixels. The dataset is divided into a training set of 60,000 images and a test set of 10,000 images. Each image is labeled with the corresponding digit it represents.

Downloading the Dataset

We will use the MNIST dataset provided by the Keras library, which makes it easy to download and use in our model.

Step 1: Importing the Required Libraries

Before we start building our model, we need to import the necessary libraries. These include libraries for data manipulation, visualization, and building our deep learning model.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
  • numpy and pandas are used for numerical and data manipulation.
  • matplotlib and seaborn are used for data visualization.
  • tensorflow and keras are used for building and training the deep learning model.

Step 2: Loading the Dataset

The MNIST dataset is available directly in the Keras library, making it easy to load and use.

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

This line of code downloads the MNIST dataset and splits it into training and test sets:

  • X_train and y_train are the training images and their corresponding labels.
  • X_test and y_test are the test images and their corresponding labels.

Step 3: Inspecting the Dataset

Let’s take a look at the shape of our training and test datasets to understand their structure.

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
  • X_train.shape outputs (60000, 28, 28), indicating there are 60,000 training images, each of size 28×28 pixels.
  • X_test.shape outputs (10000, 28, 28), indicating there are 10,000 test images, each of size 28×28 pixels.
  • y_train.shape outputs (60000,), indicating there are 60,000 training labels.
  • `y_test

.shapeoutputs(10000,)`, indicating there are 10,000 test labels.

To get a better understanding, let’s visualize one of the training images and its corresponding label.

plt.imshow(X_train[2], cmap='gray')
plt.show()
print(y_train[2])
  • plt.imshow(X_train[2], cmap=’gray’) displays the third image in the training set in grayscale.
  • plt.show() renders the image.
  • print(y_train[2]) outputs the label for the third image, which is the digit the image represents.

Step 4: Rescaling the Dataset

Pixel values in the images range from 0 to 255. To improve the performance of our neural network, we rescale these values to the range [0, 1].

X_train = X_train / 255
X_test = X_test / 255

This normalization helps the neural network learn more efficiently by ensuring that the input values are in a similar range.

Step 5: Reshaping the Dataset

Our neural network expects the input to be a flat vector rather than a 2D image. Therefore, we reshape our training and test datasets accordingly.

X_train = X_train.reshape(len(X_train), 28 * 28)
X_test = X_test.reshape(len(X_test), 28 * 28)
  • X_train.reshape(len(X_train), 28 * 28) reshapes the training set from (60000, 28, 28) to (60000, 784), flattening each 28×28 image into a 784-dimensional vector.
  • Similarly, X_test.reshape(len(X_test), 28 * 28) reshapes the test set from (10000, 28, 28) to (10000, 784).

Step 6: Building Our First ANN Model

We will build a simple neural network with one input layer and one output layer. The input layer will have 784 neurons (one for each pixel), and the output layer will have 10 neurons (one for each digit).

ANN1 = keras.Sequential([
keras.layers.Dense(10, input_shape=(784,), activation='sigmoid')
])
  • keras.Sequential() creates a sequential model, which is a linear stack of layers.
  • keras.layers.Dense(10, input_shape=(784,), activation=’sigmoid’) adds a dense (fully connected) layer with 10 neurons, input shape of 784, and sigmoid activation function.

Next, we compile our model by specifying the optimizer, loss function, and metrics.

ANN1.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
  • optimizer=’adam’ specifies the Adam optimizer, which is an adaptive learning rate optimization algorithm.
  • loss=’sparse_categorical_crossentropy’ specifies the loss function, which is suitable for multi-class classification problems.
  • metrics=[‘accuracy’] specifies that we want to track accuracy during training.

We then train the model on the training data.

ANN1.fit(X_train, y_train, epochs=5)
  • ANN1.fit(X_train, y_train, epochs=5) trains the model for 5 epochs. An epoch is one complete pass through the training data.

Step 7: Evaluating the Model

After training the model, we evaluate its performance on the test data.

ANN1.evaluate(X_test, y_test)
  • ANN1.evaluate(X_test, y_test) evaluates the model on the test data and returns the loss value and metrics specified during compilation.

Step 8: Making Predictions

We can use our trained model to make predictions on the test data.

y_predicted = ANN1.predict(X_test)
  • ANN1.predict(X_test) generates predictions for the test images.

To see the predicted label for the first test image:

print(np.argmax(y_predicted[10]))
print(y_test[10])
  • np.argmax(y_predicted[10]) returns the index of the highest value in the prediction vector, which corresponds to the predicted digit.
  • print(y_test[10]) prints the actual label of the first test image for comparison.

Step 9: Building a More Complex ANN Model

To improve our model, we add a hidden layer with 150 neurons and use the ReLU activation function, which often performs better in deep learning models.

ANN2 = keras.Sequential([
keras.layers.Dense(150, input_shape=(784,), activation='relu'),
keras.layers.Dense(10, activation='sigmoid')
])
  • keras.layers.Dense(150, input_shape=(784,), activation=’relu’) adds a dense hidden layer with 150 neurons and ReLU activation function.

We compile and train the improved model in the same way.

ANN2.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ANN2.fit(X_train, y_train, epochs=5)

Step 10: Evaluating the Improved Model

We evaluate the performance of our improved model on the test data.

ANN2.evaluate(X_test, y_test)

Step 11: Confusion Matrix

To get a better understanding of how our model performs, we can create a confusion matrix.

y_predicted2 = ANN2.predict(X_test)
y_predicted_labels2 = [np.argmax(i) for i in y_predicted2]
  • y_predicted2 = ANN2.predict(X_test) generates predictions for the test images.
  • y_predicted_labels2 = [np.argmax(i) for i in y_predicted2] converts the prediction vectors to label indices.

We then create the confusion matrix and visualize it.

cm = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels2)
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
  • tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels2) generates the confusion matrix.
  • sns.heatmap(cm, annot=True, fmt=’d’) visualizes the confusion matrix with annotations.
CONFUSION MATRIX
SEABORN — HEATMAP

Conclusion

In this blog post, we covered the basics of deep learning and walked through the steps of building, training, and evaluating a simple ANN model using the MNIST dataset. We also improved the model by adding a hidden layer and using a different activation function. Deep learning models, though seemingly complex, can be built and understood step-by-step, enabling us to tackle various machine learning problems.

This brings us to the end of this article. I hope you have understood everything clearly. Make sure you practice as much as possible.

If you wish to check out more resources related to Data Science, Machine Learning and Deep Learning you can refer to my Github account.

You can connect with me on LinkedIn — RAVJOT SINGH.

I hope you like my article. From a future perspective, you can try other algorithms also, or choose different values of parameters to improve the accuracy even further. Please feel free to share your thoughts and ideas.

P.S. Claps and follows are highly appreciated.


Building Your First Deep Learning Model: A Step-by-Step Guide was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

Smart Factories: Concepts and Features

Exploring how new technologies, including artificial intelligence (AI), revolutionize manufacturing processes.

A smart factory is a cyber-physical system that leverages advanced technologies to analyze data, automate processes, and learn continuously. It’s part of the Industry 4.0 transformation, which combines digitalization and intelligent automation. Here are some key features:

  1. Interconnected Network: Smart factories integrate machines, communication mechanisms, and computing power. They form an interconnected ecosystem where data flows seamlessly.
  2. Advanced Technologies: Smart factories use AI, machine learning, and robotics to optimize operations. These technologies enable real-time decision-making and adaptability.
  3. Data-Driven Insights: Sensors collect data from equipment, production lines, and supply chains. AI processes this data to improve efficiency, quality, and predictive maintenance.
Smart Factory leverages advanced technologies to analyze data, automate processes, and learn continuously.

Automation, Robots, and AI on the Factory Floor

1. Production Automation

  • Robotic Arms: Robots handle repetitive tasks like assembly, welding, and material handling. They enhance precision and speed.
  • Collaborative Robots: These work alongside humans, assisting with tasks like packaging, quality control, and logistics.

2. Quality Inspection

3. IoT + AI: Predictive Maintenance and Energy Efficiency

  • Predictive Maintenance (IoT Sensors): Connected sensors monitor equipment health. AI algorithms predict failures, allowing timely maintenance. This minimizes unplanned downtime and reduces costs.
  • Energy Management and Energy Consumption Analysis: AI analyzes vast data sets to optimize energy usage. It helps reduce waste, manage various energy sources, and enhance sustainability.
  • Predictive Energy Demand: AI predicts energy demand patterns, aiding efficient resource allocation.
AI turning IoT Data into Information: predictive maintenance, automated quality inspection, optimized energy consumption, etc.

AI-Driven Energy Management in Smart Factories

1. Real-Time Energy Optimization

  • IoT Data Integration: Smart factories deploy IoT sensors across their infrastructure to collect real-time data on energy consumption. These sensors monitor machinery, lighting, HVAC systems, and other energy-intensive components.
  • Weather Forecast Integration: By combining IoT data with weather forecasts, AI algorithms predict energy demand variations. For example: when a heatwave is predicted, the factory can pre-cool the facility during off-peak hours to reduce energy costs during peak demand.

2. Dynamic Energy Source Selection

  • Production Schedules and Energy Sources: AI analyzes production schedules, demand patterns, and energy prices. It optimally selects energy sources (e.g., solar, grid, and battery storage) based on cost and availability. For example: during high-demand production hours, the factory might rely on grid power. At night or during low-demand periods, it switches to stored energy from batteries or renewable sources.

3. Predictive Maintenance and Energy Efficiency

  • Predictive Maintenance: AI predicts equipment failures, preventing unplanned downtime. Well-maintained machinery operates more efficiently, reducing energy waste.
  • Energy-Efficient Equipment: AI identifies energy-hungry equipment and suggests upgrades or replacements. For instance: replacing old motors with energy-efficient ones, installing variable frequency drives (VFDs) to optimize motor speed, and others.

4. Demand Response and Load Shifting

  • Demand Response Programs: AI participates in utility demand response programs. When the grid is stressed, the factory reduces non-essential loads or switches to backup power.
  • Load Shifting: AI shifts energy-intensive processes to off-peak hours. For example: running heavy machinery during nighttime when electricity rates are lower, charging electric forklifts during off-peak hours, etc.
Benefits of implementing industrial AI solutions.

Benefits and Dollar Savings

AI and IoT empower smart factories to make data-driven decisions, minimize waste, and contribute to a more sustainable future. Dollar savings, environmental benefits, and operational efficiency go hand in hand.

Moreover, implementing automated visual inspection and AI-driven predictive maintenance in factories enables more benefits like:

Reduced Downtime:

  • Predictive Maintenance: By identifying potential equipment failures before they occur, factories can schedule maintenance during planned downtime. This minimizes unplanned interruptions and keeps production lines running smoothly.

Enhanced Quality Control:

  • Automated Visual Inspection: AI-powered systems detect defects, inconsistencies, or deviations in real-time. This ensures that only high-quality products reach the market.
  • Cost Savings: Fewer defective products mean less waste and rework, leading to cost savings.

Optimized Resource Allocation:

  • Energy Efficiency: AI analyzes energy consumption patterns and suggests adjustments. Factories can allocate resources (such as electricity, water, and raw materials) more efficiently.
  • Resource Cost Reduction: By using resources judiciously, factories reduce expenses.

Improved Safety:

  • Predictive Maintenance: Well-maintained machinery is less likely to malfunction, reducing safety risks for workers.
  • Visual Inspection: Detecting safety hazards (e.g., loose bolts, and faulty wiring) prevents accidents.

Streamlined Inventory Management:

  • Predictive Maintenance: AI predicts spare part requirements. Factories maintain optimal inventory levels, avoiding overstocking or stockouts.
  • Cost Savings: Efficient inventory management reduces storage costs and ensures timely replacements.

Better Workforce Utilization:

  • Predictive Maintenance: Workers focus on value-added tasks instead of emergency repairs.
  • Visual Inspection: Skilled workers can focus on complex inspections, while AI handles routine checks.

Reduced Environmental Impact:

  • Energy Efficiency: By optimizing energy usage, factories contribute to sustainability goals and reduce their carbon footprint.
  • Waste Reduction: Fewer defects mean less waste, benefiting the environment.

Smart factories leverage technologies like AI, IoT, and advanced robotics to optimize efficiency, quality, and competitiveness. Benefits include reduced downtime, increased operational efficiency, improved product quality, enhanced worker safety, and greater flexibility in responding to market demands.

For instance, a large company (Praxair/Linde) achieved up to $10 million/year savings with a 1% increase in operating efficiency, while a 25% reduction in energy usage, operations costs, and downtime resulted in about $55 million in annual savings at a J&J manufacturing base.

These technologies not only enhance efficiency but also lead to tangible cost savings, improved safety, and a more sustainable manufacturing ecosystem. Overall, the adoption of smart factory technologies leads to tangible benefits, cost savings, and operational improvements.

Beyond the Factory Floor: AI in Back Office Automation

While AI plays a crucial role on the factory floor, its impact extends beyond production lines. Back-office automation is equally vital. Here are a few examples:

In summary, AI transforms not only the factory floor but also the entire organizational ecosystem.

Let’s meet at IDC Smart Factory conference in Katowice in Poland! June 20th, 2024
Keynote Presentation at IDC Smart Factory conference

Learn more:


Smart Factories: Concepts and Features was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

How Often Should You Change Account Passwords to Prevent Fraud?

Experts say you should change your password every three months to avoid any potential issues. So, here’s the answer to the title – we should change our passwords every three months. Studies also say the average person has around 80 passwords, but we highly doubt that. It’s probably more true that the average person has […]

Data Centres See Increased Demand Thanks to AI and IoT, Says MP Vijay Kumar

Data centres in India are in good demand and it is largely due to advancements in artificial intelligence (AI) and the Internet of Things (IoT). One of the key players in the sector is Sify Technologies. It is capitalizing on the trend. Its existing footprint is nearly 100 MW across 12 data centres in six […]

5 Reasons Why a Google Ads Agency Can Transform Your Business

In the competitive digital landscape, gaining the power of Google Ads can be a huge game-changer for your company. But, navigating this complex advertising platform requires more than a basic understanding of keywords and ad placements. This is where a highly specialized Google Ads agency can make all the difference. With their expertise, strategic insights, […]

How Tech is Transforming St. Louis Commercial Vehicle Crash Investigations?

For decades, investigating commercial vehicle crashes in St. Louis relied heavily on manual processes. This included things like collecting witness statements, sketching accident scenes, and sifting through physical evidence.  This approach, while thorough, was often time-consuming and prone to human error. However, a wave of technological advancements is transforming the way these accidents are investigated. […]

Waabi Announces $200M Series B Funding Round Led by Uber, Khosla Ventures, Nvidia

Toronto-based autonomous trucking startup Waabi is revolutionizing the transportation industry and lately has secured $200 million in Series B funding round. The investors included Uber, Khosla Ventures, Nvidia, Porsche and Volvo’s venture capital arm. The new capital highlights confidence of investors in the company. Waabi has distinguished itself in the autonomous vehicle development segment. It […]

Hospitality Startup Oyo Finalizes Rs 1,000 Crore Fundraise from Family Investors

Hospitality startup Oyo adding up some funds lately. It is in the final stages of securing Rs 1,000 crore ($120 million) and primarily from the family offices of some of the prominent corporate executives and stock market experts. The notable figures include Anand Jain, a former senior executive at Reliance Industries, Mankind Pharma promoters Ramesh […]

DRDO Partners Startup to Develop AI Authentication Tool for Defence, Law Enforcement

India’s defense and law enforcement agencies are embracing technological revolution. It has partnered with woman-led startup Ingenious Research Solutions Pvt Ltd. to make use of its AI authentication tool called ‘Divya Drishti.’ It is learned to help in enhancing security measures. The development was announced by Defence Ministry and it showcases exciting collaboration between Defence […]

Reskilling for the AI Era: Navigating Career Transitions

Artificial Intelligence, commonly known as AI, refers to the simulation of human intelligence in machines designed to think and learn like humans. These machines are capable of performing tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation. As AI continues to evolve, it is becoming an integral part […]