How to use collate_fn()

juhyung: <p>Hi,<br>
I am not sure with what collate_fn does.<br>
is there any example that helps understanding what it does?</p>

ptrblck: <p>You can use your own <code>collate_fn</code> to process the list of samples to form a batch.<br>
The <code>batch</code> argument is a list with all your samples. E.g. if you would like to return variable-sized data, have a look at <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/3?u=ptrblck">this thread</a>.</p>

Brando_Miranda: <p>so as ptrblck said the <code>collate_fn</code> is your callable/function that processes the batch you want to return from your dataloader. e.g.</p>
<pre><code class="lang-auto">    def collate_fn(batch):
        print(type(batch))
        print(len(batch))
</code></pre>
<p>in my case of <code>batch_size=4</code> will return a list of size four. Lets check it:</p>
<pre><code class="lang-auto">&lt;class 'list'&gt;
4
</code></pre>

Paulo_Mann: <p>I have recently answered some other post with a similar question. But basically, the <code>collate_fn</code> receives a <strong>list of tuples</strong> if your <code>__getitem__</code> function from a <code>Dataset</code> subclass returns a tuple, or just a <strong>normal list</strong> if your <code>Dataset</code> subclass returns only one element. Its main objective is to create your batch without spending much time implementing it manually. Try to see it as a glue that you specify the way examples stick together in a batch. If you don’t use it, PyTorch only put <code>batch_size</code> examples together as you would using <code>torch.stack</code> (not exactly it, but it is simple like that).</p>
<p><a href="https://discuss.pytorch.org/t/how-to-create-batches-of-a-list-of-varying-dimension-tensors/50773/14">The following code I wrote on this post should help you grasp the real understanding</a>. It pads sequences with 0 until the maximum sequence size of the batch, that is why I need the <code>collate_fn</code>, because a standard batching algorithm (simply using <code>torch.stack</code>) won’t work in my case, and I need to manually pad different sequences with variable length to the same size before creating the batch.</p>

Ximing_Dong11104: <p>where is the parameter ‘batch’ come from?</p>

victorvargass: <p>Batch parameter is implicit in the Dataloader function.<br>
You just have to pass the collate function name to collate_fn Dataloader parameter.</p>
<p>Here’s an example:</p>
<pre><code class="lang-auto">loader_collate = DataLoader(
    dataset, shuffle=True, batch_size=5, collate_fn=collate_fn)
</code></pre>

RachelShalom: <p>very cool example <a class="mention" href="/u/paulo_mann">@Paulo_Mann</a> , also possible to it with pytorch pad_sequences function ( which I am not sure was there in 2018:))</p>

knosing: <p>Thanks, this helped me. However, I’m still wondering what gets passed in <code>batch</code> parameter of <code>collate_fn</code>?</p>

JuyiLin: <p>I am wondering when we call <code>collate_fn</code><br>
<code>for batch in loader:</code>, it will call <code>collate_fn</code> once or n times?</p>

chenqiyuan1012: <p>The <code>collate_fn</code> will be called every batch</p>