# Multi Label Classification in pytorch

**URL:** <https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905>\
**Category:** Uncategorized\
**Created:** [March 6, 2017, 8:46am UTC](https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905 "2017-03-06T08:46:45Z")\
**Posts on this page:** 1\
**Showing post:** 45

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 31, 2018, 12:36am UTC](https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905/45 "2018-07-31T00:36:43Z")

</div>

Here is a very simple dummy example:

```python
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-in-pytorch/905)._
