Building a Neural Network with PyTorch in 15 Minutes | Coding Challenge

Nicholas Renotte
24 Aug 202220:34

Summary

TLDRIn this fast-paced video, the host challenges himself to code a PyTorch deep learning model within 15 minutes without referring to any documentation or pre-existing code. He introduces PyTorch, an open-source machine learning library, and proceeds to build a neural network for the MNIST dataset. The video covers model creation, training, and prediction, showcasing the host's ability to meet the time constraint and successfully train a model to recognize handwritten digits.

Takeaways

  • 🕒 The video is a challenge to code a PyTorch deep learning model within 15 minutes.
  • 🚫 The presenter sets a rule to avoid looking at documentation or using pre-existing code, with a 1-minute penalty for violations.
  • 🎁 There's a stake involved: if the time limit is not met, the presenter will give a $50 Amazon gift card to the viewers.
  • 🛠️ PyTorch is an open-source deep learning framework developed by Facebook, used for building deep learning models quickly.
  • 💾 The video demonstrates how to import necessary dependencies from PyTorch and TorchVision for model building.
  • 📈 The presenter guides through creating a neural network class, setting up the model architecture with convolutional layers, and defining a forward method.
  • 🔄 The training process is explained, including data loading, setting up the optimizer, defining a loss function, and writing a training loop.
  • 💾 The video includes a practical example of training a model on the MNIST dataset, which consists of images of handwritten digits.
  • 🏁 The presenter successfully trains the model and saves the weights within the time limit, demonstrating the efficiency of PyTorch.
  • 🔮 After training, the video shows how to load the saved model and make predictions on new data, showcasing the model's ability to classify images.

Q & A

  • What is the main challenge the video aims to address?

    -The video aims to challenge the presenter to code a PyTorch deep learning model within a strict time limit of 15 minutes without referring to any documentation or pre-existing code.

  • What are the consequences if the presenter fails to meet the 15-minute time limit?

    -If the presenter fails to meet the time limit, they will give away a $50 Amazon gift card to the viewers.

  • What is the purpose of using the MNIST dataset in the video?

    -The MNIST dataset is used for training the deep neural network model as it is an image classification dataset with 10 classes (0-9), which is suitable for demonstrating the capabilities of PyTorch in a short timeframe.

  • What is the role of the 'transforms' module from 'torchvision' in the script?

    -The 'transforms' module is used to convert images into tensors, which are the data format that PyTorch works with for building and training neural networks.

  • What is the significance of the 'nn.Sequential' API used in the video?

    -The 'nn.Sequential' API in PyTorch is used to create a sequence of neural network layers, making it easier to stack layers and build the model architecture.

  • How does the video demonstrate the process of creating a convolutional neural network (CNN)?

    -The video demonstrates creating a CNN by stacking multiple 'nn.Conv2d' layers with specific parameters, such as the number of input channels, output channels, and kernel size.

  • What is the purpose of the 'nn.Flatten' layer in the neural network architecture presented?

    -The 'nn.Flatten' layer is used to flatten the output of the convolutional layers so that it can be fed into the fully connected 'nn.Linear' layers for classification.

  • How is the training process of the neural network described in the video?

    -The training process involves iterating over batches of data, making predictions, calculating loss using 'nn.CrossEntropyLoss', and applying backpropagation to update the model's weights using the 'optim.Adam' optimizer.

  • What is the significance of the 'cuda' keyword in the script?

    -The 'cuda' keyword is used to specify that the model and data should be sent to the GPU for faster computation. If a GPU is not available, it defaults to using the CPU.

  • How does the presenter ensure the model's performance is evaluated during the training?

    -The presenter prints out the loss for each batch after each epoch to monitor the model's performance and ensure it is learning effectively.

  • What is the final step demonstrated in the video after training the model?

    -The final step demonstrated is saving the trained model's state to a file using 'torch.save', which allows the model to be reloaded and used for making predictions without retraining.

