Machine Learning & Deep Learning Fundamentals

with deeplizard.

Supervised Learning explained

November 22, 2017 by


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.


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 import backend as K
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense
from keras.optimizers import Adam

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

model = Sequential([
    Dense(16, input_shape=(2,), activation='relu'),
    Dense(32, activation='relu'),
    Dense(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.


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 = [
    [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 = [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.

Now, when we go to train our model, we call 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!


In this video, we explain the concept of supervised learning. We also show how supervised learning occurs in code with Keras. 💥🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎💥 👉 Check out the blog post and other resources for this video: 🔗 💻 DOWNLOAD ACCESS TO CODE FILES 🤖 Available for members of the deeplizard hivemind: 🔗 🧠 Support collective intelligence, join the deeplizard hivemind: 🔗 🤜 Support collective intelligence, create a quiz question for this video: 🔗 🚀 Boost collective intelligence by sharing this video on social media! ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Peder B. Helland 👀 Follow deeplizard: Twitter: Facebook: Patreon: YouTube: Instagram: 🎓 Other deeplizard courses: Reinforcement Learning - NN Programming - DL Fundamentals - Keras - TensorFlow.js - Data Science - Trading - 🛒 Check out products deeplizard recommends on Amazon: 🔗 📕 Get a FREE 30-day Audible trial and 2 FREE audio books using deeplizard’s link: 🔗 🎵 deeplizard uses music by Kevin MacLeod 🔗 🔗 ❤️ Please use the knowledge gained from deeplizard content for good, not evil.