maxframe.tensor.expand_dims#

maxframe.tensor.expand_dims(a, axis)[源代码]#

扩展张量的形状。

插入一个新轴,该轴将出现在扩展数组形状的 axis 位置。

参数:
  • a (array_like) -- 输入张量。

  • axis (int) -- 新轴在扩展轴中的位置。

返回:

res -- 输出张量。其维度数比输入张量多一。

返回类型:

Tensor

参见

squeeze

逆操作,移除单例维度

reshape

插入、移除和组合维度,以及调整现有维度的大小

doc.indexing, atleast_1d, atleast_2d, atleast_3d

示例

>>> import maxframe.tensor as mt
>>> x = mt.array([1,2])
>>> x.shape
(2,)

以下等价于 x[mt.newaxis,:]x[mt.newaxis]

>>> y = mt.expand_dims(x, axis=0)
>>> y.execute()
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = mt.expand_dims(x, axis=1)  # Equivalent to x[:,mt.newaxis]
>>> y.execute()
array([[1],
       [2]])
>>> y.shape
(2, 1)

请注意,某些示例可能使用 None 而不是 np.newaxis。它们是相同的对象:

>>> mt.newaxis is None
True