Outlines

00:00

🕒 Introduction to a PyTorch Deep Learning Challenge

The speaker introduces a challenge to code a PyTorch deep learning model within a 15-minute time limit. They outline the rules: no prior code or documentation can be referenced, and using copilot is not allowed due to an expired subscription. A one-minute penalty is applied for breaking these rules. If the time limit is not met, a 50 Amazon gift card is promised to the audience. The speaker briefly explains PyTorch as an open-source deep learning framework developed by Facebook, used for building deep learning models quickly and efficiently.

05:02

💻 Setting Up the PyTorch Neural Network

The speaker begins coding by creating a Python file and importing necessary dependencies from PyTorch, including the neural network class, Adam optimizer, and data loader. They also import datasets and transforms from torchvision to handle image data. The speaker then proceeds to download the MNIST dataset, a collection of images for handwritten digit classification, and sets up data transformations to convert images into tensors. A data loader is created to batch the data into sets of 32 images. The speaker then defines an 'image classifier' class, inheriting from PyTorch's neural network module, and sets up the model architecture using convolutional layers and a linear layer for classification.

10:03

🚀 Rapid Development of the Image Classifier

The speaker continues to develop the 'image classifier' by defining the model's layers, including convolutional layers with varying numbers of filters and kernel sizes. They calculate the output size after applying the convolutional layers and adjust the input size for the linear layer accordingly. The speaker also sets up the forward method for the model, which passes input data through the defined layers. They create an instance of the classifier, move it to a GPU if available, and define the Adam optimizer with a specified learning rate. A cross-entropy loss function is also instantiated. The speaker then outlines the training loop, which includes unpacking data batches, sending data to the GPU, making predictions, calculating loss, and performing backpropagation.

15:05

🏁 Completing the Training and Saving the Model

The speaker wraps up the training process by running the training loop for 10 epochs, printing the loss for each batch, and saving the trained model weights to a file. They express excitement as the training completes without errors and within the time limit. The speaker then demonstrates how to load the saved model and make predictions on new images, showcasing the model's ability to classify handwritten digits correctly. They conclude by thanking the audience and encouraging feedback, promising to share the code on GitHub for those interested in trying the challenge themselves.

20:07

🎉 Conclusion and Engagement

In the final paragraph, the speaker invites viewers to like, subscribe, and turn on notifications for more content. They express gratitude for the viewers' engagement and ask for feedback or suggestions for future challenges. The speaker also inquires if the audience enjoyed the PyTorch deep learning challenge and if they would like to see more of this type of content. The video ends with a positive note, encouraging viewers to participate and learn from the shared experience.

Mindmap

Keywords

💡PyTorch

PyTorch is an open-source machine learning library for Python, used for applications such as computer vision and natural language processing. In the video, PyTorch is the primary framework utilized to build a deep learning model. The script mentions installing PyTorch and using its various modules to create, train, and save a neural network, highlighting its versatility and efficiency in deep learning tasks.

💡Deep Learning

Deep learning is a subset of machine learning that focuses on artificial neural networks with many layers, allowing the model to learn complex patterns in data. The video's theme revolves around constructing a deep learning model using PyTorch, showcasing the process from data loading to model training and prediction, which is central to the script's educational purpose.

💡Neural Network

A neural network is a series of algorithms modeled loosely after the human brain that are designed to recognize patterns. In the script, the creator builds a 'deep neural network' using PyTorch, which involves stacking layers of neural network classes to process and learn from data, exemplified by the 'image classifier' class defined in the video.

💡Convolutional Neural Network (CNN)

A CNN is a type of deep learning model commonly used for image recognition tasks. The script describes adding convolutional layers to the neural network, which are essential for feature extraction in image data. The use of 'nn.conv2d' in the script to create these layers illustrates the application of CNNs in the model.

💡Optimizer

