torch.isfinite
function isfinite<S extends Shape, D extends DType = DType, Dev extends DeviceType = DeviceType>(input: Tensor<S, D, Dev>): Tensor<S, 'bool', Dev>Returns a boolean tensor indicating which elements are finite (valid numbers).
Tests for finite values: NOT NaN and NOT ±∞. Finite values are normal, representable numbers in the valid floating-point range. Essential for:
- Validity checking: Ensuring all values are valid numbers (neither NaN nor ±∞)
- Data filtering: Removing invalid, missing, or exploded values
- Numerical stability: Confirming computations haven't diverged
- Algorithm safety: Checking preconditions before mathematical operations
- Quality control: Validating model outputs and intermediate computations
- Sanity checks: Ensuring tensor values are sane and usable
Finite encompasses all representable floating-point numbers in the valid range. It's the inverse of "invalid" (NaN or ±∞). For most practical ML code, you want all values to be finite. If you find non-finite values, something went wrong.
- Opposite of invalid: isfinite(x) = NOT (isnan(x) OR isinf(x))
- Not NaN, not ±∞: Finite includes all normal representable numbers
- Logical complement: NOT isfinite = isnan OR isinf
- Most common check: This is the general "is value OK?" test
- Subnormal numbers: Includes smallest representable positive numbers
- Float dtype: Only meaningful for floating-point dtypes
- Integer always finite: Integer tensors always return true
- Both infinity signs: Catches +∞ and -∞, not just one
Parameters
inputTensor<S, D, Dev>- The input tensor (any shape, any dtype)
Returns
Tensor<S, 'bool', Dev>– A boolean tensor same shape as input: true where element is finiteExamples
// Basic finite check
const x = torch.tensor([1.0, 2.0/0.0, 0.0/0.0, -3.0, -1.0/0.0]);
torch.isfinite(x); // [true, false, false, true, false]
// Filtering all invalid values (NaN and ±∞)
const data = torch.tensor([1.5, 2.0/0.0, 3.5, 0.0/0.0, -5.0/0.0, 4.5]);
const valid_data = torch.maskedSelect(data, torch.isfinite(data));
// valid_data: [1.5, 3.5, 4.5] - removed all invalid values
// Model output validation
const logits = torch.randn(100, 10); // Model predictions
const all_finite = torch.all(torch.isfinite(logits));
if (!all_finite.item()) {
console.error('Model produced invalid logits!');
// Handle divergence: clip, reset, fallback, etc.
}
// Numerical computation sanity check
const matrix = torch.randn(50, 50);
const eigenvalues = torch.linalg.eigvals(matrix);
const valid_eigs = torch.maskedSelect(eigenvalues, torch.isfinite(eigenvalues));
if (valid_eigs.shape[0] < eigenvalues.shape[0]) {
console.warn('Some eigenvalues are non-finite');
}
// Cleaning dataset with mixed valid/invalid values
const measurements = torch.tensor([10.5, 11.2, Infinity, 9.8, NaN, 12.1]);
const clean = torch.maskedSelect(measurements, torch.isfinite(measurements));
// clean: [10.5, 11.2, 9.8, 12.1] - removed Infinity and NaN
// Combining with other checks
const x = torch.randn(1000);
const is_finite = torch.isfinite(x);
const is_positive = torch.gt(x, 0.0);
const valid_positive = torch.logicalAnd(is_finite, is_positive);
// valid_positive: both finite AND positive
// Replacing non-finite values with defaults
const raw = torch.tensor([1.0, 2.0/0.0, 3.0, 0.0/0.0]);
const default_val = 0.0;
const cleaned = torch.where(torch.isfinite(raw), raw, torch.full_like(raw, default_val));
// cleaned: [1.0, 0.0, 3.0, 0.0]See Also
- PyTorch torch.isfinite()
- isnan - Check for NaN only
- isinf - Check for ±∞
- isposinf - Check for +∞ specifically
- isneginf - Check for -∞ specifically