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.

```
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)
```