In machine learning, an optimizer is an algorithm that helps in adjusting the model's parameters to minimize the loss function. The script mentions using the 'Adam' optimizer from PyTorch, which is a method to update the model's weights during training, as indicated by the line 'from torch.optim import adam'.

💡Loss Function

A loss function is a measure of how far the model's predictions are from the actual outcome. The video script includes the use of 'nn.cross_entropy_loss' as the loss function, which is crucial for training the neural network by providing a way to quantify and minimize the error during the learning process.

💡Backpropagation

Backpropagation is a method used to calculate the gradient of the loss function with respect to the model's parameters, which is essential for learning. The script refers to applying backpropagation by using 'loss.backward()' to compute the gradient and then updating the model's weights.

💡Epoch

An epoch in machine learning refers to a full pass through the entire dataset. The script mentions training the model for '10 epochs', which means the entire dataset is used for training the model 10 times, a common practice to improve the model's performance.

💡Batch

A batch is a subset of the dataset used in training. The video script describes batching the data into sets of 32 images, which is a common practice to manage memory usage and computational efficiency during training, as shown by the 'batch_size=32' parameter in the DataLoader.

💡Tensor

In the context of PyTorch, a tensor is a generalization of vectors and matrices and is the primary data structure for neural networks. The script includes converting images into tensors using 'transforms.ToTensor()', which is necessary because PyTorch models operate on tensors rather than raw image data.

💡GPU

A GPU (Graphics Processing Unit) is used for accelerating the computation in deep learning tasks. The script mentions sending data to the GPU using 'cuda', which is important for speeding up the training process by leveraging the parallel processing capabilities of GPUs, as indicated by 'x, y = x.cuda(), y.cuda()'.

Highlights

Introduction to a challenge of coding a PyTorch deep learning model in 15 minutes.

Setting a 15-minute time limit for building the model without referencing documentation or pre-existing code.

A one-minute penalty is applied for using documentation or pre-existing code.

The presenter is not using Copilot due to an expired subscription.

Failure to meet the time limit results in a 50 Amazon gift card giveaway.

PyTorch is an open-source deep learning framework developed by Facebook.

Importing necessary dependencies for building the neural network.

Downloading the MNIST dataset for image classification.

Creating a neural network class called 'image classifier'.

Stacking layers for the model using PyTorch's sequential API.

Using convolutional neural network layers with specific parameters.

Adjusting for the reduction in image size after applying convolutional layers.

Creating a linear layer to output predictions for the 10 classes of the MNIST dataset.

Instantiating the neural network, optimizer, and loss function.

Setting up a training loop with backpropagation.

Printing out the loss for each batch during training.

Saving the trained model to the environment.

Successfully training the model and making predictions within the time limit.

Loading the trained model and making predictions on new images.

The presenter expresses happiness and satisfaction with the successful training and prediction.

Encouragement for viewers to try the challenge and provide feedback.

Transcripts

play00:00

so i looked at the pie torch

play00:01

documentation for the very first time

play00:02

yesterday and in this video we're going

play00:04

to be trying to code a pie torch deep

play00:06

learning model in 15 minutes

play00:08

[Music]

play00:09

this is gonna be interesting what's

play00:11

happening guys welcome back to another

play00:12

episode of

play00:14

code that where i try to build stuff in

play00:17

a ridiculously short time frame in this

play00:19

episode as i mentioned we are going to

play00:21

be building a deep neural network using

play00:23

pie torch

play00:25

very quickly so what are the rules well

play00:27

first and foremost we're going to have a

play00:29

time limit as per usual the time limit

play00:31

is going to be 15 minutes now we need

play00:34

some constraints in this particular case

play00:36

i'm not going to be allowed to look at

play00:38

any documentation or pre-existing code

play00:42

also i'm not gonna be able to use

play00:44

copilot to be honest my subscriptions

play00:46

expired anyway so that doesn't really

play00:47

matter if i do go and use existing code

play00:49

or documentation it is a one minute time

play00:53

penalty off our total time limit we're

