Why we need collate_fn()?

The input of collate_fn is a list of training examples which are returned by getitem. Why we process data in collate_fn instead of getitem? I don’t know what the difference is.

You could use a custom collate_fn to create batches containing tensors with e.g. different shapes.
The default collate function creates a batch by stacking the tensors in dim0. However, this is not possible, if the spatial size of each tensor is different, so that a custom collate function gives you more flexibility to create the batches for your use case.

Yes, i have read some answers about that. But what confuse me is why not limit the tensor shape in __getitem__(). For example, i can reshape a tensor by truncating or padding in __getitem__() to meet requirements. Maybe collate_fn is more flexible, but is there any further difference?

Creating tensors with the same shape would be the default approach and thus the default collate function will just work.
However, a custom collate function allows you to work with more flexible approaches.
Not providing this method would limit some use cases.

Well, i haven’t met such a condition. Maybe i will use it in the future.
Thanks for your help!