Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Figure 5.22 - Feature maps (classifier)The hidden layer performed an affine transformation (remember those?), reducingthe dimensionality from sixteen to ten dimensions. Next, the activation function, aReLU, eliminated negative values, resulting in the "activated" feature space in themiddle row.Finally, the output layer used these ten values to compute three logits, one foreach class. Even without transforming them into probabilities, we know that thelargest logit wins. The largest logit is shown as the brightest pixel, so we can tellwhich class was predicted by looking at the three shades of gray and picking theindex of the brightest one.The classifier got eight out of ten right. It made wrong predictions for images #6and #8. Unsurprisingly, these are the two images that got their vertical linessuppressed. The filter doesn’t seem to work so well whenever the vertical line istoo close to the left edge of the image."How good is the model actually?"Good question! Let’s check it out.AccuracyIn Chapter 3, we made predictions using our own predict() method and usedScikit-Learn’s metrics module to evaluate them. Now, let’s build a method that alsotakes features (x) and labels (y), as returned by a data loader, and that takes allnecessary steps to produce two values for each class: the number of correctpredictions and the number of data points in that class.Visualizing Filters and More! | 407

StepByStep Methoddef correct(self, x, y, threshold=.5):self.model.eval()yhat = self.model(x.to(self.device))y = y.to(self.device)self.model.train()# We get the size of the batch and the number of classes# (only 1, if it is binary)n_samples, n_dims = yhat.shapeif n_dims > 1:# In a multiclass classification, the largest logit# always wins, so we don't bother getting probabilities# This is PyTorch's version of argmax,# but it returns a tuple: (max value, index of max value)_, predicted = torch.max(yhat, 1)else:n_dims += 1# In binary classification, we NEED to check if the# last layer is a sigmoid (and then it produces probs)if isinstance(self.model, nn.Sequential) and \isinstance(self.model[-1], nn.Sigmoid):predicted = (yhat > threshold).long()# or something else (logits), which we need to convert# using a sigmoidelse:predicted = (torch.sigmoid(yhat) > threshold).long()# How many samples got classifiedcorrectly for each classresult = []for c in range(n_dims):n_class = (y == c).sum().item()n_correct = (predicted[y == c] == c).sum().item()result.append((n_correct, n_class))return torch.tensor(result)setattr(StepByStep, 'correct', correct)If the labels have two or more columns, it means we’re dealing with a multiclass408 | Chapter 5: Convolutions

StepByStep Method

def correct(self, x, y, threshold=.5):

self.model.eval()

yhat = self.model(x.to(self.device))

y = y.to(self.device)

self.model.train()

# We get the size of the batch and the number of classes

# (only 1, if it is binary)

n_samples, n_dims = yhat.shape

if n_dims > 1:

# In a multiclass classification, the largest logit

# always wins, so we don't bother getting probabilities

# This is PyTorch's version of argmax,

# but it returns a tuple: (max value, index of max value)

_, predicted = torch.max(yhat, 1)

else:

n_dims += 1

# In binary classification, we NEED to check if the

# last layer is a sigmoid (and then it produces probs)

if isinstance(self.model, nn.Sequential) and \

isinstance(self.model[-1], nn.Sigmoid):

predicted = (yhat > threshold).long()

# or something else (logits), which we need to convert

# using a sigmoid

else:

predicted = (torch.sigmoid(yhat) > threshold).long()

# How many samples got classified

correctly for each class

result = []

for c in range(n_dims):

n_class = (y == c).sum().item()

n_correct = (predicted[y == c] == c).sum().item()

result.append((n_correct, n_class))

return torch.tensor(result)

setattr(StepByStep, 'correct', correct)

If the labels have two or more columns, it means we’re dealing with a multiclass

408 | Chapter 5: Convolutions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!