play00:55

also going to need

play00:56

some steaks if i don't make that time

play00:58

limit it is going to be a 50 amazon gift

play01:01

card to you guys now the last two

play01:04

episodes we haven't quite hit that time

play01:06

limit so who knows we're gonna have to

play01:08

try to make a bit of an effort in this

play01:10

one before we jump right into it we

play01:12

should probably take a little bit of a

play01:13

look as to what is pie torch pie torch

play01:15

is an open source deep learning

play01:17

framework which is predominantly used in

play01:18

python it's developed by the team at

play01:20

facebook and it accelerates the speed at

play01:22

which you can go and build deep learning

play01:24

models it is used quite heavily in

play01:26

state-of-the-art models as well as in

play01:28

deep learning research ready to do it

play01:31

let's get to it

play01:35

alrighty guys let's get into it 15

play01:38

minutes on the clock

play01:40

let's go

play01:42

okay so the first thing that we need to

play01:44

do is create a new python file so i'm

play01:47

just going to call it uh

play01:49

torch and then dot pi

play01:52

okay then we need to import a bunch of

play01:54

dependencies so i've already gone

play01:56

and installed pi torch on this

play01:57

particular machine but we need to import

play01:59

some pie torch dependencies so let's go

play02:01

for it so uh from

play02:03

i torch actually from torch well i'm

play02:05

already screwing up from torch import

play02:07

and then

play02:12

from

play02:14

porch.optimum import adam

play02:18

from torch.utils.data

play02:22

import what is it uh data loader

play02:27

all right cool so those are so torch so

play02:29

this most of our neural network classes

play02:31

are going to be inside of the neural

play02:33

network class atom is going to be our

play02:34

optimizer and data loader is needed to

play02:36

load a data set from pytorch then we

play02:39

need a bunch of dependencies from torch

play02:40

vision so from torch vision

play02:48

watch oh my gosh

play02:50

everything torch vision uh import data

play02:53

sets and then from torch

play02:56

vision so we need this next dependency

play02:58

to convert our images into tensors which

play03:00

are going to work with pytorch so from

play03:02

torch vision dot

play03:04

transforms

play03:06

import uh what do we need

play03:08

through tensor

play03:10

boom okay let's close this for now all

play03:12

right so those are our dependencies

play03:13

imported all we now need to do is import

play03:15

our data set so we don't have any data

play03:17

sets in our folders but some images that

play03:19

have already gone and downloaded which

play03:20

we'll be able to test out later on so

play03:22

let's download our data set so we're

play03:24

going to use the mnist data set which is

play03:26

an image classification data set there

play03:28

are 10 classes so 0 to 9 so we're going

play03:30

to be able to download that using the

play03:32

data sets

play03:34

class over here so train equals

play03:36

datasets.mnist

play03:39

and then we need to specify where we

play03:41

want to download it so root equals and

play03:42

then the folder so data

play03:44

and then we're going to specify download

play03:46

equals true because we need to download

play03:48

it and then we also need to download we

play03:50

want the train partition

play03:52

set that equal to true

play03:54

and then we need to specify any data

play03:56

transformations that we want to go ahead

play03:58

and apply so we're going to specify

play03:59

transform

play04:00

and we want to transform it to a tensor

play04:05

and then we are going to create our data

play04:06

set so data set equals

play04:09

data loader so we need to pass our train

play04:11

partition so this over here to this data

play04:13

load us to train and then specify how we

play04:16

want to batch up our data set so we're

play04:17

going to

play04:18

convert it into batches of 32 images

play04:21

cool so this is

play04:22

that's right two comments so import

play04:24

dependencies

play04:27

get data

play04:30

all right now we actually need to create

play04:31

our neural network class so

play04:33

the first thing that we're going to do

play04:34

is create a class and we're going to

play04:35

call it image classifier

play04:37

and we are going to subclass it from the

play04:39

neural network.module class from pi

play04:41

