TensorFlow
tf.int64
tf.float32
type conversion
machine learning

How to convert tf.int64 to tf.float32?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow operations are strict about data types, so mixing integers and floating-point tensors often causes runtime errors. Converting tf.int64 to tf.float32 is straightforward, but it helps to understand when to cast, why models usually expect floats, and where silent precision issues can appear.

Use tf.cast

The standard TensorFlow tool for dtype conversion is tf.cast. It returns a new tensor with the same values represented in a different dtype.

python
1import tensorflow as tf
2
3values = tf.constant([1, 2, 3], dtype=tf.int64)
4converted = tf.cast(values, tf.float32)
5
6print(values.dtype)
7print(converted.dtype)
8print(converted.numpy())

This is the correct solution for almost every basic conversion case. You do not need NumPy, and you do not need to rebuild the tensor manually.

Why Models Usually Want float32

Neural network layers, optimizers, and loss functions are usually tuned for floating-point math. Image pixels, normalized features, embeddings, and continuous targets are commonly represented as tf.float32 because:

  • GPU kernels are heavily optimized for float operations
  • gradients are defined over floating-point values
  • many TensorFlow ops reject integer inputs when division or normalization is involved

For example:

python
1import tensorflow as tf
2
3pixels = tf.constant([0, 128, 255], dtype=tf.int64)
4normalized = tf.cast(pixels, tf.float32) / 255.0
5
6print(normalized.numpy())

If you divide integer tensors without casting, you risk dtype mismatches or an input pipeline that does not match the rest of your model.

Cast Inside A tf.data Pipeline

Casting is especially common in input pipelines:

python
1import tensorflow as tf
2
3dataset = tf.data.Dataset.from_tensor_slices({
4    "feature": [1, 2, 3, 4],
5    "label": [0, 1, 0, 1],
6})
7
8def preprocess(example):
9    feature = tf.cast(example["feature"], tf.float32)
10    label = tf.cast(example["label"], tf.float32)
11    return feature, label
12
13dataset = dataset.map(preprocess).batch(2)
14
15for batch_features, batch_labels in dataset:
16    print(batch_features.dtype, batch_labels.dtype)

Doing the cast in the pipeline keeps the model code cleaner and ensures every batch arrives with the dtype you expect.

Know When Not To Cast

Not every integer tensor should become float32. Some values are meant to remain integer-based:

  • class IDs used with sparse losses
  • token IDs for embedding layers
  • indices passed to gather operations
  • counts or sizes used in control flow

For example, SparseCategoricalCrossentropy expects integer class labels:

python
1import tensorflow as tf
2
3labels = tf.constant([0, 2, 1], dtype=tf.int64)
4loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

So the real rule is not "always cast to float," but "cast when the next operation expects float."

Precision And Range Considerations

tf.float32 can exactly represent many integers, but not all very large int64 values. If your integers are large identifiers, timestamps, or counters, converting them to float may lose precision.

python
1import tensorflow as tf
2
3big_value = tf.constant([9_007_199_254_740_993], dtype=tf.int64)
4as_float = tf.cast(big_value, tf.float32)
5
6print(big_value.numpy())
7print(as_float.numpy())

For model features such as counts or pixel values, this is usually not a problem. For identifiers, it can be a serious bug.

Common Pitfalls

The biggest mistake is casting labels to float32 when the downstream loss expects integer class IDs. That produces harder-to-debug training errors than a simple cast mistake.

Another common issue is converting very large int64 values and assuming the floating-point representation is exact. It often is not.

Developers also often cast too late. If earlier preprocessing steps expect floats, delaying the cast keeps the real bug in the pipeline.

Finally, do not assume Python integers and TensorFlow tensors behave the same way. Check tensor.dtype explicitly when debugging.

Summary

  • Use tf.cast(tensor, tf.float32) to convert tf.int64 tensors when float math is required.
  • Cast early in the input pipeline when later preprocessing expects floats.
  • Keep integer tensors as integers when they represent labels, token IDs, or indices.
  • Be careful with very large integers because float32 may not preserve them exactly.
  • Always cast based on what the next TensorFlow operation expects, not by habit alone.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.