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

TensorFlow · Class

tf.Tensor

tf.Tensor is TensorFlow’s immutable, multidimensional array type. Its elements share one dtype, and the tensor carries a shape for use in eager or graph-based computation.

How it works

A tf.Tensor is TensorFlow's immutable, typed multidimensional data object. It carries a shape and element type, and operations can run immediately in eager execution or as part of a graph for later evaluation.

Syntax

class tf.Tensor

Key points

  • The class models data arranged across zero or more dimensions.
  • TensorFlow computations commonly consume and produce tensors.
  • Each tensor has one dtype shared by all its elements.
  • New TensorFlow programs normally evaluate operations immediately.
  • Graph mode builds a deferred representation before computation runs.

Common mistakes

  • A tensor cannot mix element types; every value uses one known dtype.

Example: Matrix multiplication with tf.Tensor

import tensorflow as tf

c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
print(e)

Creates two rank-2 tensors and multiplies them with a TensorFlow operation.

When to use it

Model data flow

Represent inputs, intermediate values, and outputs in TensorFlow computations.

Numerical computation

Store typed multidimensional values for arithmetic and linear algebra operations.

Graph execution

Build deferred computations that can be evaluated later.

Interview questions

QWhat is tf.Tensor?

A tf.Tensor is a multidimensional array whose elements share one known data type. It also has a shape, and it is the primary object passed between TensorFlow operations.

Follow-up: How does execution differ between modes? In eager execution operations run immediately; in graph execution TensorFlow builds a computation graph for later evaluation.

QHow are TensorFlow tensors evaluated?

TensorFlow supports eager and graph execution. Eager execution is the default, while graph execution constructs operations for later evaluation.

Follow-up: Why might graph execution be useful? A graph separates construction from evaluation, allowing the computation to be represented and run later.

QHow does tf.Tensor differ from mutable state?

Regular tensors are immutable, so an operation returns a tensor rather than changing an existing tensor's contents. Use tf.Variable for mutable state such as model weights.

Follow-up: What property must tensor elements share? All elements must have one common known data type, such as float32, int32, or string.

  • tf.constant
  • tf.Variable
  • tf.matmul
  • tf.convert_to_tensor

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