import torch


def normalize_last_dim(tensor, eps=1e-6):
    """
    Normalizes to a mean of 0 and a norm of 1.

    :param tensor: a ND tensor
    :param eps: epsilon to add not to divide by 0
    :return: the tensor with normalization along last dimension axis.
    """
    tensor = tensor - tensor.mean(-1, keepdim=True)
    tensor = tensor / (torch.norm(tensor, p=2, dim=-1, keepdim=True) + eps)
    return tensor