def l2_error(X, X_ref, relative=False, squared=False):
    """ Compute average l2-error.

    Parameters
    ----------
    X : torch.Tensor
        The input tensor of shape [..., 1, N].
    X_ref : torch.Tensor
        The reference tensor of same shape.
    relative : bool, optional
        Use relative error. (Default False)
    squared : bool, optional
        Use squared error. (Default False)
    use_magnitude : bool, optional
        Use complex magnitudes. (Default True)

    Returns
    -------
    err_av :
        The average error.
    err :
        Tensor with individual errors.

    """
    if squared:
        err = (X - X_ref).norm(p=2, dim=-1) ** 2
    else:
        err = (X - X_ref).norm(p=2, dim=-1)

    if relative:
        if squared:
            err = err / (X_ref.norm(p=2, dim=-1) ** 2)
        else:
            err = err / X_ref.norm(p=2, dim=-1)

    if X_ref.ndim > 1:
        err_av = err.sum() / X_ref.shape[0]
    else:
        err_av = err.sum()
    return err_av, err