import torch


def nanmean(v: torch.Tensor, *args, **kwargs) -> torch.Tensor:
    """
    https://github.com/pytorch/pytorch/issues/21987#issuecomment-539402619

    :param v: A tensor
    :param args: Arguments one might pass to `mean` like `dim`.
    :param kwargs: Arguments one might pass to `mean` like `dim`.
    :return: The mean, excluding nans.
    """
    v = v.clone()
    is_nan = torch.isnan(v)
    v[is_nan] = 0
    return v.sum(*args, **kwargs) / (~is_nan).float().sum(*args, **kwargs)