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

TensorFlow · Function

tf.linspace

Generates a tensor of evenly spaced values between start and stop along a chosen axis. For more than one value, the sequence ends exactly at stop.

How it works

The function creates a sequence containing num evenly spaced values beginning at start and ending at stop when num is greater than one. For multidimensional inputs, axis determines the output dimension whose size becomes num; with num less than or equal to zero, it raises ValueError.

Syntax

tf.linspace(start, stop, num, name=None, axis=0)

Parameters

NameTypeDescription
startTensorA floating-point N-D tensor providing the first value in the range. Supported types are bfloat16, float32, and float64.
stopTensorA tensor with the same type and shape as start, providing the last value in the range.
numTensorA scalar integer tensor specifying the number of values to generate. Supported types are int32 and int64.
namestringOptional operation name.
axisintAxis along which values are generated for N-D inputs; defaults to 0.

Returns

A tensor containing num evenly spaced values generated from start to stop along axis.

Key points

  • Produces evenly spaced values over an interval, with the sequence arranged along a chosen axis.
  • The result contains exactly num values and begins at start.
  • For num greater than one, the final generated value is exactly stop.
  • Multidimensional start and stop tensors are supported, provided their shapes match.
  • The output dimension selected by axis has length num.
  • start supports bfloat16, float32, and float64 tensor element types.
  • stop must match start in both tensor type and shape.

Common mistakes

  • A non-positive num does not produce an empty sequence; it raises ValueError.
  • Its behavior generally matches NumPy linspace, but the zero-count case differs.

Example: Generate an inclusive numeric sequence

import tensorflow as tf

values = tf.linspace(10.0, 12.0, 3)
print(values.numpy())  # [10. 11. 12.]

Creates three evenly spaced values from 10.0 through 12.0, including both endpoints.

When to use it

Numerical grids

Create coordinate values for plotting or evaluating a function over a bounded interval.

Batched ranges

Generate matching sequences across vector or matrix endpoints while preserving batch dimensions.

Interpolation samples

Construct evenly spaced inputs for interpolation or sampling procedures.

Interview questions

QHow does tf.linspace determine the spacing and endpoint?

tf.linspace generates num evenly spaced values from start toward stop. When num is greater than one, the increment is (stop - start) / (num - 1), making stop the final value.

Follow-up: What happens when num is one? The sequence contains one generated value; the documented increment rule applies only when num is greater than one.

QWhat are the required input constraints?

start and stop must be tensors with supported floating-point types and matching shape. num is a scalar integer tensor specifying how many values to generate.

Follow-up: What shape does num have? It must be a 0-D tensor, unlike start and stop, which may be N-D.

QHow does axis affect multidimensional inputs?

axis controls where the generated sequence is inserted when start and stop are N-D tensors. The corresponding output dimension has size num.

Follow-up: What is the default axis? axis defaults to 0, so the generated dimension is placed first unless another axis is supplied.

  • tf.keras.ops.linspace
  • tf.experimental.numpy.linspace
  • tf.Tensor

Source: tf.linspace | TensorFlow v2.16.1. Checked 2026-10-15.