22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Output

tensor(0.2464)

Wrong! It was 0.1643 when we had two data points, one of each class. Now it is

0.2464, even though we assigned a weight to the positive class.

"Why is it different?"

Well, it turns out, PyTorch does not compute a weighted average. Here’s what you

would expect from a weighted average:

Equation 3.16 - Weighted average of losses

But this is what PyTorch does:

Equation 3.17 - PyTorch’s BCEWithLogitsLoss

See the difference in the denominator? Of course, if you multiply the losses of the

positive examples without multiplying their count (N pos ), you’ll end up with a

number larger than an actual weighted average.

"What if I really want the weighted average?"

230 | Chapter 3: A Simple Classification Problem

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

Saved successfully!

Ooh no, something went wrong!