import torch


def long_tensor_to_onehot(idx, max_idx):
    """ from a one-dimension LongTensor to get the onehot vector. 
    >>> long_tensor_to_onehot(torch.LongTensor([1,0,2]), 3)
    tensor([[ 0,  1,  0],
            [ 1,  0,  0],
            [ 0,  0,  1]])
    """
    return torch.zeros(idx.size()[0], max_idx).scatter_(1, idx.view(-1,1), 1).long()