torch.isposinf
function isposinf<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 positive infinity (+∞).
Tests specifically for positive infinity: the unbounded value in the positive direction. Unlike isinf which catches both +∞ and -∞, this only returns true for +∞. Essential for:
- Sign-aware overflow detection: Differentiating positive from negative divergence
- Direction sensitivity: Checking for overflow in specific direction
- Algorithm flow: Different handling for overflow in different signs
- Physical meaning: Distinguishing between growth and decay infinities
- Gradient analysis: Checking if gradients exploded in positive direction
- Debugging: Finding where positive values diverged
Positive infinity (+∞) results from operations exceeding the upper floating-point bound, like exp(1000) or positive division by zero (1/0). Pair with isneginf for complete infinity analysis.
- Positive sign: Only returns true for +∞, not -∞
- Never NaN: isposinf(NaN) = false
- Subset of isinf: isposinf(x) → isinf(x) always true when isposinf true
- Sign distinction: isposinf + isneginf covers all infinity cases
- Never zero: isposinf(0) = false, even for -0
- Not negative infinity: Only detects +∞, use isneginf for -∞
- Float dtype only: Meaningful only for floating-point tensors
- Combined with isinf: isposinf + isneginf + isnan + isfinite = complete classification
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 positive infinity detection
const x = torch.tensor([1.0, 1.0/0.0, -1.0/0.0, 0.0]);
torch.isposinf(x); // [false, true, false, false]
// Detecting positive overflow in exponentials
const exponents = torch.tensor([-100, 500, 1000]);
const results = torch.exp(exponents);
const pos_overflow = torch.isposinf(results);
// pos_overflow: [false, true, true] - exp always positive
// 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]
// pos_inf + neg_inf: identify which direction diverged
// Handling divergence by sign
const values = torch.tensor([1.5, 1.0/0.0, -2.0, -1.0/0.0]);
const pos_diverged = torch.isposinf(values);
const neg_diverged = torch.isneginf(values);
// Can apply different corrections for positive vs negative overflow
// Gradient explosion detection
const gradients = torch.tensor([-0.5, 1.0/0.0, 0.2, -1.0/0.0]);
const exploded_pos = torch.isposinf(gradients);
// exploded_pos: [false, true, false, false]
// Clipping only positive infinities
const data = torch.tensor([1.0, 2.0/0.0, 3.0, -1.0/0.0]);
const clipped = torch.where(
torch.isposinf(data),
torch.full_like(data, 1e10), // Replace +∞ with large finite value
data
);
// clipped: [1.0, 1e10, 3.0, -∞]See Also
- PyTorch torch.isposinf()
- isneginf - Check for negative infinity only
- isinf - Check for any infinity (both signs)
- isnan - Check for NaN
- isfinite - Check for finite values