Create index sequences
Generate integer indices for tensor slicing, lookup tables, or iteration-like graph logic.
TensorFlow · Function
Creates a one-dimensional Tensor containing a numeric sequence from start toward, but excluding, limit. The step is delta, and TensorFlow infers the result dtype unless dtype is specified.
tf.range builds a one-dimensional Tensor whose values begin at start and advance by delta until reaching, but excluding, limit. With the two-argument form, start is interpreted as the exclusive limit and the sequence begins at zero; TensorFlow infers the element type unless dtype is supplied.
tf.range(limit, delta=1, dtype=None, name='range')
tf.range(start, limit, delta=1, dtype=None, name='range')| Name | Type | Description |
|---|---|---|
start | Tensor or compatible scalar | A scalar 0-D Tensor. It is the first sequence value when limit is provided; otherwise it is treated as the exclusive limit and start becomes 0. |
limit | Tensor or compatible scalar, optional | A scalar 0-D Tensor representing the exclusive upper limit. If omitted, the value supplied as start is used as the limit. |
delta | Tensor or compatible scalar | A scalar 0-D Tensor specifying how much to add to start for each element. Defaults to 1. |
dtype | tf.DType, optional | The type of the resulting Tensor's elements. |
name | string, optional | The operation name. Defaults to "range". |
A one-dimensional Tensor with element type dtype.
import tensorflow as tf
values = tf.range(3, 18, 3)
print(values.numpy()) # [ 3 6 9 12 15]Creates an integer tensor containing values from 3 through 15 in steps of 3.
Generate integer indices for tensor slicing, lookup tables, or iteration-like graph logic.
Build regularly spaced numeric values when the endpoint should not be included.
Create floating-point sequences by supplying floating-point inputs or an explicit dtype.
tf.range returns a rank-1 Tensor containing a numeric sequence. It starts at start, increments by delta, and stops before limit; the element type is inferred unless dtype is provided.
Follow-up: How does the one-argument form work? The supplied argument is treated as limit, start defaults to zero, and the limit remains exclusive.
The signature supports either tf.range(limit, delta=1, dtype=None, name='range') or tf.range(start, limit, delta=1, dtype=None, name='range'). In the one-argument form, start defaults to zero.
Follow-up: What does a negative delta imply? The sequence advances downward, so the bounds and step must be oriented consistently for values to be produced.
start, limit, and delta are scalar, zero-dimensional Tensor-like inputs. limit is the exclusive upper bound, delta defaults to one, and name defaults to 'range'.
Follow-up: What shape does the result have? It is always a one-dimensional Tensor, with element type dtype.
tf.linspacetf.rangetf.ragged.rangetf.compat.v1.range