13 – PyTorch V2 Part 5 V1 (1)

Hey there. So now, we’re going to start talking about inference and validation. So, when you have your trained network, you typically want to use it for making predictions. This is called inference, it’s a term borrowed from statistics. However, neural networks have a tendency to perform too well on your training data and they aren’t able to generalize the data that your network hasn’t seen before. This is called overfitting. This happens because as you’re training more and more and more on your training set, your network starts to pick up correlations and patterns that are in your training set but they aren’t in the more general dataset of all possible handwritten digits. So, to test for overfitting, we measure the performance of the network on data that isn’t in the training set. This data is usually called the validation set or the test set. So, while we measure the performance on the validation set, we also tried to reduce overfitting through regularization such as dropout. So, in this notebook, I’ll show you how we can both look at our validation set and also use dropout to reduce overfitting. So, to get the training set for your data like from PyTorch, then we say train equals true and for fashionMNIST. To get our test set, we’re actually going to set train equals false here. Here, I’m just defining the model like we did before. So, the goal of validation is to measure our model’s performance on data that is not part of our training set. But what we mean by performance is up to you, up to the developer, the person who’s writing the code. A lot of times, it’ll just be the accuracy. So, like how many correct classifications did our model make compared to all of the predictions? And other options for metrics are precision and recall, and the top five error rate. So here, I’ll show you how to actually measure the accuracy on the validation set. So first, I’m going to do a forward pass that is one batch from the test set. So, see in our test set we get our probabilities. So, just 64 examples in a batch. Then 10 columns like one for each of the classes. So, the accuracy, we want to see if our model made the correct prediction of the class given the image. The prediction we can consider it to be whichever class has the highest probability. So, for this, we can use this top-k method on our tensors. This returns the k highest values. So, if we pass in one, then this is going to give us the one highest value. This one highest value is the most likely class that our network is predicting. So, for the first ten examples, and this batch of test data that I grabbed, we see that the class four and class five are what are being predicted for these. So, remember that this network actually hasn’t been trained yet, and so it’s just making these guesses randomly because it doesn’t really know anything about the data yet. So, top-k actually returns a tuple with two tensors. So, the first tensor is the actual probability values, and the second tensor are the class indices themselves. So typically, we just want this top class here. So, I’m calling top-k here and I’m separating out the probabilities in the classes. So, we’ll just use this top class going forward. So, now that we have the predicted classes from our network, we can compare that with the true labels. So, we say, we can say like top class equals equals labels. The only trick here is that we need to make sure our top class tensor and the labels tensor has the same shape. So, this equality actually operates appropriately like we expect. So, labels from the test loader is actually a 1D tensor with 64 elements, but top class itself is a 2D tensor, 64 by one. So here, I’m just like changing the shape of labels to match the shape of top class. This gives us this equals tensor. We can actually see it looks like. So, it gives us a bunch of zeros and ones. So, zeros are where they don’t match, and then ones are where they do match. Now, we have this tensor that’s all just a bunch of zeros and ones. So, if we want to know the accuracy, right? We can just sum up all the correct things, all the correct predictions, and then divide by the total number of predictions. If you’re tensor is all zeros and ones, that’s actually equivalent to taking the mean. So for that, we can do torch.mean, but the problem is that equals is actually a byte tensor, and torch.mean won’t work on byte tensors. So, we actually need to convert equals until a float tensor. If we do that, then we can actually see our accuracy for this one particular batch is 15.6 percent. So, this is roughly what we expect. So, our network hasn’t been trained yet. It’s making pretty much random guesses. That means that we should see our accuracy be about one in ten for any particular image because it’s just uniformly guessing one of the classes, okay? So here, I’m going to have you actually implement this validation loop, where you’ll pass in data from the test set through the network and calculate the loss and the accuracy. So, one thing to note, I think I mentioned this before. For the validation paths, we’re not actually going to be doing any training. So, we don’t need the gradients. So, you can actually speed up your code a little bit if you turn off the gradients. So, using this context, so with torch.no_grad, then you can put your validation pass in here. So, for images and labels in your test loader and then do the validation pass here. So, I’ve basically built a classifier for you, set all this up. Here’s the training pass, and then it’s up to you to implement the validation pass, and then print out the accuracy. All right. Good luck, and if you get stuck or want any help, be sure to check out my solution.

%d 블로거가 이것을 좋아합니다: