# BCEWITHLOGITSLOSS binary classification

**URL:** <https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591>\
**Category:** vision\
**Created:** [February 22, 2021, 10:49am UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591 "2021-02-22T10:49:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alessio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alessio/32/33323_2.png) [@alessio](https://discuss.pytorch.org/u/alessio)\
**Post date:** [February 22, 2021, 10:49am UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/1 "2021-02-22T10:49:05Z")

</div>

Hey guys,  
I built a CNN for a binary classification task, so I’m using it as a loss function BCEWITHLOGITSLOSS.  
My dataset is unbalanced 24 positive examples 399 negatives; therefore, I want to use the pos\_weight parameter to counter this problem.  
But I’m not sure if I understood how to use the parameter correctly; here the code for the CNN and the pos\_weight initialization.

```auto
    def __init__ (self):
        super(CNNModel, self). __init__ ()

        self.conv_layer1 = self._conv_layer_set(1, 32)
        self.norm1 = nn.BatchNorm3d(32)
        self.conv_layer2 = self._conv_layer_set(32, 64)
        self.norm2 = nn.BatchNorm3d(64)
        self.conv_layer3 = self._conv_layer_set2(64, 64)
        self.fc1 = nn.Linear(64 * 1 * 21 * 23, 500)
        self.fc2 = nn.Linear(500, 100)
        self.bn1 = nn.LayerNorm(100)
        self.fc3 = nn.Linear(100, 1)
        self.Re = nn.ReLU()

        # self.softmax = nn.Softmax(dim=1)
        # self.sig = nn.Sigmoid()

    def _conv_layer_set(self, in_c, out_c):
        conv_layer = nn.Sequential(
            nn.Conv3d(in_c, out_c, kernel_size=(3, 3, 3), padding=0),
            nn.LeakyReLU(),
            nn.MaxPool3d(3, 3),
        )
        return conv_layer

    def _conv_layer_set2(self, in_c, out_c):
        conv_layer = nn.Sequential(
            nn.Conv3d(in_c, out_c, kernel_size=(3, 3, 3), padding=0),
            nn.LeakyReLU(),
            nn.MaxPool3d(2, 2),
        )
        return conv_layer

    def forward(self, x):
        # Set 1
        out = self.conv_layer1(x)
        out = self.norm1(out)
        out = self.conv_layer2(out)
        out = self.norm2(out)
        out = self.conv_layer3(out)
        out = self.norm2(out)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        out = self.Re(out)
        out = self.fc2(out)
        out = self.Re(out)
        out = self.bn1(out)
        out = self.fc3(out)

        return out
pos_weight = torch.Tensor([16]).to(device)
    criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
    optimizer = optim.Adam(net.parameters(), lr=0.1)

```

---

<div class="post-metadata">

**Author:** ![Dwight\_Foster](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dwight_foster/32/18182_2.png) [@Dwight\_Foster](https://discuss.pytorch.org/u/Dwight_Foster)\
**Post date:** [February 22, 2021, 2:39pm UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/2 "2021-02-22T14:39:27Z")

</div>

You could try looking at [this](https://discuss.pytorch.org/t/weights-in-bcewithlogitsloss/27452) post. You have to have the same number of weights as classes.

---

<div class="post-metadata">

**Author:** ![alessio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alessio/32/33323_2.png) [@alessio](https://discuss.pytorch.org/u/alessio)\
**Post date:** [February 23, 2021, 1:35pm UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/3 "2021-02-23T13:35:38Z")

</div>

thank you for your answer.  
In my case i have just to predict 0 or 1, so I assume i just need to use one weight for the positive class. right?

---

<div class="post-metadata">

**Author:** ![Dwight\_Foster](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dwight_foster/32/18182_2.png) [@Dwight\_Foster](https://discuss.pytorch.org/u/Dwight_Foster)\
**Post date:** [February 23, 2021, 1:47pm UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/4 "2021-02-23T13:47:13Z")

</div>

Yes. You’re right you should only need one weight.

---

<div class="post-metadata">

**Author:** ![alessio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alessio/32/33323_2.png) [@alessio](https://discuss.pytorch.org/u/alessio)\
**Post date:** [March 21, 2021, 1:59pm UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/5 "2021-03-21T13:59:23Z")

</div>

Hi again, I was wondering in BCEwithlogits loss the weight parameter has a different meaning from the BCEloss classic. The documentation gives a different definition in each of them.  
thank you in advance

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [March 22, 2021, 2:25am UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/6 "2021-03-22T02:25:30Z")

</div>

Hi Alessio!

> [@alessio](#):
>
> I was wondering in BCEwithlogits loss the weight parameter has a different meaning from the BCEloss classic.

I’m pretty sure that `BCELoss` and `BCEWithLogitsLoss` use  
their `weight` arguments in the same way. On the other hand,  
`BCEWithLogitsLoss` has a `pos_weight` argument, while `BCELoss`  
does not. I don’t know of any good reason for this – I’ve always  
assumed that it’s just an oversight.

Best.

K. Frank

---

<div class="post-metadata">

**Author:** ![alessio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alessio/32/33323_2.png) [@alessio](https://discuss.pytorch.org/u/alessio)\
**Post date:** [March 23, 2021, 6:09pm UTC](https://discuss.pytorch.org/t/bcewithlogitsloss-binary-classification/112591/7 "2021-03-23T18:09:19Z")

</div>

Thanks for the answer i will try to use the weights param and check how it goes.
