RuntimeError: Function 'AcosBackward0' returned nan values in its 0th output.

When I calculated the spectral angles of the two images in the program, I get a error:"RuntimeError: Function ‘AcosBackward0’ returned nan values in its 0th output."I tried the suggestions on the Internet, and used the torch.clamp function to limited the data range to (-1, 1) before the arccos operation. but the problem is not solved.Below is my code:

def forward(self, lr_image, sr_image) -> torch.Tensor:
    # 将 SR 下采样至与 LR 尺寸相同
    sr_image = F.interpolate(sr_image, scale_factor=1 / 3, mode='bicubic')

    assert lr_image.ndim == 4 and lr_image.shape == sr_image.shape

    dot_sum = torch.sum(lr_image * sr_image, dim=1)

    # 求范数
    norm_lr = torch.linalg.norm(lr_image, dim=1)
    norm_sr = torch.linalg.norm(sr_image, dim=1)

    res = torch.arccos(torch.clamp(input=dot_sum / norm_lr / norm_sr, min=-0.99999, max=0.99999))

    is_nan = torch.nonzero(torch.isnan(res), as_tuple=True)

    for c, x, y in zip(is_nan[0], is_nan[1], is_nan[2]):
        res[c, x, y] = 0

    return torch.mean(res)
1 Like