tensorflow
machine learning
deep learning
dynamic tensor
programming tutorial

tensorflow constant with variable size

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 constants are immutable tensors, so their values do not change after creation. A common question is whether a constant can have variable size. The short answer is that the created constant has a fixed value and shape, but you can build functions that produce tensors whose shape depends on runtime input.

What tf.constant Does and Does Not Do

tf.constant materializes a concrete tensor immediately.

python
1import tensorflow as tf
2
3c = tf.constant([1, 2, 3], dtype=tf.int32)
4print(c)
5print(c.shape)

The shape here is fixed as length three. You cannot resize c in place because constants are immutable.

If you need different sizes, create a new tensor for each size:

python
n = 5
c2 = tf.constant([0] * n)
print(c2.shape)

This works in eager mode because Python constructs a new list before TensorFlow creates the constant.

Build Runtime-Dependent Tensors with Tensor Ops

In graph workflows, Python list construction is not enough for dynamic dimensions. Instead, use TensorFlow ops that accept dynamic shape tensors.

python
1import tensorflow as tf
2
3@tf.function
4def make_vector(length):
5    length = tf.cast(length, tf.int32)
6    return tf.fill([length], 7)
7
8print(make_vector(tf.constant(3)))
9print(make_vector(tf.constant(8)))

tf.fill produces new tensors with runtime-defined size, while each produced tensor is still immutable after creation.

For two-dimensional shapes:

python
1@tf.function
2def make_matrix(rows, cols):
3    shape = tf.stack([rows, cols])
4    return tf.ones(shape, dtype=tf.float32)
5
6m = make_matrix(tf.constant(2), tf.constant(4))
7print(m.shape)

This pattern is often what people mean by a constant with variable size.

Use tf.Variable for Mutable State

If the requirement is to update stored values over time, use tf.Variable instead of constants.

python
1import tensorflow as tf
2
3v = tf.Variable([1.0, 2.0, 3.0])
4v.assign_add([0.5, 0.5, 0.5])
5print(v.numpy())

Variables are mutable and optimized for training state such as weights and moving averages.

If shape changes are required, declare flexible shape behavior carefully.

python
v = tf.Variable([1, 2], dtype=tf.int32, shape=tf.TensorShape([None]))
v.assign([1, 2, 3, 4])
print(v)

Even then, shape flexibility has constraints and should be used intentionally.

Handle Irregular Length Data with RaggedTensor

For batches containing sequences of different lengths, RaggedTensor is often cleaner than forcing padded constants.

python
1import tensorflow as tf
2
3rt = tf.ragged.constant([[1, 2, 3], [4], [5, 6]])
4print(rt)
5print(rt.bounding_shape())

Ragged tensors represent variable-length rows directly and are useful in NLP and recommendation pipelines.

Shape Signatures in tf.function

When tracing functions, shape signatures help TensorFlow compile reusable graphs.

python
1import tensorflow as tf
2
3@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
4def normalize(x):
5    return (x - tf.reduce_mean(x)) / (tf.math.reduce_std(x) + 1e-6)
6
7print(normalize(tf.constant([1.0, 2.0, 3.0])))
8print(normalize(tf.constant([2.0, 4.0, 6.0, 8.0])))

The None dimension allows varying input length while preserving a consistent function contract.

Dynamic Batching with Padding

For sequence tasks, per-example lengths vary while batch tensors need rectangular shapes. Use padded batching to keep runtime flexibility.

python
1import tensorflow as tf
2
3dataset = tf.data.Dataset.from_generator(
4    lambda: ([1.0, 2.0], [3.0], [4.0, 5.0, 6.0]),
5    output_signature=tf.TensorSpec(shape=(None,), dtype=tf.float32),
6)
7
8batched = dataset.padded_batch(2, padded_shapes=[None], padding_values=0.0)
9for batch in batched:
10    print(batch)

This keeps source examples variable in length while producing tensors suitable for model input.

Common Pitfalls

A common misunderstanding is expecting tf.constant itself to resize after creation. It never mutates, so shape changes always mean creating a new tensor.

Another issue is mixing Python-side dynamic list creation with tf.function tracing and expecting graph-level flexibility automatically. Use TensorFlow shape-aware ops for robust graph behavior.

Developers also overuse tf.Variable when immutable tensors are enough. Mutable state adds complexity and should be limited to parameters or counters that truly need updates.

Summary

  • tf.constant creates immutable tensors with fixed value and shape.
  • Variable-size results come from creating new tensors, often via dynamic TensorFlow ops.
  • Use tf.fill, tf.ones, or related ops for runtime-dependent shapes in graphs.
  • Use tf.Variable only when data must be updated over time.
  • Consider RaggedTensor and TensorSpec for variable-length workflows.

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.