Supervised vs. unsupervised vs. reinforcement learning

Supervised learning

In supervised learning, the AI model is trained based on the given input and its expected output, i.e., the label of the input. The model creates a mapping equation based on the inputs and outputs and predicts the label of the inputs in the future based on that mapping equation.

Example

Let’s suppose we have to develop a model that differentiates between a cat and a dog. To train the model, we feed multiple images of cats and dogs into the model with a label indicating whether the image is of a cat or a dog. The model tries to develop an equation between the input images and their labels. After training, the model can predict whether an image is of a cat or a dog even if the image is previously unseen by the model.

Code example

Let’s see the code example of classifying the images using Keras. The following code is trained with limited images for seas and buildings. Therefore, we’ll be providing it with an unseen image from one of the two categories to see how well it predicts that image.

Note: The images have been taken from the “Intel Image Classification” dataset.

import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
import base64

imageSize = (250, 250)
batchSize = 20

trainDirectory = 'archive/seg_train/seg_train'
testDirectory = 'archive/seg_test/seg_test'

generateTrainingData = ImageDataGenerator(
    rescale=1./255,
    rotation_range=25,
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    fill_mode='nearest'
)

trainDataset = generateTrainingData.flow_from_directory(
    trainDirectory,
    seed=594,
    target_size=imageSize,
    batch_size=batchSize,
    class_mode='sparse'
)

validationDataset = tf.keras.utils.image_dataset_from_directory(
    testDirectory,
    seed=594,
    image_size=imageSize,
    batch_size=batchSize
)

classNames = list(trainDataset.class_indices.keys())
classCount = len(classNames)

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(20, 3, activation='relu', input_shape=(imageSize[0], imageSize[1], 3)),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(40, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(80, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(80, activation='relu'),
    tf.keras.layers.Dense(classCount)
])

model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy']
)

history = model.fit(
    trainDataset,
    validation_data=validationDataset,
    epochs=15
)

img = image.load_img('19763.jpg', target_size=imageSize)
imgArray = image.img_to_array(img)
imgArray = np.expand_dims(imgArray, axis=0)
imgArray = imgArray / 255.0

predictions = model.predict(imgArray)
predictedClassIndex = np.argmax(predictions)
predictedClass = classNames[predictedClassIndex]

plt.imshow(imgArray[0])
plt.title(predictedClass)
plt.savefig('output.png')

html = f'''
<html>
<body>
<h1>Predicted Class: {predictedClass}</h1>
<img src="data:image/png;base64,{base64.b64encode(open('output.png', 'rb').read()).decode('utf-8')}" alt="Output">
</body>
</html>
'''

with open('output.html', 'w') as file:
    file.write(html)

Note: To read more about the image classification using Keras, check out this answer.

Unsupervised learning

In unsupervised learning, the AI model is trained only on the inputs, without their labels. The model classifies the input data into classes that have similar features. The label of the input is then predicted in the future based on the similarity of its features with one of the classes.

Example

Suppose we have a collection of red and blue balls and we have to classify them into two classes. Let’s say all other features of the balls are the same except for their color. The model tries to find the dissimilar features between the balls on the basis of how the model can classify the balls into two classes. After the balls are classified into two classes depending on their color, we get two clusters of balls, one of blue color and one of red color.

Code example

Let’s see the code example of clustering using the DBSCAN algorithm.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.cluster import DBSCAN
# Create a random dataset with 1000 samples and 2 features
X, _= make_classification(
n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4
)
df = pd.DataFrame(X)
print(df.shape)
# # define the model
dbscan_model = DBSCAN(eps=0.35,min_samples=16)
# # train the model
dbscan_model.fit(df)
# #visualize the clusters.
plt.figure(figsize=(10,10))
plt.scatter(df[0],df[1],c = dbscan_model.labels_,s=15)
plt.title('DBSCAN Clustering',fontsize=20)
plt.xlabel('Feature 1',fontsize=14)
plt.ylabel('Feature 2',fontsize=14)
plt.show()

Note: To read more about the DBSCAN algorithm, check out this answer.

Reinforcement learning

In reinforcement learning, the AI model tries to take the best possible action in a given situation to maximize the total profit. The model learns by getting feedback on its past outcomes.

Consider the example of a robot that is asked to choose a path between A and B. In the beginning, the robot chooses either of the paths as it has no past experience. The robot is given feedback on the path it chooses and learns from this feedback. The next time the robot gets into a similar situation, it can use feedback to solve the problem. For example, if the robot chooses path B and gets a reward, i.e., positive feedback, this time the robot knows that it has to choose path B to maximize its reward.

Side-by-side comparison

Criteria

Supervised Learning

Unsupervised Learning

Reinforcement Learning

Input Data

Input data is labelled.

Input data is not labelled.

Input data is not predefined.

Problem

Learn pattern of inputs and their labels.

Divide data into classes.

Find the best reward between a start and an end state.

Solution

Finds a mapping equation on input data and its labels.

Finds similar features in input data to classify it into classes.

Maximizes reward by assessing the results of state-action pairs

Model Building

Model is built and trained prior to testing.

Model is built and trained prior to testing.

The model is trained and tested simultaneously.

Applications

Deal with regression and classification problems.

Deals with clustering and associative rule mining problems.

Deals with exploration and exploitation problems.

Algorithms Used

Decision trees, linear regression, K-nearest neighbors

K-means clustering, k-medoids clustering, agglomerative clustering

Q-learning, SARSA, Deep Q Network

Examples

Image detection, Population growth prediction

Customer segmentation, feature elicitation, targeted marketing, etc

Drive-less cars, self-navigating vacuum cleaners, etc

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved