Dear K. Franck,
Thanks for you answer.
I am using pytorch version 1.4.0
No error happens when I don’t use pos_weight and an additional dimension simultaneously so it was a good guess to suspect an interaction.
Both:
import torch
import torch.nn.functional as F
print(torch.__version__)
batch_size = 8
groups = 2
num_classes = 10
print(pos_weight)
criterion = nn.BCEWithLogitsLoss()
x = torch.rand(batch_size, num_classes, groups)
print(x.shape)
# print(x)
y = torch.empty(batch_size, dtype=torch.long).random_(num_classes)
print(y.shape)
y = y.unsqueeze(1).expand(-1,groups)
print(y.shape)
y = F.one_hot(y, num_classes).type_as(x).transpose(1, 2)
print(y.shape)
# print(y)
criterion(x,y)

and
import torch
import torch.nn.functional as F
print(torch.__version__)
batch_size = 8
groups = 2
num_classes = 10
pos_weight = (num_classes-1.0)*torch.ones([num_classes])
print(pos_weight)
criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
x = torch.rand(batch_size, num_classes)
print(x.shape)
# print(x)
y = torch.empty(batch_size, dtype=torch.long).random_(num_classes)
print(y.shape)
y = F.one_hot(y, num_classes).type_as(x)
print(y.shape)
# print(y)
criterion(x,y)

work just fine. When used, pos_weight is:
tensor([9., 9., 9., 9., 9., 9., 9., 9., 9., 9.])
which looks like what is expected by the function according to the documentation: pos_weight ( Tensor  1 , optional ) – a weight of positive examples. Must be a vector with length equal to the number of classes.
It happens that the following works:
import torch
import torch.nn.functional as F
print(torch.__version__)
batch_size = 8
groups = 2
num_classes = 10
pos_weight = (num_classes-1.0)*torch.ones([num_classes, groups])
print(pos_weight)
criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
x = torch.rand(batch_size, num_classes, groups)
print(x.shape)
# print(x)
y = torch.empty(batch_size, dtype=torch.long).random_(num_classes)
print(y.shape)
y = y.unsqueeze(1).expand(-1,groups)
print(y.shape)
y = F.one_hot(y, num_classes).type_as(x).transpose(1, 2)
print(y.shape)
# print(y)
print(criterion(x,y))

with output:
1.4.0
tensor([[9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.],
        [9., 9.]])
torch.Size([8, 10, 2])
torch.Size([8])
torch.Size([8, 2])
torch.Size([8, 10, 2])
tensor(1.3508)

So pos_weight should have the same size as input and target (without the batch axis) but this is not clear in the documentation.
Best regards,
Georges.