def unpad(x, x_mask):
    """
    Unpad a batch of sequences by selecting elements not masked by x_mask.
    Returns a list of sequences and their corresponding lengths
    """
    x_unp = []
    x_unp_mask = []
    x_unp_len = []
    for seq, seq_mask in zip(x, x_mask):
        seq_unp = seq[(1 - seq_mask)]
        x_unp.append(seq_unp)
        x_unp_mask.append(seq_mask[1 - seq_mask])
        x_unp_len.append(seq_unp.shape[0])
    return x_unp, x_unp_mask, x_unp_len