@tf_export('dtypes.cast', 'cast')
@dispatch.add_dispatch_support
def cast(x, dtype, name=None):
    'Casts a tensor to a new type.\n\n  The operation casts `x` (in case of `Tensor`) or `x.values`\n  (in case of `SparseTensor` or `IndexedSlices`) to `dtype`.\n\n  For example:\n\n  ```python\n  x = tf.constant([1.8, 2.2], dtype=tf.float32)\n  tf.cast(x, tf.int32)  # [1, 2], dtype=tf.int32\n  ```\n\n  The operation supports data types (for `x` and `dtype`) of\n  `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,\n  `float16`, `float32`, `float64`, `complex64`, `complex128`, `bfloat16`.\n  In case of casting from complex types (`complex64`, `complex128`) to real\n  types, only the real part of `x` is returned. In case of casting from real\n  types to complex types (`complex64`, `complex128`), the imaginary part of the\n  returned value is set to `0`. The handling of complex types here matches the\n  behavior of numpy.\n\n  Args:\n    x: A `Tensor` or `SparseTensor` or `IndexedSlices` of numeric type. It could\n      be `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`,\n      `int64`, `float16`, `float32`, `float64`, `complex64`, `complex128`,\n      `bfloat16`.\n    dtype: The destination type. The list of supported dtypes is the same as\n      `x`.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` or `SparseTensor` or `IndexedSlices` with same shape as `x` and\n      same type as `dtype`.\n\n  Raises:\n    TypeError: If `x` cannot be cast to the `dtype`.\n  '
    base_type = dtypes.as_dtype(dtype).base_dtype
    if (isinstance(x, (ops.Tensor, _resource_variable_type)) and (base_type == x.dtype)):
        return x
    with ops.name_scope(name, 'Cast', [x]) as name:
        if isinstance(x, sparse_tensor.SparseTensor):
            values_cast = cast(x.values, base_type, name=name)
            x = sparse_tensor.SparseTensor(x.indices, values_cast, x.dense_shape)
        elif isinstance(x, ops.IndexedSlices):
            values_cast = cast(x.values, base_type, name=name)
            x = ops.IndexedSlices(values_cast, x.indices, x.dense_shape)
        else:
            x = ops.convert_to_tensor(x, name='x')
            if (x.dtype.base_dtype != base_type):
                x = gen_math_ops.cast(x, base_type, name=name)
        if (x.dtype.is_complex and base_type.is_floating):
            logging.warn('Casting complex to real discards imaginary part.')
        return x
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.cast(x, tf.int32)  # [1, 2], dtype=tf.int32
```

The operation supports data types (for `x` and `dtype`) of
`uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,
`float16`, `float32`, `float64`, `complex64`, `complex128`, `bfloat16`.
In case of casting from complex types (`complex64`, `complex128`) to real
types, only the real part of `x` is returned. In case of casting from real
types to complex types (`complex64`, `complex128`), the imaginary part of the
returned value is set to `0`. The handling of complex types here matches the
behavior of numpy.

Args:
  x: A `Tensor` or `SparseTensor` or `IndexedSlices` of numeric type. It could
    be `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`,
    `int64`, `float16`, `float32`, `float64`, `complex64`, `complex128`,
    `bfloat16`.
  dtype: The destination type. The list of supported dtypes is the same as
    `x`.
  name: A name for the operation (optional).

Returns:
  A `Tensor` or `SparseTensor` or `IndexedSlices` with same shape as `x` and
    same type as `dtype`.

Raises:
  TypeError: If `x` cannot be cast to the `dtype`.
@tf_export('dtypes.cast', 'cast')
@dispatch.add_dispatch_support
def cast(x, dtype, name=None):
    'Casts a tensor to a new type.\n\n  The operation casts `x` (in case of `Tensor`) or `x.values`\n  (in case of `SparseTensor` or `IndexedSlices`) to `dtype`.\n\n  For example:\n\n  ```python\n  x = tf.constant([1.8, 2.2], dtype=tf.float32)\n  tf.cast(x, tf.int32)  # [1, 2], dtype=tf.int32\n  ```\n\n  The operation supports data types (for `x` and `dtype`) of\n  `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,\n  `float16`, `float32`, `float64`, `complex64`, `complex128`, `bfloat16`.\n  In case of casting from complex types (`complex64`, `complex128`) to real\n  types, only the real part of `x` is returned. In case of casting from real\n  types to complex types (`complex64`, `complex128`), the imaginary part of the\n  returned value is set to `0`. The handling of complex types here matches the\n  behavior of numpy.\n\n  Args:\n    x: A `Tensor` or `SparseTensor` or `IndexedSlices` of numeric type. It could\n      be `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`,\n      `int64`, `float16`, `float32`, `float64`, `complex64`, `complex128`,\n      `bfloat16`.\n    dtype: The destination type. The list of supported dtypes is the same as\n      `x`.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` or `SparseTensor` or `IndexedSlices` with same shape as `x` and\n      same type as `dtype`.\n\n  Raises:\n    TypeError: If `x` cannot be cast to the `dtype`.\n  '
    base_type = dtypes.as_dtype(dtype).base_dtype
    if (isinstance(x, (ops.Tensor, _resource_variable_type)) and (base_type == x.dtype)):
        return x
    with ops.name_scope(name, 'Cast', [x]) as name:
        if isinstance(x, sparse_tensor.SparseTensor):
            values_cast = cast(x.values, base_type, name=name)
            x = sparse_tensor.SparseTensor(x.indices, values_cast, x.dense_shape)
        elif isinstance(x, ops.IndexedSlices):
            values_cast = cast(x.values, base_type, name=name)
            x = ops.IndexedSlices(values_cast, x.indices, x.dense_shape)
        else:
            x = ops.convert_to_tensor(x, name='x')
            if (x.dtype.base_dtype != base_type):
                x = gen_math_ops.cast(x, base_type, name=name)
        if (x.dtype.is_complex and base_type.is_floating):
            logging.warn('Casting complex to real discards imaginary part.')
        return x