add chinese API documentation of Flatten, Eye, Gather, etc.

This commit is contained in:
wangshuide2020 2021-12-04 18:09:50 +08:00
parent eb9b976082
commit ad31be6507
15 changed files with 762 additions and 0 deletions

View File

@ -0,0 +1,38 @@
mindspore.nn.Flatten
====================
.. py:class:: mindspore.nn.Flatten
对输入Tensor的第0维的batch size之外的维度进行展平操作。
**输入:**
- **x** (Tensor) - 要展平的输入Tensor。shape为 :math:`(N,*)`,其中 :math:`*` 表示任意的附加维度数。数据类型为Number。
**输出:**
Tensorshape为 :math:`(N,X)`,其中 :math:`X` 是输入 x 的shape除N之外的其余维度的乘积。
**异常:**
- **TypeError:** `x` 不是Tensor。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([[[1.2, 1.2], [2.1, 2.1]], [[2.2, 2.2], [3.2, 3.2]]]), mindspore.float32)
>>> net = nn.Flatten()
>>> output = net(x)
>>> print(output)
[[1.2 1.2 2.1 2.1]
[2.2 2.2 3.2 3.2]]
>>> print(f"Before flatten the x shape is {x.shape}.")
展平前x的shape为(2, 2, 2).
>>> print(f"After flatten the output shape is {output.shape}.")
展平后输出的shape为(2, 4).

View File

@ -0,0 +1,72 @@
mindspore.nn.Moments
====================
.. py:class:: mindspore.nn.Moments(axis=None, keep_dims=None)
计算 `x` 的均值和方差。
均值和方差是通过聚合 `x` 在 `axis` 上的值来计算的。特别的,如果 `x` 是1-D的Tensor `axis` 等于0这相当于计算向量的均值和方差。
**参数:**
- **axis** (Union[int, tuple(int), None]): 沿指定 `axis` 计算均值和方差值为None时代表计算 `x` 所有值的均值和方差。默认值None。
- **keep_dims** (Union[bool, None]): 如果为True计算结果会保留 `axis` 的维度即均值和方差的维度与输入的相同。如果为False或None则会消减 `axis` 的维度。默认值None。
**输入:**
- **x** (Tensor) - 用于计算均值和方差的Tensor。数据类型仅支持float16和float32。shape为 :math:`(N,*)` 其中 :math:`*` 表示任意的附加维度数。
**输出:**
- **mean** (Tensor) - `x` 在 `axis` 上的均值,数据类型与输入 `x` 相同。
- **variance** (Tensor) - `x` 在 `axis` 上的方差,数据类型与输入 `x` 相同。
**异常:**
- **TypeError** `axis` 不是inttuple或None。
- **TypeError** `keep_dims` 既不是bool也不是None。
- **TypeError** `x` 的数据类型既不是float16也不是float32。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([[[[1, 2, 3, 4], [3, 4, 5, 6]]]]), mindspore.float32)
>>> net = nn.Moments(axis=0, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00],
[ 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]]]]),
Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]]]))
>>> net = nn.Moments(axis=1, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00],
[ 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]]]]),
Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]]]))
>>> net = nn.Moments(axis=2, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 1, 4], dtype=Float32, value=
[[[[ 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 5.00000000e+00]]]]),
Tensor(shape=[1, 1, 1, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]]]))
>>> net = nn.Moments(axis=3, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 1], dtype=Float32, value=
[[[[ 2.50000000e+00],
[ 4.50000000e+00]]]]), Tensor(shape=[1, 1, 2, 1], dtype=Float32, value=
[[[[ 1.25000000e+00],
[ 1.25000000e+00]]]]))

View File

