MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / cast

Function cast

tensorflow/python/ops/math_ops.py:648–707  ·  view source on GitHub ↗

Casts a tensor to a new type. The operation casts `x` (in case of `Tensor`) or `x.values` (in case of `SparseTensor` or `IndexedSlices`) to `dtype`. For example: ```python x = tf.constant([1.8, 2.2], dtype=tf.float32) tf.dtypes.cast(x, tf.int32) # [1, 2], dtype=tf.int32 ``` The

(x, dtype, name=None)

Source from the content-addressed store, hash-verified

646@tf_export("cast", "dtypes.cast")
647@dispatch.add_dispatch_support
648def cast(x, dtype, name=None):
649 """Casts a tensor to a new type.
650
651 The operation casts `x` (in case of `Tensor`) or `x.values`
652 (in case of `SparseTensor` or `IndexedSlices`) to `dtype`.
653
654 For example:
655
656 ```python
657 x = tf.constant([1.8, 2.2], dtype=tf.float32)
658 tf.dtypes.cast(x, tf.int32) # [1, 2], dtype=tf.int32
659 ```
660
661 The operation supports data types (for `x` and `dtype`) of
662 `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,
663 `float16`, `float32`, `float64`, `complex64`, `complex128`, `bfloat16`.
664 In case of casting from complex types (`complex64`, `complex128`) to real
665 types, only the real part of `x` is returned. In case of casting from real
666 types to complex types (`complex64`, `complex128`), the imaginary part of the
667 returned value is set to `0`. The handling of complex types here matches the
668 behavior of numpy.
669
670 Args:
671 x: A `Tensor` or `SparseTensor` or `IndexedSlices` of numeric type. It could
672 be `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`,
673 `int64`, `float16`, `float32`, `float64`, `complex64`, `complex128`,
674 `bfloat16`.
675 dtype: The destination type. The list of supported dtypes is the same as
676 `x`.
677 name: A name for the operation (optional).
678
679 Returns:
680 A `Tensor` or `SparseTensor` or `IndexedSlices` with same shape as `x` and
681 same type as `dtype`.
682
683 Raises:
684 TypeError: If `x` cannot be cast to the `dtype`.
685 """
686 base_type = dtypes.as_dtype(dtype).base_dtype
687 if isinstance(x,
688 (ops.Tensor, _resource_variable_type)) and base_type == x.dtype:
689 return x
690 with ops.name_scope(name, "Cast", [x]) as name:
691 if isinstance(x, sparse_tensor.SparseTensor):
692 values_cast = cast(x.values, base_type, name=name)
693 x = sparse_tensor.SparseTensor(x.indices, values_cast, x.dense_shape)
694 elif isinstance(x, ops.IndexedSlices):
695 values_cast = cast(x.values, base_type, name=name)
696 x = ops.IndexedSlices(values_cast, x.indices, x.dense_shape)
697 else:
698 # TODO(josh11b): If x is not already a Tensor, we could return
699 # ops.convert_to_tensor(x, dtype=dtype, ...) here, but that
700 # allows some conversions that cast() can't do, e.g. casting numbers to
701 # strings.
702 x = ops.convert_to_tensor(x, name="x")
703 if x.dtype.base_dtype != base_type:
704 x = gen_math_ops.cast(x, base_type, name=name)
705 if x.dtype.is_complex and base_type.is_floating:

Callers 15

_block_orthMethod · 0.70
saturate_castFunction · 0.70
to_floatFunction · 0.70
to_doubleFunction · 0.70
to_int32Function · 0.70
to_int64Function · 0.70
to_bfloat16Function · 0.70
to_complex64Function · 0.70
to_complex128Function · 0.70
_sparse_dense_truedivFunction · 0.70
_truediv_python3Function · 0.70
rangeFunction · 0.70

Calls 2

name_scopeMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected