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

argument of nn.BCEWithLogitsLoss(). To compensate for the imbalance, one canset the weight to equal the ratio of negative to positive examples:In our imbalanced dummy example, the result would be 3.0. This way, every pointin the positive class would have its corresponding loss multiplied by three. Sincethere is a single label for each data point (c = 1), the tensor used as an argument forpos_weight has only one element: tensor([3.0]). We could compute it like this:n_neg = (dummy_imb_labels == 0).sum().float()n_pos = (dummy_imb_labels == 1).sum().float()pos_weight = (n_neg / n_pos).view(1,)pos_weightOutputtensor([3])Now, let’s create yet another loss function, including the pos_weight argument thistime:loss_fn_imb = nn.BCEWithLogitsLoss(reduction='mean',pos_weight=pos_weight)Then, we can use this weighted loss function to compute the loss for ourimbalanced dataset. I guess one would expect the same loss as before; after all,this is a weighted loss. Right?loss = loss_fn_imb(dummy_imb_logits, dummy_imb_labels)lossLoss | 229

Outputtensor(0.2464)Wrong! It was 0.1643 when we had two data points, one of each class. Now it is0.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 youwould expect from a weighted average:Equation 3.16 - Weighted average of lossesBut this is what PyTorch does:Equation 3.17 - PyTorch’s BCEWithLogitsLossSee the difference in the denominator? Of course, if you multiply the losses of thepositive examples without multiplying their count (N pos ), you’ll end up with anumber larger than an actual weighted average."What if I really want the weighted average?"230 | Chapter 3: A Simple Classification Problem

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!