code: def collate_fn(data):
      """Creates mini-batch tensors from the list of tuples (image, caption).
  
      We should build custom collate_fn rather than using default collate_fn,
      because merging caption (including padding) is not supported in default.
  
      Args:
          data: list of tuple (image, caption).
              - image: torch tensor of shape (3, 256, 256).
              - caption: torch tensor of shape (?); variable length.
  
      Returns:
          images: torch tensor of shape (batch_size, 3, 256, 256).
          targets: torch tensor of shape (batch_size, padded_length).
          lengths: list; valid length for each padded caption.
      """
      # Sort a data list by caption length (descending order).
      data.sort(key=lambda x: len(x[1]), reverse=True)
      images, captions = zip(*data)
  
      # Merge images (from tuple of 3D tensor to 4D tensor).
      images = torch.stack(images, 0)
  
      # Merge captions (from tuple of 1D tensor to 2D tensor).
      lengths = [len(cap) for cap in captions]
      targets = torch.zeros(len(captions), max(lengths)).long()
      for i, cap in enumerate(captions):
          end = lengths[i]
          targets[i, :end] = cap[:end]
      return images, targets, lengths
  
file_path: pytorch-tutorial/tutorials/03-advanced/image_captioning/data_loader.py
imports: import torch
  import torchvision.transforms
  import torch.utils.data
  import os
  import pickle
  import numpy
  import nltk
  from PIL import Image
  from build_vocab import Vocabulary
  from pycocotools.coco import COCO
label_desc: Use image node_id 1 for calling a global function with example usage: collate_fn(data) and returns: images,  targets,  lengths