math
Elementwise math: division with a default for zero denominators.
Functions:
-
safe_divideβSafely divide two tensors, returning a default value if the denominator is zero.
safe_divide [source] ΒΆ
Safely divide two tensors, returning a default value if the denominator is zero.
Note
If the inputs are not floating point numbers, they will be converted to floating point numbers (float32).
Parameters:
-
a(Tensor) βThe numerator tensor.
-
b(Tensor) βThe denominator tensor.
-
default(Union[float, Tensor], default:float('nan')) βThe default value to return if the denominator is zero.
Returns:
-
TensorβThe result of the division.
Example
>>> safe_divide(torch.tensor([1.0, 2.0, 3.0]), torch.tensor([1.0, 0.0, 1.0]))
tensor([1., nan, 3.])
>>> safe_divide(torch.tensor([1.0, 2.0, 3.0]), torch.tensor([1.0, 0.0, 1.0]), default=0.0)
tensor([1., 0., 3.])
>>> safe_divide(torch.tensor([1, 2, 3]), torch.tensor([1, 0, 1]), default=torch.tensor([0, 0, 0]))
tensor([1., 0., 3.])