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

TensorFlow · Function

tf.constant

Creates a TensorFlow tensor from a tensor-like value, optionally choosing its dtype and shape. Python-side values are embedded as constants during graph construction; eager tensors remain immutable.

How it works

tf.constant converts a tensor-like value into a TensorFlow tensor. TensorFlow infers the element type unless dtype is supplied, can reshape or broadcast values to a requested shape, and embeds Python-side constant values in graph construction.

Syntax

tf.constant(value, dtype=None, shape=None, name='Const') -> Union[tf.Operation, ops._EagerTensorBase]

Parameters

NameTypeDescription
valuetensor-like objectA constant value or list of values of the output type.
dtypetf.DType, optionalThe element type of the resulting tensor; inferred when omitted.
shapeTensorShape or sequence of integers, optionalOptional dimensions for the resulting tensor.
namestring, optionalOptional name for the tensor or graph node.

Returns

A Constant Tensor.

Key points

  • Accepts tensor-like input such as Python scalars, lists, and array-like values, returning a TensorFlow tensor.
  • Omitting dtype lets TensorFlow infer the element type from value.
  • Use dtype to request the output element type and cast the supplied values accordingly.
  • A scalar value can populate every element of a multidimensional output when shape is provided.
  • New tensors are placed on the current device, while inputs that are already tensors retain their existing placements.
  • In graph construction, Python-side values are represented as Const nodes containing the embedded value.

Common mistakes

  • The returned eager tensor cannot be modified in place; use tf.Variable when mutable state is required.
  • Do not pass a symbolic tensor as value when graph embedding is required; use an operation that accepts runtime tensors instead.
  • A supplied shape must be compatible with the value, except that scalar values can fill the requested shape.

Example: Create a typed matrix constant

import tensorflow as tf

values = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
print(values)
print(values.shape)

Creates a 2-by-2 float32 tensor from nested Python lists and prints its value and shape.

When to use it

Create fixed tensors

Represent fixed numeric data such as lookup values, masks, coefficients, or small inputs in a TensorFlow computation.

Control dtype

Ensure operations receive a predictable element type, such as float32 instead of an inferred integer type.

Set or broadcast shape

Build a tensor with repeated scalar contents or a specified multidimensional arrangement.

Construct graph constants

Embed Python values into a traced or graph-based computation as a Const node.

Interview questions

QHow does dtype selection work?

tf.constant creates a tensor from a tensor-like value. If dtype is omitted, TensorFlow infers it from value; specifying dtype casts the resulting values to that type.

Follow-up: How would you create a mutable parameter? Use tf.Variable rather than tf.constant, because eager tensors are immutable.

QWhat does the shape argument do?

shape optionally determines the output dimensions. TensorFlow reshapes the value to that shape, and a scalar is expanded to populate all requested elements.

Follow-up: What should be checked when supplying a non-scalar value and shape? Its element arrangement and size must be compatible with the requested dimensions.

QHow does tf.constant behave with tensors and graphs?

Python constants are embedded in a graph Const node during graph construction. An eager Tensor passed as value is not changed by tf.constant and can transmit gradients.

Follow-up: Why can symbolic tensors cause failure? A symbolic tensor is not a Python-side constant available for embedding when the graph is built.

  • tf.convert_to_tensor
  • tf.Variable
  • tf.fill
  • tf.identity
  • tf.Tensor

Source: tf.constant. Checked 2026-10-15.