# Multi-label classification as array output in pytorch

**URL:** <https://discuss.pytorch.org/t/multi-label-classification-as-array-output-in-pytorch/105599>\
**Category:** Uncategorized\
**Created:** [December 9, 2020, 4:12am UTC](https://discuss.pytorch.org/t/multi-label-classification-as-array-output-in-pytorch/105599 "2020-12-09T04:12:48Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![shartzog](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shartzog/32/27488_2.png) [@shartzog](https://discuss.pytorch.org/u/shartzog)\
**Post date:** [December 12, 2020, 4:04pm UTC](https://discuss.pytorch.org/t/multi-label-classification-as-array-output-in-pytorch/105599/4 "2020-12-12T16:04:56Z")

</div>

I’m still struggling to understand your use case, so I’m not sure this will help, but to multi-hot encode a class list dataset with integer labels of:  
Sample 0: [3, 7, 4]  
Sample 1: [2, 4]  
Sample 2: [9, 1, 6, 2]  
Sample 3: [8, 9]  
you’d transform it into something like:

```auto
Label: 0 1 2 3 4 5 6 7 8 9 ... N
           | | | | | | | | | | |
           v v v v v v v v v v v
Sample 0: [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, ..., 0]
Sample 1: [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ..., 0]
Sample 2: [0, 1, 1, 0, 0, 0, 1, 0, 0, 1, ..., 0]
Sample 3: [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ..., 0]

```

where N is the total number of classes. Once you’ve transformed the labels, it’s pretty straightforward to put together a simple CNN using [BCELossWithLogits](https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html). This is a common strategy for basic CNNs and is the basis of most “multi-label CNN” tutorials, but you’ll notice it does NOT preserve order or provide bounding boxes for detected entities/labeled objects. If you’re looking for that more advanced functionality, I’d recommend looking at:

> **[GitHub - AlexeyAB/darknet: YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks...](https://github.com/AlexeyAB/darknet)**
>
> YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet ) - GitHub - AlexeyAB/darknet: YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object ...

but that would take you out of the torch framework altogether. Hopefully, someone more knowledgeable can comment and point you in the right direction for using torch. If not, here’s a link to a post that discusses multi-label classification problems in general. You might find some inspiration there…

> [@Multi Label Classification in pytorch](https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905/45):
>
> Here is a very simple dummy example: model = nn.Linear(20, 5) # predict logits for 5 classes x = torch.randn(1, 20) y = torch.tensor([[1., 0., 1., 0., 0.]]) # get classA and classC as active criterion = nn.BCEWithLogitsLoss() optimizer = optim.SGD(model.parameters(), lr=1e-1) for epoch in range(20): optimizer.zero\_grad() output = model(x) loss = criterion(output, y) loss.backward() optimizer.step() print('Loss: {:.3f}'.format(loss.item()))

---

_[View the full topic](https://discuss.pytorch.org/t/multi-label-classification-as-array-output-in-pytorch/105599)._
