Neetz World
January 7, 2025

Deep Learning with example

Posted on January 7, 2025  •  3 minutes  • 623 words

Learning (In machine learning context):

Describes an automatic search process for data transformations that produce useful representations of some data, guided by some feedback signal.

Machine Learning:

Machine Learning is searching for useful representations and rules over some input data, within a predefined space of possibilities, using guidance from a feedback signal.

Deep Learning:

Deep in deep learning represents ‘how many layers contribute to a model of the data’. So, deep learning is a new take on learning representations from data that emphasizes learning successive layers of increasingly meaningful representations. It is also called layered representation learning or hierarchical representation learning.

Its also called a multiple stage way to learn data representations.

Neutral Network:

Layered representation in deep learning are learned via models called neutral networks. These are structured in literal layers stacked on each other. 3

How Deep Learning works:

Now we know that machine learning maps the inputs (e.g cat images) to targets (e.g cat labels) which is done by observing my examples of these inputs and results. Similarly, deep learning does these mapping of input-to-target via deep sequence of layers(data transformations) and they learn by the exposure to examples.

Since there are multiple layers involved, how would go with one over other. So, it depends on the transformation implemented by layers ie. specification of what a layer does to its input data is stored in the layer’s weights. Weights are the bunch of numbers, they are also sometimes called parameters of a layer

Lets start with an example and explain each steps to understand it better.

model= Sequential([layers.Dense(512, activation="relu"),
                  layers.Dense(10, activation="softmax")])
model.compile(optimizer="adam", 
              loss= "sparse_categorical_crossentropy",
              metrics=["accuracy"])
train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype("float32")/255
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype("float32")/255
model.fit(train_images, train_labels, epochs=5, batch_size=128)
test_digits = test_images[0:10]
predictions = model.predict(test_digits)
for i in range(len(predictions)):
    print("Test Sample", i, "\nPredictions:",predictions[i],"\nPrediction MaxValue:",predictions[i].argmax(),"\nTest Label: ", test_labels[i], "\n\n")

This is the complete code:

from keras import layers, Sequential
from keras.datasets import mnist

#Get training and testing data
(train_images, train_labels), ( test_images, test_labels) = mnist.load_data()

# Create neutral network with 2 Dense layers
model= Sequential([layers.Dense(512, activation="relu"),
                  layers.Dense(10, activation="softmax")])

# Add Optimizer: improves the performance of the prediction
# Loss function: gives the difference between actual and expected result and stare the direction
# Metrics: Parameter on which result will be checked against. 
model.compile(optimizer="adam", 
              loss= "sparse_categorical_crossentropy",
              metrics=["accuracy"])

# Currently, images are in array of (60000, 28, 28) uint8 type but we need to preprocess the data by reshaping into shape
# model expect it and scale it to range of [0,1] interval by transforming it into a float32 array
# of shape (60000, 28*28) with values between 0-1.

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype("float32")/255
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype("float32")/255

model.fit(train_images, train_labels, epochs=5, batch_size=128)

test_digits = test_images[0:10]
predictions = model.predict(test_digits)
for i in range(len(predictions)):
    print("Test Sample", i, "\nPredictions:",predictions[i],"\nPrediction MaxValue:",predictions[i].argmax(),"\nTest Label: ", test_labels[i], "\n\n")


test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print("Test Accuracy:",test_accuracy )

Lets create an application with the above example of neural network. Checkout my next blog :) . Hope you learned something new.

Follow me

To have some coding fun and learning new things!