tf.linspace
Stay organized with collections
Save and categorize content based on your preferences.
Generates evenly-spaced values in an interval along a given axis.
tf.linspace(
start, stop, num, name=None, axis=0
)
A sequence of num
evenly-spaced values are generated beginning at start
along a given axis
.
If num > 1
, the values in the sequence increase by
(stop - start) / (num - 1)
, so that the last one is exactly stop
.
If num <= 0
, ValueError
is raised.
Matches
np.linspace's
behaviour
except when num == 0
.
For example:
tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]
Start
and stop
can be tensors of arbitrary size:
tf.linspace([0., 5.], [10., 40.], 5, axis=0)
<tf.Tensor: shape=(5, 2), dtype=float32, numpy=
array([[ 0. , 5. ],
[ 2.5 , 13.75],
[ 5. , 22.5 ],
[ 7.5 , 31.25],
[10. , 40. ]], dtype=float32)>
Axis
is where the values will be generated (the dimension in the
returned tensor which corresponds to the axis will be equal to num
)
tf.linspace([0., 5.], [10., 40.], 5, axis=-1)
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[ 0. , 2.5 , 5. , 7.5 , 10. ],
[ 5. , 13.75, 22.5 , 31.25, 40. ]], dtype=float32)>
Args |
start
|
A Tensor . Must be one of the following types: bfloat16 ,
float32 , float64 . N-D tensor. First entry in the range.
|
stop
|
A Tensor . Must have the same type and shape as start . N-D tensor.
Last entry in the range.
|
num
|
A Tensor . Must be one of the following types: int32 , int64 . 0-D
tensor. Number of values to generate.
|
name
|
A name for the operation (optional).
|
axis
|
Axis along which the operation is performed (used only when N-D
tensors are provided).
|
Returns |
A Tensor . Has the same type as start .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.linspace\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/math_ops.py#L115-L225) |\n\nGenerates evenly-spaced values in an interval along a given axis.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.lin_space`](https://www.tensorflow.org/api_docs/python/tf/linspace), [`tf.compat.v1.linspace`](https://www.tensorflow.org/api_docs/python/tf/linspace)\n\n\u003cbr /\u003e\n\n tf.linspace(\n start, stop, num, name=None, axis=0\n )\n\nA sequence of `num` evenly-spaced values are generated beginning at `start`\nalong a given `axis`.\nIf `num \u003e 1`, the values in the sequence increase by\n`(stop - start) / (num - 1)`, so that the last one is exactly `stop`.\nIf `num \u003c= 0`, `ValueError` is raised.\n\nMatches\n[np.linspace](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html)'s\nbehaviour\nexcept when `num == 0`.\n\n#### For example:\n\n tf.linspace(10.0, 12.0, 3, name=\"linspace\") =\u003e [ 10.0 11.0 12.0]\n\n`Start` and `stop` can be tensors of arbitrary size: \n\n tf.linspace([0., 5.], [10., 40.], 5, axis=0)\n \u003ctf.Tensor: shape=(5, 2), dtype=float32, numpy=\n array([[ 0. , 5. ],\n [ 2.5 , 13.75],\n [ 5. , 22.5 ],\n [ 7.5 , 31.25],\n [10. , 40. ]], dtype=float32)\u003e\n\n`Axis` is where the values will be generated (the dimension in the\nreturned tensor which corresponds to the axis will be equal to `num`) \n\n tf.linspace([0., 5.], [10., 40.], 5, axis=-1)\n \u003ctf.Tensor: shape=(2, 5), dtype=float32, numpy=\n array([[ 0. , 2.5 , 5. , 7.5 , 10. ],\n [ 5. , 13.75, 22.5 , 31.25, 40. ]], dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|-------------------------------------------------------------------------------------------------------------------------|\n| `start` | A `Tensor`. Must be one of the following types: `bfloat16`, `float32`, `float64`. N-D tensor. First entry in the range. |\n| `stop` | A `Tensor`. Must have the same type and shape as `start`. N-D tensor. Last entry in the range. |\n| `num` | A `Tensor`. Must be one of the following types: `int32`, `int64`. 0-D tensor. Number of values to generate. |\n| `name` | A name for the operation (optional). |\n| `axis` | Axis along which the operation is performed (used only when N-D tensors are provided). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same type as `start`. ||\n\n\u003cbr /\u003e"]]