tf.linspace
沿给定轴在间隔内生成均匀间隔的值。
tf.linspace(start, stop, num, name=None, axis=0)
参数 | |
start | 张量。必须为以下类型:bfloat16,float32,float64。N-D张量。范围中的第一项。 |
stop | 张量。必须和start有相同的类型和形状。N-D张量。范围中的最后一项。 |
num | 张量。必须为以下类型:int32,int64。0-D张量。生成数值的数量。 |
name | 操作的名称(可选)。 |
axis | 执行操作的Axis(仅在提供N-D张量时使用)。 |
Returns | 张量。和start有相同的类型。 |
从起始点开始,沿着给定的轴生成一个num均匀间隔值序列。
如果num > 1,序列中的值加(stop - start) / (num - 1),最后一个值正好是stop。
如果num <= 0,则引发ValueError。
实例
(python3.7) E:\PYTHON>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.linspace(10.0, 12.0, 3, name="linspace")
2023-03-28 10:38:30.881152: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([10., 11., 12.], dtype=float32)>
>>>
Start和stop可以是任意大小的张量:
>>> 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是生成值的地方(对应于Axis返回的张量维度将等于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)>
内容参考自:https://www.tensorflow.org/api_docs/python/tf/linspace