torch.isneginf
function isneginf<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 negative infinity (-∞).
Tests specifically for negative infinity: the unbounded value in the negative direction. Unlike isinf which catches both +∞ and -∞, this only returns true for -∞. Essential for:
- Sign-aware underflow detection: Differentiating positive from negative divergence
- Direction-sensitive algorithms: Different handling for overflow by sign
- Loss analysis: Detecting when loss or metrics explode negatively
- Decay processes: Identifying negative divergence in exponential decay
- Gradient tracking: Finding if gradients exploded in negative direction
- Debugging: Pinpointing negative-direction numerical failures
Negative infinity (-∞) results from operations exceeding the lower floating-point bound, like log(0), negative division by zero (-1/0), or exp(-1000). Pair with isposinf for complete infinity analysis.
- Negative sign: Only returns true for -∞, not +∞
- Never NaN: isneginf(NaN) = false
- Subset of isinf: isneginf(x) → isinf(x) always true when isneginf true
- Sign distinction: isposinf + isneginf covers all infinity cases
- Distinct from zero: isneginf(0) = false, even though -0 and +0 are equal
- Not positive infinity: Only detects -∞, use isposinf for +∞
- Float dtype only: Meaningful only for floating-point tensors
- Complete classification: isposinf + isneginf + isnan + isfinite exhaustive
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 -∞Examples
// Basic negative infinity detection
const x = torch.tensor([1.0, 1.0/0.0, -1.0/0.0, 0.0]);
torch.isneginf(x); // [false, false, true, false]
// Detecting negative underflow
const exponents = torch.tensor([1000, -500, -1000]);
const results = torch.exp(exponents);
const neg_underflow = torch.isneginf(results);
// neg_underflow: [false, false, false] - exp is never negative, produces 0
// Log of zero and negative numbers
const inputs = torch.tensor([1.0, 0.0, -1.0]);
const logs = torch.log(inputs);
const neg_inf = torch.isneginf(logs);
// neg_inf: [false, true, true] - log(0) and log(-1) produce -∞ or NaN
// Distinguishing overflow directions
const mixed = torch.tensor([1.0/0.0, -1.0/0.0, 2.0]);
const pos_inf = torch.isposinf(mixed); // [true, false, false]
const neg_inf = torch.isneginf(mixed); // [false, true, false]
// Complete infinity classification in both directions
// Loss explosion analysis (negative direction)
const losses = torch.tensor([0.5, 1.0, -1.0/0.0, 2.0]);
const negative_loss = torch.isneginf(losses);
// negative_loss: [false, false, true, false] - identifies bad loss value
// Divergence by sign in iterative algorithm
let x = torch.tensor([1.0, -1.0, 0.5]);
for (let i = 0; i < 100; i++) {
x = torch.mul(x, -2.0); // Oscillating growth
if (torch.any(torch.isposinf(x)).item()) {
console.log('Positive overflow');
}
if (torch.any(torch.isneginf(x)).item()) {
console.log('Negative overflow');
}
}
// Handling only negative infinities separately
const data = torch.tensor([1.0, 2.0/0.0, 3.0, -1.0/0.0]);
const clipped = torch.where(
torch.isneginf(data),
torch.full_like(data, -1e10), // Replace -∞ with large negative finite value
data
);
// clipped: [1.0, +∞, 3.0, -1e10]See Also
- PyTorch torch.isneginf()
- isposinf - Check for positive infinity only
- isinf - Check for any infinity (both signs)
- isnan - Check for NaN
- isfinite - Check for finite values