2. 02 PyTorch, Datasets, and Models
2. 02 PyTorch, Datasets, and Models
Learning Objectives
By the end of this chapter, you should be able to:
1. Discuss the difference between supervised and unsupervised learning
2. Discuss the difference between software development and machine &
deep learning
3. Understand the general idea behind building and training a model
4. Identify commonly used terms in machine and deep learning and their
meanings
Deep Learning Framework
▪ Deep learning frameworks are software libraries (APIs, tools, abstractions) that
help ML professionals with tools for training deep learning models.
▪ The packages in the library include activation functions, layers, loss functions and
optimizers that help create different architectures dor deep neural networks.
▪ Examples are Tensorflow, keras, Microsoft Cognitive Toolkit (CNTK0, Scikit-learn,
Theano
What Is PyTorch?
▪ PyTorch is an open-source deep learning framework developed by Meta AI in
2016. It offers both modularity and flexibility, making it a capable tool for
everything from tinkering with innovative models to maintaining an industry-scale
application.
▪ PyTorch can be used to solve many types of problems, including
optimization problems.
▪ To estimate the fuel efficiency of cars based on their power and weight. You'll need
data for this. PyTorch can help you develop a model that predicts the car's
efficiency based on its power and weight. This would be a specific type of
optimization problem called a linear regression model. This is a simple problem
that could be solved in Excel, but this makes it a useful thought experiment to
begin wrapping your head around the sorts of problems PyTorch can solve.
What Is PyTorch?
▪ But, what about really complex problems? What if you have hundreds
or thousands of columns in a spreadsheet? What if you have a
humongous amount of data (picture millions of rows in a
spreadsheet)?
▪ PyTorch uses an algorithm called gradient descent that is capable of
looking for solutions regardless of how complex the problem is, or
how massive the amount of data is. It starts from a random point and,
little by little, works on improving the solution, one baby step at a time.
This is performed by PyTorch's autograd module, which does the
heavy lifting for you so you can focus on more interesting matters.
The PyTorch Ecosystem
The range of fields and applications that can be powered by PyTorch is extensive:
▪ Computer Vision (Kornia, Medical Open Network for Artificial Intelligence (MONAI), OpenMMLab, PyTorchVideo,
Detectron2, PyTorch3D)
▪ machine and vehicular object detection, tracking, identification, and avoidance
▪ medical image analysis and diagnosis
▪ image recognition, classification, and tagging
▪ video classification and detection
▪ Natural Language Processing (AllenNLP, NeMo, Transformers, flair)
▪ text classification, summarization, generation, and translation virtual assistants
▪ sentiment analysis
▪ question answering and search engines
▪ Graph Neural Networks (torchdrug, PyTorch Geometric, DGL)
▪ molecule fingerprinting
▪ drug discovery and protein interface prediction
▪ social network analysis
▪ Spatio-Temporal Graph Neural Networks (PyTorch Geometric Temporal)
▪ route planning and navigation
▪ traffic control and management
▪ inventory planning
▪ logistics optimization
The PyTorch Ecosystem
The range of fields and applications that can be powered by PyTorch is extensive:
▪ Gaussian Processes (GPyTorch)
▪ time series modeling and anomaly detection
▪ risk management
▪ control engineering and robotics
▪ Reinforcement Learning (PRFL)
▪ industry automation and robotics manipulation
▪ dynamic treatment regimes (DTRs) in healthcare
▪ real-time bidding
▪ strategy games
▪ Recommender Systems (TorchRec)
▪ Interpretability and Explainability (Captum)
▪ Privacy-Preserving Machine Learning (CrypTen, PySyft, Opacus)
▪ Federated Learning - collaboratively training a model without the need to centralize the data (PySyft, Flower)
In a nutshell, there are libraries and pre-trained models available for a wide
range of topics and applications.
Hugging Face
▪ While not a part of PyTorch, Hugging Face is widely known for its
large open-source community and is a central hub for models and
Python libraries, especially in the area of natural language
processing (NLP).
▪ Because it is so often used alongside PyTorch, it belongs in any
discussion of the PyTorch ecosystem and we will make use of it in
this course.
Types of Machine Learning
There are three general categories under the umbrella of machine learning-
supervised learning, unsupervised learning and reinforcement learning.
Supervised vs. Unsupervised Learning
▪ The majority of models and algorithms in machine and deep learning fall
into one of these classes: supervised learning and unsupervised
learning.
▪ To train a model using supervised learning is like actively teaching a
toddler. You can show them a picture of a zebra and ask them what they
see. They reply "it's a horse", because they've never seen a zebra before.
You tell them it was a good guess, but the right answer is "zebra". You're
supervising their learning by providing the right answer to every question.
Hopefully, with enough examples and their corresponding answers, your
model will also learn.
Supervised Learning
Supervised learning can be used for two major tasks:
• Classification
• Regression
Classification tasks predict labels that are a category or class- like spam or
not spam.
Regression tasks predict labels that are numeric - like this house is valued
at $456,000.
Questions
Match the example with the appropriate type of supervised learning.
▪ An app to identify the plant type from an image.
▪ An e-commerce site predicting prices for their products.
▪ An e-commerce site recommending products based on other customers'
purchase similarities.
▪ A car dealership is scoring their customers on long-term value into 1, 2, 3
or 4
▪ A medical research company is predicting drug dosages for patients.
▪ A design software that removes noise from visual data to improve picture
quality.
Supervised vs. Unsupervised Learning
▪ Unsupervised learning, on the other hand, is like giving a toddler
a bunch of building blocks and asking them to organize them
without any specific instructions on how to do it.
▪ Maybe the toddler will split the blocks by color. Maybe they will
split the blocks by size. It depends on which feature of the
blocks, the color or the size, is more noticeable to them. Notice
that you're not giving the toddler any "right" answers. It's exactly
the same with these models and algorithms: they will look for
similarities in the data and use it to split it into groups. We won't
be covering those in this course.
Questions
Match the example with the correct type of machine learning.
▪ You ask a child to sort toys, and they can do it based on any
characteristics they like.
▪ You ask a child to sort toys into stuffed toys, trucks and building
materials.
Reinforcement Learning
▪ Reinforcement learning (RL) systems learn from
continuous experience rather than labels or historical
patterns.
▪ Learning happens through interaction.
▪ For every action, the system takes, a reward is given, and
the system's goal is to maximize the accumulated
rewards.
Software Development vs Machine and Deep
Learning
A function that converts temperature from Celsius to Fahrenheit, is given by:
def celsius2fahrenheit(celsius):
fahrenheit = celsius*(9/5)+32
return fahrenheit
That's "Software 1.0": given the arguments and the rules, it will produce the desired output.
Software Development vs Machine and Deep
Learning
Things start to fall apart quickly if the rules aren't clear. Let's go back to our toddler and
the zebra. If they're seeing the zebra for the first time, they may ask you: "is this a
horse?" And you'll explain to them that it's not a horse, even though both animals have
four legs. Zebras have stripes. That should do the trick for the toddler. But what about
code? Can you write a function that takes an image and returns if it's a zebra or not?
def contains_zebra(image):
# write your code here
is_zebra = ...
return is zebra
How do you even start coding that?! We challenge you to try to come up with a set of rules
to determine if an image shows a zebra in it or not. That's impossibly hard using traditional
software development.
Software Development vs Machine and Deep
Learning
Still, the toddler can easily learn that after a couple of tries. How? They're learning by
example, not by rules. You show the toddler the picture of the zebra (the input), and you
tell them the answer (the desired output). Their brain comes up with the rules, even if no
one can write them down or implement them in a function.
That's "Software 2.0". You don't know the rules: you only have inputs and outputs.
Unlike toddlers, however, it won't take just a couple of tries for a model to learn. It will
need thousands, if not millions, of examples. That's just how models are.
"Hello Model"
▪ Let's train a simple model from scratch to give you a better idea of the process. It is the "Hello
World" of training models or, better yet, the "Hello Model".
▪ We have four cars: "Chevrolet Chevelle Malibu", "Buick Skylark 320", "AMC Rebel SST", and "Ford
Torino".
▪ For each car, we know its power in HP: 130, 165, 150, and 140, respectively. We also know that
the more powerful a car is, the less efficient consumption-wise it will be too. But, how
inefficient? It would be interesting to devise a rule to figure out, given the car's power, how
efficient (or not) we expect it to be.
▪ So, let's drive them around and collect some fuel consumption data! Once data is collected, we
get the desired output of our model for the cars we drove around. The fuel consumption for each
one of the cars, in miles per gallon, is 18, 15, 16, and 17, respectively.
▪ We have data (power in HP), we have the desired output (fuel consumption in MPG), and we're
missing the rule that maps one into the other. Sounds like a task for "Software 2.0", that is, a
model.
▪ What's the simplest model you can come up with?
"Hello Model"
Y=-0.086x+29.075
MPG=-0.086HP+29.075
What if we wanted to use our new model to estimate the fuel consumption of the Ford
Torino? It has 140 HP. Answer is 17.035
Biological Neuron (Source: Fundamental of Neural Networks: Architectures, Algorithms and Application By Laurene Fausett)
Similarity/Analogy of Biological Neural Network with ANN
Output Signals
Output Signals
Input Signals
Signals
Input
Click on "Save" to close the menu, and once you run any of the cells, it will connect to a GPU-powered runtime (it may
take a few seconds) and you'll be good to take that GPU for a spin!
Your Learning Journey
The road to learning how to effectively use deep learning models isn't without its
difficulties. We'll cover all the steps you need to master in order to prepare your data, train
your model, and use it to make predictions. There will be a lot of back and forth, so we'll
be showing you a map throughout your learning journey, to help keep you oriented at any
given moment:
Most of the time, we'll be following the steps in the right order when tackling
a given task. However, you'll see that in later chapters, we'll skip some steps,
or even move backwards (usually to the "Preprocessing Data" step).