@ -0,0 +1,56 @@
mindspore.nn.SmoothL1Loss
==========================
.. py:class:: mindspore.nn.SmoothL1Loss(beta=1.0)
创建一个标准来计算loss函数如果输入的绝对误差小于 `beta` 则用平方项,否则用绝对误差项。
SmoothL1Loss可以看成 class: `mindspore.nn.L1Loss` 的修改版本,也可以看成 class: `mindspore.nn.L1Loss` 和 class: `mindspore.ops.L2Loss` 的组合。
class: `mindspore.nn.L1Loss` 计算两个输入Tensor之间的绝对误差而 class: `mindspore.ops.L2Loss` 计算两个输入Tensor之间的平方误差。 class: `mindspore.ops.L2Loss` 通常更快收敛,但对离群值的鲁棒性较差。
给定两个输入 :math:`x,\ y`,长度为 :math:`N` unreduced SmoothL1Loss定义如下
.. math::
L_{i} =
\begin{cases}
\frac{0.5 (x_i - y_i)^{2}}{\text{beta}}, & \text{if } |x_i - y_i| < \text{beta} \\
|x_i - y_i| - 0.5 \text{beta}, & \text{otherwise.}
\end{cases}
其中, :math:`\text{beta}` 控制loss函数从二次变为线性。 默认值为1.0。 :math:`N` 为batch size。该函数返回一个unreduced loss Tensor。
**参数:**
**beta** (float): 用于控制loss函数从二次变为线性的参数。默认值1.0。
**输入:**
- **logits** (Tensor)预测值shape为 :math:`(N, *)` 的Tensor其中 :math:`*` 表示任意的附加维度数。数据类型必须为float16或float32。
- **labels** (Tensor)目标值shape为 :math:`(N, *)` 的Tensor数据类型和shape与 `logits` 相同。
**输出:**
Tensorshape和数据类型与 `logits` 相同。
**异常:**
- **TypeError** `beta` 不是float。
- **TypeError** `logits` 或 `labels` 的数据类型既不是float16也不是float32。
- **ValueError** `beta` 小于或等于0。
- **ValueError** `logits` 的shape与 `labels` 不同。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> loss = nn.SmoothL1Loss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[0. 0. 0.5]

View File

@ -0,0 +1,51 @@
mindspore.nn.Softmax
====================
.. py:class:: mindspore.nn.Softmax(axis=-1)
Softmax激活函数。
计算n维输入Tensor的Softmax函数。
对输入Tensor在 `axis` 上的元素计算其指数函数值,然后归一化到[0, 1]范围总和为1。
Softmax定义为
.. math::
\text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},
其中, :math:`x_{i}` 是输入Tensor在 `axis` 上的第 :math:`i` 个元素。
**参数:**
**axis** (Union[int, tuple[int]]): 指定Softmax运算的axis-1表示最后一个维度。默认值-1。
**输入:**
**x** (Tensor)用于计算Softmax函数的Tensor数据类型为float16或float32。
**输出:**
Tensorshape和数据类型与 `x` 相同,取值范围[0,1]。
**异常:**
- **TypeError** `axis` 既不是int也不是tuple。
- **TypeError** `x` 的数据类型既不是float16也不是float32。
- **ValueError** `axis` 是长度小于1的tuple。
- **ValueError** `axis` 是一个tuple其元素不都在 `[-x.ndim, x.ndim)` 范围内。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> softmax = nn.Softmax()
>>> output = softmax(x)
>>> print(output)
[0.03168 0.01166 0.0861 0.636 0.2341 ]

View File

