# How to handle variable length inputs (sentences)

**URL:** <https://discuss.pytorch.org/t/how-to-handle-variable-length-inputs-sentences/5407>\
**Category:** Uncategorized\
**Created:** [July 27, 2017, 8:18am UTC](https://discuss.pytorch.org/t/how-to-handle-variable-length-inputs-sentences/5407 "2017-07-27T08:18:41Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![sinhasam](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sinhasam](https://discuss.pytorch.org/u/sinhasam)\
**Post date:** [July 27, 2017, 3:01pm UTC](https://discuss.pytorch.org/t/how-to-handle-variable-length-inputs-sentences/5407/3 "2017-07-27T15:01:23Z")

</div>

This link might help you with using pack\_padded\_sequence and pad\_packed\_sequence. The implementation is a bit trivial, but it simplifies things a lot.

> [@Simple working example how to use packing for variable-length sequence inputs for rnn](https://discuss.pytorch.org/t/simple-working-example-how-to-use-packing-for-variable-length-sequence-inputs-for-rnn/2120):
>
> Hi, Updated - here’s a simple example of how I think you use pack\_padded\_sequence and pad\_packed\_sequence, but I don’t know if it’s the right way to use them? import torch import torch.nn as nn from torch.autograd import Variable batch\_size = 3 max\_length = 3 hidden\_size = 2 n\_layers =1 # container batch\_in = torch.zeros((batch\_size, 1, max\_length)) #data vec\_1 = torch.FloatTensor([[1, 2, 3]]) vec\_2 = torch.FloatTensor([[1, 2, 0]]) vec\_3 = torch.FloatTensor([[1, 0, 0]]) batch\_in[0] = vec\_1…

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-handle-variable-length-inputs-sentences/5407)._
