Casting

TensorFlow provides several operations that you can use to cast tensor data types in your graph.

tf.string_to_number(string_tensor, out_type=None, name=None)

Converts each string in the input Tensor to the specified numeric type.

(Note that int32 overflow results in an error while float overflow results in a rounded value.)

Args:
  • string_tensor: A Tensor of type string.
  • out_type: An optional tf.DType from: tf.float32, tf.int32. Defaults to tf.float32. The numeric type to interpret each string in string_tensor as.
  • name: A name for the operation (optional).
Returns:

A Tensor of type out_type. A Tensor of the same shape as the input string_tensor.


tf.to_double(x, name='ToDouble')

Casts a tensor to type float64.

Args:
  • x: A Tensor or SparseTensor.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x with type float64.

Raises:
  • TypeError: If x cannot be cast to the float64.

tf.to_float(x, name='ToFloat')

Casts a tensor to type float32.

Args:
  • x: A Tensor or SparseTensor.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x with type float32.

Raises:
  • TypeError: If x cannot be cast to the float32.

tf.to_bfloat16(x, name='ToBFloat16')

Casts a tensor to type bfloat16.

Args:
  • x: A Tensor or SparseTensor.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x with type bfloat16.

Raises:
  • TypeError: If x cannot be cast to the bfloat16.

tf.to_int32(x, name='ToInt32')

Casts a tensor to type int32.

Args:
  • x: A Tensor or SparseTensor.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x with type int32.

Raises:
  • TypeError: If x cannot be cast to the int32.

tf.to_int64(x, name='ToInt64')

Casts a tensor to type int64.

Args:
  • x: A Tensor or SparseTensor.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x with type int64.

Raises:
  • TypeError: If x cannot be cast to the int64.

tf.cast(x, dtype, name=None)

Casts a tensor to a new type.

The operation casts x (in case of Tensor) or x.values (in case of SparseTensor) to dtype.

For example:

# tensor `a` is [1.8, 2.2], dtype=tf.float
tf.cast(a, tf.int32) ==> [1, 2]  # dtype=tf.int32
Args:
  • x: A Tensor or SparseTensor.
  • dtype: The destination type.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor with same shape as x.

Raises:
  • TypeError: If x cannot be cast to the dtype.

tf.bitcast(input, type, name=None)

Bitcasts a tensor from one type to another without copying data.

Given a tensor input, this operation returns a tensor that has the same buffer data as input with datatype type.

If the input datatype T is larger than the output datatype type then the shape changes from [...] to [..., sizeof(T)/sizeof(type)].

If T is smaller than type, the operator requires that the rightmost dimension be equal to sizeof(type)/sizeof(T). The shape then goes from [..., sizeof(type)/sizeof(T)] to [...].

NOTE: Bitcast is implemented as a low-level cast, so machines with different endian orderings will give different results.

Args:
  • input: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
  • type: A tf.DType from: tf.float32, tf.float64, tf.int64, tf.int32, tf.uint8, tf.uint16, tf.int16, tf.int8, tf.complex64, tf.complex128, tf.qint8, tf.quint8, tf.qint32, tf.half.
  • name: A name for the operation (optional).
Returns:

A Tensor of type type.


tf.saturate_cast(value, dtype, name=None)

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.
  • dtype: The desired output DType.
  • name: A name for the operation (optional).
Returns:

value safely cast to dtype.