maxframe.tensor.linspace#

maxframe.tensor.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, gpu=None, chunk_size=None)[源代码]#

在指定区间内返回均匀间隔的数字。

返回在区间 [start, stop] 上计算出的 num 个均匀间隔的样本。

区间的端点可以选择性地被排除。

参数:
  • start (scalar) -- 序列的起始值。

  • stop (scalar) -- 序列的结束值,除非 endpoint 设置为 False。在这种情况下,序列包含 num + 1 个均匀间隔样本中除了最后一个的所有样本,因此 stop 被排除。注意当 endpoint 为 False 时,步长会发生变化。

  • num (int, optional) -- 要生成的样本数量。默认为 50。必须为非负数。

  • endpoint (bool, optional) -- 如果为 True,stop 是最后一个样本。否则,它不被包含。默认为 True。

  • retstep (bool, optional) -- 如果为 True,返回 (samples, step),其中 step 是样本之间的间隔。

  • dtype (dtype, optional) -- 输出张量的类型。如果未提供 dtype,则从其他输入参数推断数据类型。

  • gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False

  • chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小

返回:

  • samples (Tensor) -- 在闭区间 [start, stop] 或半开区间 [start, stop) 中有 num 个等间距的样本(取决于 endpoint 是否为 True)。

  • step (float, 可选) -- 仅当 retstep 为 True 时返回

    样本之间的间距大小。

参见

arange

linspace 类似,但使用步长(而不是样本数量)。

logspace

在对数空间中均匀分布的样本。

示例

>>> import maxframe.tensor as mt
>>> mt.linspace(2.0, 3.0, num=5).execute()
array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
>>> mt.linspace(2.0, 3.0, num=5, endpoint=False).execute()
array([ 2. ,  2.2,  2.4,  2.6,  2.8])
>>> mt.linspace(2.0, 3.0, num=5, retstep=True).execute()
(array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

图示:

>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = mt.zeros(N)
>>> x1 = mt.linspace(0, 10, N, endpoint=True)
>>> x2 = mt.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1.execute(), y.execute(), 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2.execute(), y.execute() + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()