torch then we need two functions or two

play04:44

methods so definite

play04:47

and we also need a forward

play04:51

a forward method this is akin to

play04:54

the call method inside of

play04:57

tensorflow

play04:59

all right so we've initialized our first

play05:01

method so now what we need to do is

play05:03

create a model so this is where we

play05:05

actually stack all our layers so we are

play05:07

going to call self.models we'll create a

play05:09

new attribute equals nn.sequential so

play05:12

we're going to be using the sequential

play05:14

api from pytorch so it's coming from up

play05:17

here

play05:18

and then what we want to do is we're

play05:20

going to use convolutional neural

play05:21

network layers so uh so nn.conf 2d

play05:25

and then we need to specify what this is

play05:28

intense all right so the input channel

play05:30

is going to be one because our images

play05:32

are black and white then we want 32

play05:34

filters of shape 3x3 or 32 kernels

play05:38

all right so that's our one

play05:39

convolutional neural network layer then

play05:40

we need some activations

play05:43

to handle non-lyrics yeah

play05:44

non-linearities

play05:47

and then uh what are we doing so we're

play05:48

going to add in a bunch more

play05:49

convolutional neural network layers so

play05:51

we now need 32 as our input channels and

play05:53

then we're going to output 64.

play05:57

and then let's do one more set of

play05:59

convolutional neural network layer or

play06:00

conv conv layers and then we are going

play06:03

to be taking 64 from up here

play06:06

now without getting too deep into the

play06:07

math by applying each one of these

play06:09

layers with these specific parameters

play06:11

we're going to be shaving off two pixels

play06:13

off the height and width of each image

play06:15

so we need to adjust for that in our

play06:16

output when we apply a linear layer so

play06:18

first up we need to flatten these layers

play06:21

and then we're going to apply uh

play06:22

creating linear layers so nn dot linear

play06:26

and our shape so we need to pass through

play06:28

the input shape into the linear layer so

play06:30

it's going to be 64 channels because

play06:32

that's the final channel output from our

play06:35

last convolutional neural network layer

play06:37

multiplied by our images from mnist are

play06:40

in the shape of one comma 28 comma 28.

play06:45

so if we're applying

play06:47

three convolutional neural network

play06:48

layers with these specific parameters

play06:50

this specific stride and whatnot we're

play06:52

gonna be shaving off two pixels each

play06:53

time so we're effectively going to be

play06:54

shaving off two

play06:56

two and then two so it's minus six so

play06:59

it's going to be 68 multiplied or minus

play07:01

6

play07:02

sorry 28

play07:04

minus 6 then 28 minus 6 over here and

play07:08

then our output shape needs to be the

play07:09

number of classes so out we've got 10

play07:11

different classes so 0 to 9

play07:14

classes 0 to 9 so we need 10 outputs

play07:20

beautiful okay i think that's our neural

play07:22

network layer let me just quickly take a

play07:23

look at that um all right wait we need

play07:25

to subclass this model

play07:27

so we are going to call super

play07:30

dot

play07:31

init

play07:33

and that looks good and then our forward

play07:36

function or method is pretty simple so

play07:38

it's just going to

play07:39

take in our current instance and we're

play07:41

going to take in our x data and we are

play07:43

going to return

play07:44

self.model and then we're going to pass

play07:46

through our x value into that all right

play07:48

how we're looking how are we looking for

play07:49

time all right six minutes of we need to

play07:51

accelerate okay so that's our image

play07:53

classifier class

play07:57

uh neural network

play08:00

now what we need to do is set up a a

play08:02

couple of things so we need to create an

play08:04

instance

play08:06

of the neural network

play08:09

uh loss and our optimizer

play08:13

okay so instance of our neural network

play08:14

so we're going to call it classifiers to

play08:16

clf equals image classifier

play08:19

and then we need to say that we want to

play08:21

use our gpu so i've installed the gpu

play08:23

edition of torch on this so it's the

