Deep Learning Fundamentals - Classic Edition

A newer version of this course is available! Check here for details!

Supervised Learning explained

video

expand_more chevron_left

text

expand_more chevron_left

Supervised learning for machine learning

In this post, we'll be discussing supervised learning. Up to this point in this series, each time we've mentioned the process of training a model or the learning process that the model goes through, we've actually been implicitly talking about supervised learning.

Labeled data

Supervised learning occurs when the data in our training set is labeled.

Labels are used to supervise or guide the learning process.

Recall from our post on training, validation, and testing sets, we explained that both the training data and validation data are labeled when passed to the model. This is the case for supervised learning.

With supervised learning, each piece of data passed to the model during training is a pair that consists of the input object, or sample, along with the corresponding label or output value.

Essentially, with supervised learning, the model is learning how to create a mapping from given inputs to particular outputs based on what it's learning from the labeled training data.

For example, say we're training a model to classify different types of reptiles based on images of reptiles. Now during training, we pass in an image of a lizard.

lizard

Since we're doing supervised learning, we'll also be supplying our model with the label for this image, which in this case is simply just lizard.

Based on what we saw in our post on training, we know that the model will then classify the output of this image, and then determine the error for that image by looking at the difference between the value it predicted and the actual label for the image.

Labels are numeric

To do this, the labels need to be encoded into something numeric. In this case, the label of lizard may be encoded as 0, whereas the label of turtle may be encoded as 1.

After this, we go through this process of determining the error or loss for all of the data in our training set for as many epochs as we specify. Remember, during this training, the objective of the model is to minimize the loss, so when we deploy our model and use it to predict on data it wasn't trained on, it will be making these predictions based on the labeled data that it did see during training.

If we didn't supply our labels to the model, though, then what's the alternative? Well, as opposed to supervised learning, we could instead use something called unsupervised learning. We could also use another technique called semi-supervised learning. We'll be covering each of these topics in future posts.

For now, we're going to take a peek at some Keras code to reiterate how and where we're supplying our labeled samples to our model.

keras logo

Working with labeled data in Keras

We'll start with our imports:

import keras
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense
from keras.optimizers import Adam
import numpy as np

Suppose we have a simple Sequential model here with two hidden dense layers and an output layer with two output categories.

model = Sequential([
    Dense(units=16, input_shape=(2,), activation='relu'),
    Dense(units=32, activation='relu'),
    Dense(units=2, activation='sigmoid')
])

Nothing here should be new information. Everything shown here in terms of what libraries we've imported as well as the architecture of the model and how we're compiling our model have been covered in earlier posts in the Keras series.

We're assuming the task of this model is to classify whether an individual is male or female based on his or her height and weight.

model.compile(
    optimizer=Adam(learning_rate=0.0001), 
    loss='sparse_categorical_crossentropy', 
    metrics=['accuracy']
)

After compiling our model, we've have an example here of some training data that is completely made up for illustration purposes.

# weight, height
train_samples = np.array([
    [150, 67], 
    [130, 60], 
    [200, 65], 
    [125, 52], 
    [230, 72], 
    [181, 70]
])

The actual training data is stored in the train_samples variable. Here, we have a list of pairs, and each of these pairs is an individual sample, and a sample is the weight and height of a person.

The first element in each pair is the weight measured in pounds, and the second element is the height measured in inches.

Next, we have our labels stored in this train_labels variable. Here, a 0 represents a male, and a 1 represents a female.

# 0: male
# 1: female
train_labels = np.array([1, 1, 0, 1, 0, 0])

The position of each of these labels corresponds to the positions of each sample in our train_samples variable. For example, this first 1 here, which represents a female, is the label for the first element in the train_samples array. This second 1 in train_labels corresponds to the second sample in train_samples, and so on.

model.fit(
    x=train_samples, 
    y=train_labels,
    batch_size=3,
    epochs=10,
    shuffle=True,
    verbose=2
)

Now, when we go to train our model, we call model.fit() as we've discussed in previous posts, and the first parameter here specified by x is going to be our train_samples variable, and the second parameter, specified by y, is going to be the corresponding train_labels.

Wrapping up

That's all there is to it for supplying labeled data to a Keras model for supervised learning!

In addition to this, hopefully you now have an understanding for what supervised learning is and how to make use of it. In future posts we'll contrast this idea with other learning mechanisms. I'll see ya in the next one!

quiz

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

Quiz Results

resources

expand_more chevron_left
In this video, we explain the concept of supervised learning. We also show how supervised learning occurs in code with Keras. πŸ•’πŸ¦Ž VIDEO SECTIONS πŸ¦ŽπŸ•’ 00:00 Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources 00:30 Help deeplizard add video timestamps - See example in the description 04:16 Collective Intelligence and the DEEPLIZARD HIVEMIND πŸ’₯🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎πŸ’₯ πŸ‘‹ Hey, we're Chris and Mandy, the creators of deeplizard! πŸ‘€ CHECK OUT OUR VLOG: πŸ”— https://youtube.com/deeplizardvlog πŸ’ͺ CHECK OUT OUR FITNESS CHANNEL: πŸ”— https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA 🧠 Use code DEEPLIZARD at checkout to receive 15% off your first Neurohacker order: πŸ”— https://neurohacker.com/shop?rfsn=6488344.d171c6 ❀️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Mano Prime πŸ‘€ Follow deeplizard: Our vlog: https://youtube.com/deeplizardvlog Fitness: https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA Facebook: https://facebook.com/deeplizard Instagram: https://instagram.com/deeplizard Twitter: https://twitter.com/deeplizard Patreon: https://patreon.com/deeplizard YouTube: https://youtube.com/deeplizard πŸŽ“ Deep Learning with deeplizard: AI Art for Beginners - https://deeplizard.com/course/sdcpailzrd Deep Learning Dictionary - https://deeplizard.com/course/ddcpailzrd Deep Learning Fundamentals - https://deeplizard.com/course/dlcpailzrd Learn TensorFlow - https://deeplizard.com/course/tfcpailzrd Learn PyTorch - https://deeplizard.com/course/ptcpailzrd Natural Language Processing - https://deeplizard.com/course/txtcpailzrd Reinforcement Learning - https://deeplizard.com/course/rlcpailzrd Generative Adversarial Networks - https://deeplizard.com/course/gacpailzrd Stable Diffusion Masterclass - https://deeplizard.com/course/dicpailzrd πŸŽ“ Other Courses: DL Fundamentals Classic - https://deeplizard.com/learn/video/gZmobeGL0Yg Deep Learning Deployment - https://deeplizard.com/learn/video/SI1hVGvbbZ4 Data Science - https://deeplizard.com/learn/video/d11chG7Z-xk Trading - https://deeplizard.com/learn/video/ZpfCK_uHL9Y πŸ›’ Check out products deeplizard recommends on Amazon: πŸ”— https://amazon.com/shop/deeplizard πŸ“• Get a FREE 30-day Audible trial and 2 FREE audio books using deeplizard's link: πŸ”— https://amzn.to/2yoqWRn 🎡 deeplizard uses music by Kevin MacLeod πŸ”— https://youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ ❀️ Please use the knowledge gained from deeplizard content for good, not evil.

updates

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

Update history for this page

Did you know you that deeplizard content is regularly updated and maintained?

  • Updated
  • Maintained

Spot something that needs to be updated? Don't hesitate to let us know. We'll fix it!


All relevant updates for the content on this page are listed below.