Performs a safe saturating cast of `value` to `dtype`. This function casts the input to `dtype` without applying any scaling. If there is a danger that values would over or underflow in the cast, this op applies the appropriate clamping before the cast. Args: value: A `Tensor`. dt
(value, dtype, name=None)
| 710 | @tf_export("dtypes.saturate_cast", "saturate_cast") |
| 711 | @dispatch.add_dispatch_support |
| 712 | def saturate_cast(value, dtype, name=None): |
| 713 | """Performs a safe saturating cast of `value` to `dtype`. |
| 714 | |
| 715 | This function casts the input to `dtype` without applying any scaling. If |
| 716 | there is a danger that values would over or underflow in the cast, this op |
| 717 | applies the appropriate clamping before the cast. |
| 718 | |
| 719 | Args: |
| 720 | value: A `Tensor`. |
| 721 | dtype: The desired output `DType`. |
| 722 | name: A name for the operation (optional). |
| 723 | |
| 724 | Returns: |
| 725 | `value` safely cast to `dtype`. |
| 726 | """ |
| 727 | # When casting to a type with smaller representable range, clamp. |
| 728 | # Note that this covers casting to unsigned types as well. |
| 729 | with ops.name_scope(name, "saturate_cast", [value]) as name: |
| 730 | value = ops.convert_to_tensor(value, name="value") |
| 731 | dtype = dtypes.as_dtype(dtype).base_dtype |
| 732 | if value.dtype.min < dtype.min: |
| 733 | value = gen_math_ops.maximum( |
| 734 | value, |
| 735 | ops.convert_to_tensor(dtype.min, dtype=value.dtype, name="min")) |
| 736 | if value.dtype.max > dtype.max: |
| 737 | value = gen_math_ops.minimum( |
| 738 | value, |
| 739 | ops.convert_to_tensor(dtype.max, dtype=value.dtype, name="max")) |
| 740 | return cast(value, dtype, name=name) |
| 741 | |
| 742 | |
| 743 | @deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.") |
nothing calls this directly
no test coverage detected