play08:25

kudo equivalent so we're going to send

play08:27

it to that device so we can type in dot

play08:29

2

play08:30

and then say cuda if you don't have a

play08:32

gpu then you can set this to cpu

play08:36

really need to stop talking and stop

play08:37

writing okay so that's our classifier

play08:39

cool i think that is good then we need

play08:41

to instantiate our optimizer

play08:43

and we uh imported our optimizer from up

play08:46

here i don't know why this turtle thing

play08:47

imports so our optimizer is atom so

play08:49

we're going to be using atom

play08:51

and then to that we need to pass our

play08:52

classifier and the parameters

play08:56

and then we need to specify our learning

play08:58

rate learning rate adjusts how fast or

play09:00

slow our neural network is going to

play09:01

learn so we're going to specify lr

play09:02

equals 1 e negative 3.

play09:05

cool all right so that's our optimizer

play09:07

then we need to create a loss function

play09:09

so loss function equals

play09:11

uh nn dot cross entropy

play09:14

loss

play09:16

beautiful okay so that's our

play09:19

neural network done that's our optimizer

play09:20

done that's our loss function done all

play09:22

right now what we need is a training

play09:23

function we are we're gonna make this

play09:25

seven minutes oh yeah we're gonna make

play09:27

this all right um

play09:31

okay so training

play09:34

uh flow so we're just gonna do it inside

play09:36

of like our typical function or like

play09:38

python function here

play09:40

that is still recording cool all right

play09:41

um

play09:43

so if

play09:44

breathe breathe

play09:47

remember your pressure point

play09:50

remember your pressure points okay if

play09:52

name

play09:54

equals equals

play09:56

main i finally want to make this

play09:58

hit the code that time limit all right

play10:00

so if name let's get rid of that equals

play10:02

main then what do we want to do so we

play10:04

want to run our deep learning training

play10:06

for a number of epochs that's

play10:07

effectively going through all of our

play10:09

batches so

play10:11

if actually four epoch in range

play10:16

range 10

play10:17

so this means we're going to be training

play10:19

for 10 epochs

play10:21

train for 10 epochs

play10:23

then for batch in

play10:27

data set so we're going to be looping

play10:29

through

play10:30

this over here

play10:33

then what we need to do is unpack the

play10:35

data that we're getting there so x and y

play10:37

are going to equal batch so we're going

play10:39

to unpack that data and then we need to

play10:41

send the x and y values to our gpu again

play10:43

so it's going to be x comma y equals

play10:47

x dot 2.

play10:49

cuda again if you've got a cpu only it's

play10:51

going to be true cpu

play10:53

y dot 2

play10:55

cuda

play10:57

so this is a little different compared

play10:58

to when you're working in tensorflow um

play11:00

all right then we need to make a

play11:01

prediction so y hat equals

play11:04

clf and then pass through x

play11:08

to generate a prediction yes that makes

play11:10

sense then we need to calculate

play11:12

loss so this is how you typically go

play11:15

through and actually build or build a

play11:17

flow input intense flow you just

play11:19

effectively go compile and then dot fit

play11:21

all right so we need to calculate our

play11:23

loss so our loss is going to be our loss

play11:24

function

play11:27

plus function and then we are passing

play11:29

through y hat and then y so that

play11:31

calculates our loss and then we actually

play11:33

need to go and apply backdrop apply

play11:35

back prop

play11:37

so first what we need to do is we need

play11:39

to zero out any existing gradients so we

play11:42

go opt.zero grad

play11:44

then we need to take or we need to

play11:46

calculate those gradients so loss got

play11:48

backward

play11:51

and then we need to go and take a step

play11:54

and effectively uh

play11:57

apply gradient descent so opt dot

play12:00

visit op dot step

play12:01

cool i think that's okay guys all right

play12:03

so then what we want to do is we want to

play12:05

print out our loss for every batch so

play12:07

print

play12:08

we are getting close print

play12:11

