22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Output

tensor([[-1.5229, -0.3146, -2.9600],

[-1.7934, -1.0044, -0.7607],

[-1.2513, -1.0136, -1.0471],

[-2.6799, -0.2219, -2.0367],

[-1.0728, -1.9098, -0.6737]])

Can you hand-pick the log probabilities that are going to be

actually used in the loss computation?

relevant_log_probs = torch.tensor([-1.5229, -1.7934, -1.0136,

-2.0367, -1.9098])

-relevant_log_probs.mean()

Output

tensor(1.6553)

Now, let’s use nn.NLLLoss() to create the actual loss function, and then use

predictions and labels to check if we got the relevant log probabilities right:

loss_fn = nn.NLLLoss()

loss_fn(dummy_log_probs, dummy_labels)

Output

tensor(1.6553)

Right, indeed! What if we want to balance our dataset, giving data points with label

(y=2) double the weight of the other classes?

loss_fn = nn.NLLLoss(weight=torch.tensor([1., 1., 2.]))

loss_fn(dummy_log_probs, dummy_labels)

A Multiclass Classification Problem | 381

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

Saved successfully!

Ooh no, something went wrong!