@ -0,0 +1,78 @@
mindspore.nn.Tril
=================
.. py:class:: mindspore.nn.Tril
返回一个Tensor其中第 `k` 个对角线以上的元素被置为零。
矩阵的下三角把矩阵分成对角线上和对角线下的元素。
参数 `k` 控制着矩阵的对角线。如果 `k` 为0则保留主对角线上和下面的所有元素。正值包括主对角线上方尽可能多的对角线类似地负值排除主对角线下方尽可能多的对角线。
**输入:**
- **x** (Tensor)输入Tensor。数据类型为Number。shape为 :math:`(N,*)`,其中 :math:`*` 表示任意的附加维度数。
- **k** (Int)对角线的索引。默认值0。
**输出:**
Tensorshape和数据类型与 `x` 相同。
**异常:**
- **TypeError** `k` 不是int。
- **ValueError** `x` 的维度小于1。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [10, 11, 12, 13],
... [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x)
>>> print(result)
[[ 1 0 0 0]
[ 5 6 0 0]
[10 11 12 0]
[14 15 16 17]]
>>> x = Tensor(np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [10, 11, 12, 13],
... [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, 1)
>>> print(result)
[[ 1 2 0 0]
[ 5 6 7 0]
[10 11 12 13]
[14 15 16 17]]
>>> x = Tensor(np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [10, 11, 12, 13],
... [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, 2)
>>> print(result)
[[ 1 2 3 0]
[ 5 6 7 8]
[10 11 12 13]
[14 15 16 17]]
>>> x = Tensor(np.array([[ 1, 2, 3, 4],
... [ 5, 6, 7, 8],
... [10, 11, 12, 13],
... [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, -1)
>>> print(result)
[[ 0 0 0 0]
[ 5 0 0 0]
[10 11 0 0]
[14 15 16 0]]

View File

@ -0,0 +1,46 @@
mindspore.nn.LeakyReLU
=======================
.. py:class:: mindspore.nn.LeakyReLU(alpha=0.2)
Leaky ReLU激活函数。
LeakyReLU与ReLU相似但LeakyReLU有一个斜率使其在x<0时不等于0该激活函数定义如下
.. math::
\text{leaky_relu}(x) = \begin{cases}x, &\text{if } x \geq 0; \cr
\text{alpha} * x, &\text{otherwise.}\end{cases}
更多细节详见 `Rectifier Nonlinearities Improve Neural Network Acoustic Models <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`_
**参数**
- **alpha** (`Union[int, float]`) x<0时激活函数的斜率默认值0.2。
**输入**
- **x** Tensor - LeakyReLU的输入。shape为 :math:`(N, *)` ,其中 :math:`*` 表示任意的附加维度数。
**输出**
Tensorshape和数据类型与 `x` 的相同。
**异常**
- **TypeError** `alpha` 不是浮点数或整数。
**支持平台**
`Ascend` `GPU` `CPU`
**样例** :
.. code-block::
>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> leaky_relu = nn.LeakyReLU()
>>> output = leaky_relu(x)
>>> print(output)
[[-0.2 4. -1.6]
[ 2. -1. 9. ]]

View File

@ -0,0 +1,44 @@
mindspore.ops.BiasAdd
=====================
.. py:class:: mindspore.ops.BiasAdd(data_format="NCHW")
返回输入Tensor和偏置Tensor之和。
将1-D偏置Tensor加到输入Tensor中相加前会把偏置Tensor广播成与 `input_x` 的shape一致。
**参数:**
**data_format** (str): 输入和输出数据的格式。取值为'NHWC'、'NCHW'或'NCDHW'。默认值:'NCHW'。
**输入:**
- **input_x** (Tensor)输入Tensor。shape可以有2~5个维度。数据类型应为float16或float32。
- **bias** (Tensor)偏置Tensorshape为 :math:`(C)`。`bias` 的shape必须与 `input_x` 的通道维度相同。数据类型应为float16或float32。
**输出:**
Tensorshape和数据类型与 `input_x` 相同。
**异常:**
- **TypeError** `data_format` 不是str。
- **TypeError** `input_x` 或 `bias` 不是Tensor。
- **TypeError** `input_x` 或 `bias` 的数据类型既不是float16也不是float32。
- **TypeError** `input_x` 的维度不在[2, 5]范围内。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> input_x = Tensor(np.arange(6).reshape((2, 3)), mindspore.float32)
>>> bias = Tensor(np.random.random(3).reshape((3,)), mindspore.float32)
>>> bias_add = ops.BiasAdd()
>>> output = bias_add(input_x, bias)
>>> print(output.shape)
(2, 3)

View File

@ -0,0 +1,52 @@
mindspore.ops.Elu
=================
.. py:class:: mindspore.ops.Elu(*args, **kwargs)
指数线性单元激活函数Exponential Linear Uint activation function
对输入的每个元素计算Elu。该激活函数定义如下
.. math::
\text{ELU}(x)= \left\{
\begin{array}{align}
\alpha(e^{x} - 1) & \text{if } x \le 0\\
x & \text{if } x \gt 0\\
\end{array}\right.
ELU相关图参见 `ELU <https://en.wikipedia.org/wiki/Activation_function#/media/File:Activation_elu.svg>`_ 。
**参数:**
**alpha** (float)Elu的alpha值数据类型为浮点数。默认值1.0。
**输入:**
**x** (Tensor) - 用于计算Elu的Tensor数据类型为float16或float32。shape为 :math:`(N,*)` :math:`*` 表示任意的附加维度数。
**输出:**
Tensorshape和数据类型与 `x` 相同。
**异常:**
- **TypeError** `alpha` 不是float。
- **TypeError** `x` 的数据类型既不是float16也不是float32。
- **ValueError** `alpha` 不等于1.0。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> elu = ops.Elu()
>>> output = elu(x)
>>> print(output)
[[-0.63212055 4. -0.99966455]
[ 2. -0.99326205 9. ]]

View File

@ -0,0 +1,51 @@
mindspore.ops.Eye
==================
.. py:class:: mindspore.ops.Eye(*args, **kwargs)
创建一个对角线上为1其余为0的Tensor。
**输入:**
- **n** (int) - 指定返回Tensor的行数。仅支持常量值。
- **m** (int) - 指定返回Tensor的列数。仅支持常量值。
- **t** (mindspore.dtype) - 指定返回Tensor的数据类型。数据类型可以是Number。
**输出:**
Tensor对角线上为1其余的元素为0。它的shape由 `n` 和 `m` 指定。数据类型由 `t` 指定。
**异常:**
- **TypeError** `m` 或 `n` 不是int。
- **ValueError** `m` 或 `n` 小于1。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> eye = ops.Eye()
>>> output = eye(2, 2, mindspore.int32)
>>> print(output)
[[1 0]
[0 1]]
>>> print(output.dtype)
Int32
>>> output = eye(1, 2, mindspore.float64)
>>> print(output)
[[1. 0.]]
>>> print(output.dtype)
Float64
>>> # if wants a anti-diagonal
>>> anti_diagonal_input = eye(2, 2, mindspore.int32)
>>> # Note that ReverseV2 only supports "Ascend" at this time
>>> reverse = ops.ReverseV2([1])
>>> anti_diagonal_output = reverse(anti_diagonal_input)
>>> print(anti_diagonal_output)
[[0 1]
[1 0]]

View File

@ -0,0 +1,40 @@
mindspore.ops.Fill
==================
.. py:class:: mindspore.ops.Fill(*args, **kwargs)
创建一个填充了Scalar值的Tensor。shape由 `shape` 参数指定,并用`value` 值填充该Tensor。
**输入:**
- **type** (mindspore.dtype) - 指定输出Tensor的数据类型。只支持常量值。
- **shape** (tuple) - 指定输出Tensor的shape。只支持常量值。
- **value** (scalar) - 用来填充输出Tensor的值。只支持常量值。
**输出:**
Tensorshape为 `shape` 的值,值为 `value` 。
**异常:**
**TypeError** `shape` 不是元组。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> fill = ops.Fill()
>>> output = fill(mindspore.float32, (2, 2), 1)
>>> print(output)
[[1. 1.]
[1. 1.]]
>>> output = fill(mindspore.float32, (3, 3), 0)
>>> print(output)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

View File

@ -0,0 +1,46 @@
mindspore.ops.Gather
======================
.. py:class:: mindspore.ops.Gather(*args, **kwargs)
返回输入Tensor在指定 `axis` 上 `input_indices` 索引对应的元素组成的切片。
**输入:**
- **input_params** (Tensor) - 原始Tensorshape为 :math:`(x_1, x_2, ..., x_R)`。
- **input_indices** (Tensor) - 要切片的索引Tensorshape为 :math:`(y_1, y_2, ..., y_S)`。
指定原始Tensor中要切片的索引。其值必须在 `[0, input_param.shape[axis])`范围内该校验仅在CPU上生效。在Ascend和GPU上超出该范围时对应的值会置为0。
数据类型可以是int32或int64。
- **axis** (int) - 指定要切片的维度索引。
**输出:**
Tensorshape为 :math:`input\_params.shape[:axis] + input\_indices.shape + input\_params.shape[axis + 1:]`。
**异常:**
- **TypeError** `axis` 不是int。
- **TypeError** `input_params` 或 `input_indices` 不是Tensor。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> input_params = Tensor(np.array([[1, 2, 7, 42], [3, 4, 54, 22], [2, 2, 55, 3]]), mindspore.float32)
>>> input_indices = Tensor(np.array([1, 2]), mindspore.int32)
>>> axis = 1
>>> output = ops.Gather()(input_params, input_indices, axis)
>>> print(output)
[[ 2. 7.]
[ 4. 54.]
[ 2. 55.]]
>>> axis = 0
>>> output = ops.Gather()(input_params, input_indices, axis)
>>> print(output)
[[3. 4. 54. 22.]
[2. 2. 55. 3.]]

View File

@ -0,0 +1,51 @@
mindspore.ops.Greater
=====================
.. py:class:: mindspore.ops.Greater(*args, **kwargs)
按元素计算 :math:`x > y` 的bool值。
输入 `x` 和 `y` 遵循隐式类型转换规则,使数据类型保持一致。
输入必须是两个Tensor或一个Tensor和一个Scalar。
当输入是两个Tensor时它们的数据类型不能同时是bool它们的shape可以广播。
当输入是一个Tensor和一个Scalar时Scalar只能是一个常数。
.. math::
out_{i} =\begin{cases}
& \text{True, if } x_{i}>y_{i} \\
& \text{False, if } x_{i}<=y_{i}
\end{cases}
.. note::
支持广播。
**输入:**
- **x** (Union[Tensor, Number, bool]) - 第一个输入是一个Number、bool值或数据类型为Number或bool的Tensor。
- **y** (Union[Tensor, Number, bool]) - 第二个输入当第一个输入是Tensor时第二个输入应该是一个Number或bool值或数据类型为Number或bool的Tensor。
**输出:**
Tensorshape与广播后的shape相同数据类型为bool。
**异常:**
**TypeError** `x` 和 `y` 都不是Tensor。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 1, 4]), mindspore.int32)
>>> greater = ops.Greater()
>>> output = greater(x, y)
>>> print(output)
[False True False]

View File

@ -0,0 +1,37 @@
mindspore.ops.Inv
=================
.. py:class:: mindspore.ops.Inv(*args, **kwargs)
按元素计算输入Tensor的倒数。
.. math::
out_i = \frac{1}{x_{i} }
**输入:**
**x** (Tensor) - shape为 :math:`(N,*)` 的Tensor其中 :math:`*` 表示任意的附加维度数。数据类型必须是float16、float32或int32。
**输出:**
Tensorshape和数据类型与 `x` 相同。
**异常:**
**TypeError** `x` 的数据类型不是float16、float32或int32。
**支持平台:**
``Ascend``
**样例:**
.. code-block::
>>> inv = ops.Inv()
>>> x = Tensor(np.array([0.25, 0.4, 0.31, 0.52]), mindspore.float32)
>>> output = inv(x)
>>> print(output)
[4. 2.5 3.2258065 1.923077 ]

View File

@ -0,0 +1,49 @@
mindspore.ops.L2Normalize
==========================
.. py:class:: mindspore.ops.L2Normalize(*args, **kwargs)
L2范数归一化算子。
该算子将对输入 `x` 在给定 `axis` 上的元素进行归一化。函数定义如下:
.. math::
\displaylines{{\text{output} = \frac{x}{\sqrt{\text{max}(\parallel x_i \parallel^2 , \epsilon)} } } \\
{\parallel x_i \parallel^2 = (\sum_{i}^{}\left | x_i \right | ^2 )^{1/2}} }
其中 :math:`\epsilon` 表示 `epsilon` :math:`\sum_{i}^{}\left | x_i \right | ^2` 表示计算输入 `x` 在给定 `axis` 上元素的平方和。
**参数:**
- **axis** (Union[list(int), tuple(int), int]):输入的起始 `axis`用于L2范数归一化。默认值0。
- **epsilon** (float)为了数值稳定性而引入的很小的浮点数。默认值1e-4。
**输入:**
**x** (Tensor) - 计算归一化的输入。shape为 :math:`(N, *)` ,其中 :math:`*` 表示任意的附加维度数。数据类型必须为float16或float32。
**输出:**
Tensorshape和数据类型与 `x` 的相同。
**异常:**
- **TypeError** `axis` 不是list、tuple或int。
- **TypeError** `epsilon` 不是float。
- **TypeError** `x` 不是Tensor。
- **TypeError** `x` 的数据类型既不是float16也不是float32。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> l2_normalize = ops.L2Normalize()
>>> x = Tensor(np.random.randint(-256, 256, (2, 3, 4)), mindspore.float32)
>>> output = l2_normalize(x)
>>> print(output.shape)
(2, 3, 4)

View File

@ -0,0 +1,51 @@
mindspore.ops.Squeeze
=====================
.. py:class:: mindspore.ops.Squeeze(axis=())
返回删除指定 `axis` 中大小为1的维度的Tensor。
如果指定了 `axis`,则删除指定 `axis` 中大小为1的维度。
如果 `axis` 为None则删除所有大小为1的维度。
例如如果输入的shape为(A×1×B×C×1×D)则输出的Tensor的shape为(A×B×C×D)如果指定维度squeeze操作仅在指定维度中进行。
如果输入的shape为(A×1×B)squeeze(input_x, 0)不会改变输入的Tensor但squeeze(input_x, 1)会使输入Tensor的shape变为(A×B)。
请注意在动态图模式下输出Tensor将与输入Tensor共享数据并且没有Tensor数据复制过程。
.. note::
维度索引从0开始并且必须在 `[-input_x.ndim, input_x.ndim)` 范围内。
**参数:**
**axis** (Union[int, tuple(int)])指定待删除shape的维度索引它会删除该维度中所有大小为1的维度。如果指定了维度索引其数据类型必须为int32或int64。默认值()空tuple。
**输入:**
**input_x** (Tensor) - 用于计算Squeeze的输入Tensorshape为 :math:`(x_1, x_2, ..., x_R)`。
**输出:**
Tensorshape为 :math:`(x_1, x_2, ..., x_S)`。
**异常:**
- **TypeError** `axis` 既不是int也不是tuple。
- **TypeError** `axis` 是tuple其元素并非全部是int。
- **ValueError** 指定 `axis` 的对应维度不等于1。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
.. code-block::
>>> input_x = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> squeeze = ops.Squeeze(2)
>>> output = squeeze(input_x)
>>> print(output)
[[1. 1.]
[1. 1.]
[1. 1.]]