import torch


def add_noise(x_data, noise_std_percent):
    """
    Adds normal noise to a dataset
    @param noise_std_percent tensor with standard deviation of the noise,
    as percent of the mean of the magnitude for that dimension
    @return x_data_, same as x_data but with noise added to it
    """
    assert (isinstance(x_data, torch.Tensor))
    x_data_ = torch.clone(x_data)
    noise_std = noise_std_percent * torch.mean(torch.abs(x_data), dim=0)
    eps = torch.randn(x_data.shape, dtype=x_data.dtype)
    x_data_ += eps * noise_std
    return x_data_