Easiest way to check for `nan` value in a model parameters

What would be the easiest way to detect if any of the weights of a model is nan?

Is there a built in function for that?

You might be looking for Automatic differentiation package - torch.autograd ā€” PyTorch 1.13 documentation

Besides the mentioned Anomaly detection util., this small code snippet would also check for NaNs in registered parameters:

model = models.resnet101()
is_nan = torch.stack([torch.isnan(p).any() for p in model.parameters()]).any()
1 Like

@ptrblck , It looks like what Iā€™m after.
But if I want it to return a bool, should I just bool(is_nan)?

is_nan.item() will return a Python bool.

1 Like