Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with denormals causing NaNs in phase loss #49

Open
Kinyugo opened this issue Jan 13, 2023 · 0 comments
Open

Fix issue with denormals causing NaNs in phase loss #49

Kinyugo opened this issue Jan 13, 2023 · 0 comments

Comments

@Kinyugo
Copy link

Kinyugo commented Jan 13, 2023

An issue with denormals in the phase loss calculation is causing the torch.angle function to return NaN values, as discussed here. A possible solution is to replace values close to zero with a certain threshold.

def replace_denormals(x: Tensor, threshold: float = 1e-10) -> Tensor:
    """Replace numbers close to zero to avoid NaNs in `angle`"""
    y = x.clone()
    y[torch.abs(x) < threshold] = threshold
    return y

def angle(x: Tensor) -> Tensor:
    """Calculates the angle of a complex or real tensor"""
    if torch.is_complex(x):
        x_real = x.real
        x_imag = x.imag
    else:
        x_real = x
        x_imag = torch.zeros_like(x_real)
    x_real = replace_denormals(x_real)
    x_imag = replace_denormals(x_imag)
    return torch.atan2(x_imag, x_real)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant