Machine Learning & Deep Learning Fundamentals

with deeplizard.

Supervised Learning explained

November 22, 2017 by

Blog

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

model.compile(
    Adam(lr=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 = [
    [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.

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!

Description

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: 🔗 https://deeplizard.com/learn/video/Quh6x4kG6VY 💻 DOWNLOAD ACCESS TO CODE FILES 🤖 Available for members of the deeplizard hivemind: 🔗 https://www.patreon.com/posts/27743395 🧠 Support collective intelligence, join the deeplizard hivemind: 🔗 https://deeplizard.com/hivemind 🤜 Support collective intelligence, create a quiz question for this video: 🔗 https://deeplizard.com/create-quiz-question 🚀 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: https://twitter.com/deeplizard Facebook: https://www.facebook.com/Deeplizard-145413762948316 Patreon: https://www.patreon.com/deeplizard YouTube: https://www.youtube.com/deeplizard Instagram: https://www.instagram.com/deeplizard/ 🎓 Other deeplizard courses: Reinforcement Learning - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xoWNVdDudn51XM8lOuZ_Njv NN Programming - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xrfNyHZsM6ufI0iZENK9xgG DL Fundamentals - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xq7LwI2y8_QtvuXZedL6tQU Keras - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xrwRnXk_yCPtnqqo4_u2YGL TensorFlow.js - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xr83l8w44N_g3pygvajLrJ- Data Science - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xrth-Cqs_R9- Trading - https://deeplizard.com/learn/playlist/PLZbbT5o_s2xr17PqeytCKiCD-TJj89rII 🛒 Check out products deeplizard recommends on Amazon: 🔗 https://www.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://www.youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ 🔗 http://incompetech.com/ ❤️ Please use the knowledge gained from deeplizard content for good, not evil.