torch.Tensor.Tensor.signbit
Extract sign bit.
Returns whether each element is negative (true/1) or non-negative (false/0). Equivalent to checking if the sign bit is set in IEEE 754 floating point. Note: sign of -0.0 is True in some implementations.
Use Cases:
- Check if values are negative
- Binary sign classification
- IEEE 754 floating point bit manipulation
Returns
Tensor<S, D, Dev>– Boolean tensor (or equivalent uint8) indicating if value is negativeExamples
// Check for negative values
const x = torch.tensor([-2, -0.5, 0, 0.5, 2]);
const is_neg = x.signbit(); // [true, true, false, false, false]
// Filter negative values
const values = torch.randn(100);
const negatives = values.masked_select(values.signbit());See Also
- PyTorch tensor.signbit()
- sign - Full sign information (-1, 0, 1)