import torch


def padding_mask(x_lens):
    """
    transform lengths of samples to a binary mask.
    inputs:
        - x_lens: length of each sample in minibatch. # tensor # (batch_size, )  
    outputs:
        - mask: 1-0 binary mask. 1 means valid and 0 means invalid.
            # tensor # (batch_size, longest_time_step, 1)
    """
    longest_len = max(x_lens)
    batch_size = len(x_lens)
    mask = torch.zeros(batch_size, longest_len, 1)
    for i, x_len in enumerate(x_lens):
        mask[i, :x_len] = 1.
    return mask