f and then we are going to

play12:14

the epoch

play12:16

so i'm just going to print out the epoch

play12:18

so we're going to print out what epoch

play12:20

we're currently up to and then we're

play12:21

just going to print out loss

play12:22

loss is

play12:24

and then we'll push loss dot item

play12:27

here

play12:29

then close that close that all right

play12:30

that looks good

play12:32

i think we're okay so what are we doing

play12:34

so let's just quickly check so we've got

play12:35

our dependencies

play12:37

utensil that's fine data loader

play12:40

we've gone and subclassed our model yes

play12:42

that's fine

play12:44

we've got our forward

play12:46

method

play12:47

instantiated all of this stuff

play12:50

gone and sent our data to the gpu gone

play12:52

and applied back prop we're then going

play12:54

to print out our epoch print out our

play12:56

lost oh last thing so we want to save

play12:58

down this model to our

play13:01

environment right so we actually want to

play13:03

go and save it here so let's go and save

play13:05

that so um with open we're going to call

play13:08

it uh

play13:09

model state

play13:11

dot pt

play13:13

and then we're going to write binary

play13:16

as f and then we need to oh actually we

play13:18

need to import some more stuff so we

play13:20

need to download we need to import save

play13:23

and then load from torch

play13:25

so we are going to

play13:28

save

play13:29

uh

play13:30

what's the format now

play13:34

it's model it'll be clf dot state it

play13:38

and then we are saving it as f i think

play13:41

that's right

play13:43

okay let's give it a crack all right so

play13:46

then we can run to run this we're just

play13:48

going to run python and then torch n.pie

play13:50

so python

play13:51

and if we successfully kick off training

play13:53

we'll pause the timer because come on

play13:55

guys you got to give me that at least

play13:56

all right uh so let's run this

play13:59

[Music]

play14:02

okay no errors so far this is looking

play14:04

promising

play14:05

all right so it's downloading the data

play14:07

set

play14:08

so we should have our data set popping

play14:10

up here you can see inside of data

play14:14

if we see a lost metric i'll pause it

play14:28

come on

play14:29

this is killing me

play14:37

yes all right

play14:40

guys

play14:41

we started trading

play14:43

oh man

play14:46

yes i think we're going to finally make

play14:48

this first coat that so you can see

play14:50

fewer that we are successfully printing

play14:52

out at epoch loss so epoch zero loss is

play14:56

0.02 then we're going to 0.01 then

play15:00

0.0002

play15:02

and once we finish training you should

play15:04

effectively see that we get our

play15:07

porch weight saved

play15:10

the intensity

play15:12

guys

play15:17

you can see that our neural network is

play15:19

currently training right

play15:22

oh i'm so happy and then we'll make a

play15:24

prediction i'm i'm counting this as a

play15:26

dub we hit it in what

play15:29

30 less than 13 minutes

play15:32

so what 12 minutes and 54 seconds that's

play15:34

got to be a new record

play15:38

got all the the gangster deep learning

play15:40

practitioners out there gonna be like

play15:41

nick you skipped over a ton of details i

play15:44

was like come on i had 15 minutes anyway

play15:46

we are chugging along

play15:48

so i effectively once our model gets to

play15:51

epoch

play15:52

what is that epoch 9 we should

play15:55

have our weight saved out because we

play15:57

went and wrote this

play15:59

and i'm going to share all this code as

play16:00

usual code should be in the description

play16:02

and you can give this a crack yourself

play16:04

all right cool we didn't get any errors

play16:07

so boom

play16:12

we've got to play some angel music in

play16:13

here

play16:17

oh guys your boy is happy

play16:19

so that is successfully how to go and

play16:21

build a neural network with pytorch so

play16:23

we did it

play16:24

i'm happy now so if we wanted to we

play16:26

could actually go and load up

play16:28

this um neural network and make a

play16:30

prediction which is why i've got these

play16:32

images here so

play16:34

