import torch


def encodeOneHot(tensor, n_dim):
    """
    One hot encoding for a given tensor
    :param tensor: (torch Tensor)
    :param n_dim: (int) Number of dimensions
    :return: (torch.Tensor)
    """
    encoded_tensor = torch.Tensor(tensor.shape[0], n_dim).zero_().to(tensor.device)
    return encoded_tensor.scatter_(1, tensor.data, 1.)