torch.is_floating_point
function is_floating_point(input: TensorClass): booleanChecks whether a tensor has a floating-point data type.
Returns true if the tensor's dtype is a floating-point number type (float32 or float16). Useful for type validation and conditional logic based on tensor precision. Useful for:
- Type validation: Checking if tensor supports floating-point operations
- Precision handling: Determining if tensor has decimal precision
- Computation validation: Ensuring proper dtype before operations
- Mixed dtype handling: Conditional casting or precision selection
- Algorithm selection: Choosing algorithms based on available precision
Supported floating-point types: float32, float16. Non-floating types: int32, uint32, int8, uint8, bool.
- Floating types: Includes float32 and float16
- Exact check: Checks dtype, not content (integer values in float32 returns true)
- Type guard: Can be used to ensure tensor has decimal precision
- Inverse: Use is_integer_dtype() for integer tensors (if available)
- Type vs content: Checks dtype, not actual values (can have integer values in float)
- Boolean excluded: bool dtype returns false (not floating-point)
- No promotion: Just checks, doesn't convert dtypes
Parameters
inputTensorClass- Tensor to check for floating-point dtype
Returns
boolean– True if tensor dtype is float32 or float16, false otherwiseExamples
// Basic floating-point checks
const float_tensor = torch.tensor([1.5, 2.3, 3.7]);
torch.is_floating_point(float_tensor); // true (default dtype is float32)
const float16 = torch.tensor([1, 2, 3], { dtype: 'float16' });
torch.is_floating_point(float16); // true
const int_tensor = torch.tensor([1, 2, 3], { dtype: 'int32' });
torch.is_floating_point(int_tensor); // false// Conditional precision handling
function ensure_float(x: torch.Tensor) {
if (torch.is_floating_point(x)) {
return x;
} else {
// Convert integer tensor to float for operations
return x.to({ dtype: 'float32' });
}
}// Filter floating-point tensors from mixed collection
const tensors = [
torch.randn([2, 3]), // float32
torch.arange(5, { dtype: 'int32' }), // int32
torch.ones([4], { dtype: 'float16' }) // float16
];
const float_tensors = tensors.filter(torch.is_floating_point);
// Returns: [float32, float16] (skips int32)See Also
- PyTorch torch.is_floating_point()
- is_floating_point_dtype - Check dtype without tensor
- Tensor.to - Convert tensor to different dtype