should we try to get this done in two

play16:36

minutes

play16:38

i'm a little nervous all right let's do

play16:39

it so we've got two minutes left on the

play16:41

clock

play16:42

let's try to get the predictions done

play16:45

okay so we've successfully loaded

play16:46

downloaded or trained our model so now

play16:48

what we can do let me just minimize this

play16:51

so now what we can do is comment this

play16:53

stuff out so we don't need to go and

play16:54

train again

play16:56

and then what we'll do is we'll load up

play16:59

so we've got a classifier there so we'll

play17:01

then go with

play17:02

open

play17:03

and then we want to load up this so

play17:05

model state dot pt

play17:07

so with open model state dot pt

play17:11

read binary

play17:14

as f and then we are going to go load

play17:18

f and then we want to go clf dot load

play17:22

i think it's load state dick yep cool so

play17:25

that's going to load the weights into

play17:27

our classifier then in order to make

play17:29

predictions we need to import

play17:32

torch

play17:34

torch up here and we also need to import

play17:37

uh pillow to load our image so then what

play17:40

we can go ahead and do

play17:42

is load in our image so image equals

play17:44

kill i'll actually wait at some point so

play17:46

from

play17:48

kill import

play17:50

image

play17:52

so then we're going to import our image

play17:53

to image

play17:55

dot open

play17:57

and we're just going to open up image

play18:00

one right now which is a number two so

play18:03

uh image underscore one

play18:05

dot jpg

play18:07

and then what do we need to do so we

play18:09

need to convert that to a tensor so

play18:11

we're going to go uh image answer

play18:14

equals

play18:15

true tensor

play18:17

pass through our image and then we need

play18:19

to unsqueeze it because we want to pass

play18:23

through a single sample so we're gonna

play18:24

unsqueeze and then send it to our gpu so

play18:27

cuda

play18:28

and then we should be able to print

play18:32

classifier and then image tensor

play18:37

i think that might be okay

play18:40

all right let's run it

play18:44

and this should print out our prediction

play18:47

oh we need to uh so torch dot ugg max

play18:52

i'm gonna take this as a win guys

play18:55

right that's the timer up though

play18:59

so if i now go and run this

play19:03

boom it's predicting two

play19:07

how good is that

play19:09

i'm gonna take that as dub guys because

play19:10

we've managed to train that model and

play19:13

make predictions so if you take a look

play19:15

now right i can then go and load up

play19:17

image two

play19:20

all i need to do is change the image

play19:21

here

play19:23

and it should print out the class

play19:26

the tens of zero so image if we go and

play19:27

open up image two

play19:29

boom that's tense to zero so it's

play19:31

predicting the the class is zero

play19:33

we go and run it for let's go and do it

play19:35

for image three

play19:37

which is a number nine so if we go and

play19:39

pass through image three here

play19:48

boom number nine

play19:53

thank you so much for tuning in guys

play19:55

hopefully you've enjoyed this speed

play19:57

running building pie torch neural

play20:00

networks code's going to be in the

play20:01

description via my github if you guys

play20:02

want to take a crack i'll catch you in

play20:04

the next one peace thanks so much for

play20:06

tuning in guys hopefully you enjoyed

play20:07

this video if you did be sure to give it

play20:08

a big thumbs up hit subscribe and tick

play20:10

that bell it really does help and i

play20:12

really do appreciate you checking out

play20:14

this video if there's any feedback any

play20:16

challenges or anything you'd like to

play20:17

hear a little bit more about do let me

play20:19

know in the comments below thanks again

play20:20

for tuning in guys also let me know if

play20:22

you like this challenge and uh if we

play20:24

could do better next time thanks again

play20:26

for tuning in peace

play20:29

[Music]

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
PyTorchDeep LearningCoding ChallengeMachine LearningAI DevelopmentPython ProgrammingNeural NetworksTimed TutorialTech SpeedrunEducational Content
Besoin d'un résumé en anglais ?