Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.cast

Casts a tensor to a new numeric dtype. It supports dense, sparse, and indexed-slice inputs while preserving their shape and structure.

How it works

The operation converts a Tensor, or the values inside a SparseTensor or IndexedSlices, to the requested destination dtype. The result preserves the input structure and shape while using the destination type; conversions can lose precision.

Syntax

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

Parameters

NameTypeDescription
xTensor | SparseTensor | IndexedSlicesA Tensor, SparseTensor, or IndexedSlices of a supported numeric type.
dtypeDTypeThe destination type; it must be one of the supported types for x.
namestringOptional name for the operation.

Returns

A Tensor, SparseTensor, or IndexedSlices with the same shape as x and the same type as dtype.

Key points

  • Use tf.cast to change a tensor's numeric dtype.
  • Dense tensors are cast directly; sparse and indexed inputs have their values cast.
  • The result retains the input's supported structure and shape.
  • dtype specifies the type of the returned values.
  • The optional name argument gives the operation an explicit graph name.
  • Supported destinations include floating-point and complex dtypes, in addition to integer and bfloat16 types.

Common mistakes

  • Casting may lose precision, especially when converting between numeric representations.
  • Both the input and destination must use one of the numeric dtypes listed on the documentation page.
  • A TypeError is raised when the input cannot be converted to the requested dtype.

Example: Convert a tensor to int32

import tensorflow as tf

x = tf.constant([1.8, 2.2], dtype=tf.float32)
y = tf.cast(x, tf.int32)
print(y)  # [1 2]

Converts the floating-point tensor to int32, truncating the fractional parts.

When to use it

Convert features to the dtype expected by a layer or computation.

Model preprocessing

Convert numeric labels to an integer dtype for indexing or classification workflows.

Label preparation

Align operands to a compatible numeric type before combining them.

Mixed-dtype arithmetic

Interview questions

QWhat does tf.cast do for dense and sparse-like inputs?

tf.cast converts a tensor to a specified destination dtype. For a Tensor it casts the tensor directly; for a SparseTensor or IndexedSlices, it casts their values while preserving the structure and shape.

Follow-up: How would you avoid overflow during conversion? Use tf.dtypes.saturate_cast, which clamps values before casting when overflow or underflow is possible.

QWhat are the arguments to tf.cast?

The signature is tf.cast(x, dtype, name=None). x is the numeric input, dtype is the destination type, and name optionally names the operation.

Follow-up: Which types can be used? The documentation lists unsigned and signed integer types, floating-point types, complex types, and bfloat16.

QWhat properties does the result preserve?

The output has the same shape as x and the same structural kind—Tensor, SparseTensor, or IndexedSlices—but uses dtype. Converting numeric values may discard precision.

Follow-up: What happens with an unsupported conversion? TensorFlow raises TypeError when x cannot be cast to the requested dtype.

  • tf.dtypes.saturate_cast
  • tf.bitcast
  • tf.convert_to_tensor

Source: tf.cast | TensorFlow v2.16.1. Checked 2026-10-15.