# Transformer decoder training. Batch slice

**URL:** <https://discuss.pytorch.org/t/transformer-decoder-training-batch-slice/125720>\
**Category:** nlp\
**Created:** [July 3, 2021, 3:44pm UTC](https://discuss.pytorch.org/t/transformer-decoder-training-batch-slice/125720 "2021-07-03T15:44:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jubick](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jubick/32/22454_2.png) [@jubick](https://discuss.pytorch.org/u/jubick)\
**Post date:** [July 3, 2021, 3:44pm UTC](https://discuss.pytorch.org/t/transformer-decoder-training-batch-slice/125720/1 "2021-07-03T15:44:40Z")

</div>

So for the transformer decoder we want to feed embedded tokens but not EOS tokens.  
Many tutorials do it like x[:,:-1] or x[:-1,:] (BxL or LxB B-batch size N-sequence length)  
It makes sense for one sentence “BOS Bob is cool EOS” → “BOS Bob is cool” as we don’t want anything to be predicted after last passed token.  
But in batch this isn’t always true.  
I don’t understand how to do this slice in batch where not all of the sequences got same length.  
For example “BOS Bob is cool EOS” → “BOS Bob is cool” but “BOS Need help EOS PAD” → “BOS Need help EOS” if we simply slice last element.  
Surely I’ve added ignore\_index to loss but I’m not sure if that’s all we need.  
Any help appreciated!

---

<div class="post-metadata">

**Author:** ![jimmykobe](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jimmykobe/32/46195_2.png) [@jimmykobe](https://discuss.pytorch.org/u/jimmykobe)\
**Post date:** [January 30, 2022, 8:07am UTC](https://discuss.pytorch.org/t/transformer-decoder-training-batch-slice/125720/2 "2022-01-30T08:07:19Z")

</div>

May I ask have you solved this issue?
