original_docstring:  eates 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.
      
docstring: eates 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.
docstring_tokens: eates 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 .
code: def collate_fn_sqn(data):
      """ eates 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)
      frames, captions, seqs = zip(*data)
  
      # Merge images (from tuple of 3D tensor to 4D tensor).
      frames = torch.stack(frames, 0)
      seqs = torch.stack(seqs, 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 frames, targets, seqs, lengths
code_tokens: def collate_fn_sqn ( data ) : data . sort ( key = lambda x : len ( x [ 1 ] ) , reverse = True ) frames , captions , seqs = zip ( * data ) frames = torch . stack ( frames , 0 ) seqs = torch . stack ( seqs , 0 ) 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 frames , targets , seqs , lengths
short_docstring: eates mini-batch tensors from the list of tuples (image, caption).
short_docstring_tokens: eates mini - batch tensors from the list of tuples ( image caption ) .
comment: """ eates 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.
      """$SEP$# Sort a data list by caption length (descending order).$SEP$# Merge images (from tuple of 3D tensor to 4D tensor).$SEP$# Merge captions (from tuple of 1D tensor to 2D tensor).