# Cross Entropy Loss for a Sequence (Time Series) of Output?

**URL:** <https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309>\
**Category:** Uncategorized\
**Created:** [June 26, 2017, 1:01am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309 "2017-06-26T01:01:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlmacedo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dlmacedo/32/1387_2.png) [@dlmacedo](https://discuss.pytorch.org/u/dlmacedo)\
**Post date:** [June 26, 2017, 1:01am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309/1 "2017-06-26T01:01:43Z")

</div>

The output of my network is a tensor of size torch.Size([time\_steps, 20, 29]).

20 is the batch size, and 29 is the number of classes. time\_steps is variable and depends on the input.

My targets has the form torch.Size([time\_steps, 20]).

How can I calculate the loss using nn.CrossEntropyLoss function?

It should be noticed that the loss should be the sum of the loss of time\_steps cross entropy loss operations.

---

<div class="post-metadata">

**Author:** ![chenyuntc](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chenyuntc/32/299_2.png) [@chenyuntc](https://discuss.pytorch.org/u/chenyuntc)\
**Post date:** [June 26, 2017, 6:22am UTC](https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309/2 "2017-06-26T06:22:26Z")

</div>

```python
output = output.view(-1,29)
target = target.view(-1)
criterion = nn.CrossEntropyLoss(29)
loss = criterion(output,target)

```

---

<div class="post-metadata">

**Author:** ![dlmacedo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dlmacedo/32/1387_2.png) [@dlmacedo](https://discuss.pytorch.org/u/dlmacedo)\
**Post date:** [June 26, 2017, 11:55pm UTC](https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309/4 "2017-06-26T23:55:12Z")

</div>

Why do I need pass 29 to CrossEntropyLoss?

Could I just use nn.CrossEntropyLoss()?

---

<div class="post-metadata">

**Author:** ![DuaneNielsen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/duanenielsen/32/4158_2.png) [@DuaneNielsen](https://discuss.pytorch.org/u/DuaneNielsen)\
**Post date:** [May 13, 2018, 8:59pm UTC](https://discuss.pytorch.org/t/cross-entropy-loss-for-a-sequence-time-series-of-output/4309/5 "2018-05-13T20:59:23Z")

</div>

For pytorch 3,this worked fine for me

output = output.view(-1,29)  
target = target.view(-1)  
criterion = nn.CrossEntropyLoss()  
loss = criterion(output,target)
