A constant tensor's value is fixed at creation time and cannot be reassigned or updated afterward — any operation that appears to modify it actually produces a brand-new tensor instead, leaving the original constant untouched. This immutability makes tf.constant() the right choice for fixed input data, hyperparameters, or any value that should never change during a model's execution, in contrast to tf.Variable(), which is specifically designed to hold values that do need to change, like a model's trainable weights.
1Understanding tf.constant()
A constant tensor's value is fixed at creation time and cannot be reassigned or updated afterward — any operation that appears to modify it actually produces a brand-new tensor instead, leaving the original constant untouched. This immutability makes tf.constant() the right choice for fixed input data, hyperparameters, or any value that should never change during a model's execution, in contrast to tf.Variable(), which is specifically designed to hold values that do need to change, like a model's trainable weights.
Use tf.constant() for genuinely fixed values, like fixed input data or hyperparameters, and tf.Variable() specifically for values that need to be updated during training, like model weights — mixing the two up, like trying to use a constant for trainable weights, will fail, since gradients can only be applied to variables.
import tensorflow as tf
x = tf.constant([1, 2, 3])
print(x)2Practical Example
Here is a real-world application of tf.constant() showing how it is used in production TensorFlow code.
import tensorflow as tf
matrix = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
print(matrix)3Best Practices
Follow these guidelines when working with tf.constant():
1. Use tf.constant() for input data, fixed configuration values, or anything that shouldn't change during execution
2. Use tf.Variable() instead whenever a value needs to be updated over time, like model weights, since constants cannot be reassigned
3. Specify dtype explicitly when the automatically-inferred type from your input data doesn't match what your model or subsequent operations actually expect
Tip: Use tf.constant() for genuinely fixed values, like fixed input data or hyperparameters, and tf.Variable() specifically for values that need to be updated during training, like model weights — mixing the two up, like trying to use a constant for trainable weights, will fail, since gradients can only be applied to variables.
import tensorflow as tf
x = tf.constant([1, 2, 3])
print(x)