[DOC][OP]Fix space_to_batch_nd op doc

Merge pull request  from zichun_ye/code_docs_space_to_batch_nd
This commit is contained in:
i-robot 2022-06-02 02:06:12 +00:00 committed by Gitee
commit ae0a4815f5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 55 additions and 53 deletions
docs/api/api_python/ops
mindspore/python/mindspore
_extends/parse
common
ops
function
operations

View File

@ -1,11 +1,11 @@
mindspore.ops.space_to_batch_nd
================================
.. py:function:: mindspore.ops.space_to_batch_nd(input_x, block_shape, paddings)
.. py:function:: mindspore.ops.space_to_batch_nd(input_x, block_size, paddings)
将空间维度划分为对应大小的块,然后在批次维度重排张量。
此函数将输入的空间维度(Space) `[1, ..., M]` 划分为形状为 `block_shape` 的块网格,并将这些块在批次维度上 (Batch默认是第0维) 中交错排列。
此函数将输入的空间维度(Space) `[1, ..., M]` 划分为形状为 `block_size` 的块网格,并将这些块在批次维度上 (Batch默认是第0维) 中交错排列。
输出的张量在空间维度上的截面是输入在对应空间维度上截面的一个网格,而输出的批次维度的大小为空间维度分解成块网格的数量乘以输入的批次维度的大小。
在划分成块之前,输入的空间维度会根据 `paddings` 填充零。
如此,假设输入的形状为 :math:`(n, c_1, ... c_k, w_1, ..., w_M)`,则输出的形状为 :math:`(n', c_1, ... c_k, w'_1, ..., w'_M)`,
@ -24,7 +24,7 @@ mindspore.ops.space_to_batch_nd
**参数:**
- **input_x** (Tensor) - 输入张量Ascend平台必须为四维。
- **block_shape** (list[int], tuple[int], int) - 块形状描述空间维度为分割的个数。如果 `block_shape` 为list或者tuple其长度 `M` 为空间维度的长度。如果 `block_shape` 为整数,那么所有空间维度分割的个数均为 `block_shape` 。在Ascend后端 `M` 必须为2。
- **block_size** (list[int], tuple[int], int) - 块形状描述空间维度为分割的个数。如果 `block_size` 为list或者tuple其长度 `M` 为空间维度的长度。如果 `block_size` 为整数,那么所有空间维度分割的个数均为 `block_size` 。在Ascend后端 `M` 必须为2。
- **paddings** (tuple, list) - 空间维度的填充大小。
**返回:**
@ -33,10 +33,10 @@ mindspore.ops.space_to_batch_nd
**异常:**
- **TypeError** - 如果 `block_shape` 不是 list, tuple 或者 int。
- **TypeError** - 如果 `block_size` 不是 list, tuple 或者 int。
- **TypeError** - 如果 `paddings` 不是 list 或者 tuple。
- **ValueError** - 如果当 `block_shape` 为 list 或 tuple `block_shape` 不是一维。
- **ValueError** - 如果 Ascend 平台上 `block_shape` 长度不是2。
- **ValueError** - 如果 `paddings` 的形状不是 (2, M), 其中 M 为 `block_shape` 的长度。
- **ValueError** - 如果 `block_shape` 的元素不是大于一的整数。
- **ValueError** - 如果当 `block_size` 为 list 或 tuple `block_size` 不是一维。
- **ValueError** - 如果 Ascend 平台上 `block_size` 长度不是2。
- **ValueError** - 如果 `paddings` 的形状不是 (2, M), 其中 M 为 `block_size` 的长度。
- **ValueError** - 如果 `block_size` 的元素不是大于一的整数。
- **ValueError** - 如果 `paddings` 的元素不是非负的整数。

View File

@ -390,10 +390,10 @@ def swapaxes(x, axis1, axis2):
new_perm = None
if axis2 + 1 < x.ndim:
new_perm = perm[0:axis1] + perm[axis2:axis2 + 1] + \
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1] + perm[axis2 + 1:]
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1] + perm[axis2 + 1:]
else:
new_perm = perm[0:axis1] + perm[axis2:axis2 + 1] + \
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1]
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1]
return F.transpose(x, new_perm)
@ -2116,22 +2116,6 @@ def space_to_batch_nd(x, block_shape, paddings):
Divides spatial dimensions into blocks and combines the block size with the original batch.
Refer to :func:`mindspore.ops.space_to_batch_nd` for more detail.
Supported Platforms:
``Ascend`` ``CPU``
Examples:
>>> import numpy as np
>>> from mindspore import Tensor
>>> block_shape = [2, 2]
>>> paddings = [[0, 0], [0, 0]]
>>> input_x = Tensor(np.array([[[[1, 2], [3, 4]]]]), mindspore.float32)
>>> output = input_x.space_to_batch_nd(block_shape, paddings)
>>> print(output)
[[[[1.]]]
[[[2.]]]
[[[3.]]]
[[[4.]]]]
"""
return P.SpaceToBatchND(block_shape, paddings)(x)

View File

@ -1671,10 +1671,10 @@ class Tensor(Tensor_):
perm = tuple(range(0, self.ndim))
if axis2 + 1 < self.ndim:
new_perm = perm[0:axis1] + perm[axis2:axis2 + 1] + \
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1] + perm[axis2 + 1:]
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1] + perm[axis2 + 1:]
else:
new_perm = perm[0:axis1] + perm[axis2:axis2 + 1] + \
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1]
perm[axis1 + 1:axis2] + perm[axis1:axis1 + 1]
return tensor_operator_registry.get('transpose')()(self, new_perm)
@ -2948,6 +2948,24 @@ class Tensor(Tensor_):
Refer to :func:`mindspore.ops.space_to_batch_nd` for more detail.
Args:
block_shape (Union[list(int), tuple(int), int]): The block size of dividing block with all value greater
than 1.
paddings (Union[tuple, list]): The padding values for spatial dimensions, containing M subtraction list.
Each contains 2 integer values.
Returns:
Tensor, the output tensor with the same data type as the input tensor.
Raises:
ValueError: If `block_shape` is not one dimensional when `block_shape` is a list or tuple.
ValueError: If the length of `block_shape` is not 2 on Ascend.
ValueError: If the element of `block_shape` is not an integer larger than 1.
ValueError: If shape of `paddings` is not (2, M), where M is the length of `block_shape`.
ValueError: If the element of `paddings` is not an integer larger than 0.
TypeError: If `block_shape` is not one of list, tuple, int.
TypeError: If `paddings` is neither list nor tuple.
Supported Platforms:
``Ascend`` ``CPU``

View File

@ -1969,53 +1969,53 @@ def space_to_batch_nd(input_x, block_size, paddings):
r"""
Divides a tensor's spatial dimensions into blocks and combines the block sizes with the original batch.
This operation will divide spatial dimensions into blocks with `block_shape`,
This operation will divide spatial dimensions into blocks with `block_size`,
and after division, the output tensor's spatial dimension is the corresponding number of blocks.
The output tensor's batch dimension is the product of the original batch and the product of `block_shape`.
The output tensor's batch dimension is the product of the original batch and the product of `block_size`.
Before division, the spatial dimensions of the input are zero padded according to paddings if necessary.
Assume input shape is :math:`(n, c_1, ... c_k, w_1, ..., w_M)` with
:math:`block\_shape` and :math:`paddings`. Then the shape of the output tensor will be
:math:`block\_size` and :math:`paddings`. Then the shape of the output tensor will be
:math:`(n', c_1, ... c_k, w'_1, ..., w'_M)`, where
.. math::
\begin{array}{ll} \\
n' = n*(block\_shape[0] * ... * block\_shape[M]) \\
w'_i = (w_i + paddings[i][0] + paddings[i][1])//block\_shape[i]
n' = n*(block\_size[0] * ... * block\_size[M]) \\
w'_i = (w_i + paddings[i][0] + paddings[i][1])//block\_size[i]
\end{array}
Args:
input_x (Tensor): The input tensor. It must be a 4-D tensor on Ascend.
block_size (Union[list(int), tuple(int), int]): The block shape of dividing block with all value greater
than 1. If `block_shape` is a tuple or list, the length of `block_shape` is M corresponding to the
number of spatial dimensions. If `block_shape` is an int, the block size of M dimensions are the same,
equal to `block_shape`. M must be 2 on Ascend.
paddings (Union[tuple, list]): The padding values for spatial dimensions, containing 2 subtraction list.
Each contains M integer values. All values must be greater than 0.
block_size (Union[list(int), tuple(int), int]): The block size of dividing block with all value greater
than 1. If `block_size` is a tuple or list, the length of `block_size` is M corresponding to the
number of spatial dimensions. If `block_size` is an int, the block size of M dimensions are the same,
equal to `block_size`. M must be 2 on Ascend.
paddings (Union[tuple, list]): The padding values for spatial dimensions, containing M subtraction list.
Each contains 2 integer values. All values must be greater than 0.
`paddings[i]` specifies the paddings for the spatial dimension i,
which corresponds to the input dimension i + offset.
It is required that input_shape[i+offset]+paddings[i][0]+paddings[i][1] is divisible by block_shape[i].
It is required that input_shape[i+offset]+paddings[i][0]+paddings[i][1] is divisible by block_size[i].
M must be 2 on Ascend.
Returns:
Tensor, the output tensor with the same data type as input.
Raises:
ValueError: If `block_shape` is not one dimensional when `block_shape` is a list or tuple.
ValueError: If the length of `block_shape` is not 2 on Ascend.
ValueError: If the element of `block_shape` is not an integer larger than 1.
ValueError: If shape of `paddings` is not (2, M), where M is the length of `block_shape`.
ValueError: If `block_size` is not one dimensional when `block_size` is a list or tuple.
ValueError: If the length of `block_size` is not 2 on Ascend.
ValueError: If the element of `block_size` is not an integer larger than 1.
ValueError: If shape of `paddings` is not (2, M), where M is the length of `block_size`.
ValueError: If the element of `paddings` is not an integer larger than 0.
TypeError: If `block_shape` is not one of list, tuple, int.
TypeError: If `block_size` is not one of list, tuple, int.
TypeError: If `paddings` is neither list nor tuple.
Supported Platforms:
``Ascend`` ``CPU``
Examples:
>>> block_shape = [2, 2]
>>> block_size = [2, 2]
>>> paddings = [[0, 0], [0, 0]]
>>> input_x = Tensor(np.array([[[[1, 2], [3, 4]]]]), mindspore.float32)
>>> output = ops.space_to_batch_nd(input_x, block_shape, paddings)
>>> output = ops.space_to_batch_nd(input_x, block_size, paddings)
>>> print(output)
[[[[1.]]]
[[[2.]]]

View File

@ -3749,7 +3749,7 @@ class StridedSlice(PrimitiveWithInfer):
if has_ellipsis:
# When there is ellipsis, handle the second half of the ellipsis split.
ellipsis_occupied_dims = x_rank - i - (slice_len - (j + 1)) + \
len(tuple(filter(lambda x: x == '1', new_axis_pos[j + 1:slice_len])))
len(tuple(filter(lambda x: x == '1', new_axis_pos[j + 1:slice_len])))
ret_shape.extend(x_shape[i:i + ellipsis_occupied_dims])
j += 1
i += ellipsis_occupied_dims
@ -5591,8 +5591,8 @@ class SpaceToBatchND(PrimitiveWithInfer):
the length of `block_shape` is the number of spatial dimensions, called M later.
If `block_shape` is an int, the block size of M dimensions are the same, equal to `block_shape`.
In this case of Ascend, M must be 2.
paddings (Union[tuple, list]): The padding values for spatial dimensions, containing 2 subtraction list.
Each contains M integer values. All values must be greater than 0.
paddings (Union[tuple, list]): The padding values for spatial dimensions, containing M subtraction list.
Each contains 2 integer values. All values must be greater than 0.
`paddings[i]` specifies the paddings for the spatial dimension i,
which corresponds to the input dimension i + offset.
For each i, input_shape[i + offset]+paddings[i][0]+paddings[i][1]
@ -5602,8 +5602,8 @@ class SpaceToBatchND(PrimitiveWithInfer):
- **input_x** (Tensor) - The input tensor. The input tensor must be a 4-D tensor on Ascend.
Outputs:
Tensor, the output tensor with the same data type as input.
Assume input shape is :math:`(n, c_1, ... c_k, w_1, ..., w_M)` with
Tensor, the output tensor with the same data type as the input.
Assume the input shape is :math:`(n, c_1, ... c_k, w_1, ..., w_M)` with
:math:`block\_shape` and :math:`paddings`.
The shape of the output tensor will be :math:`(n', c_1, ... c_k, w'_1, ..., w'_M)`,
where
@ -5681,7 +5681,7 @@ class SpaceToBatchND(PrimitiveWithInfer):
f"while the shape of blocks is {self.block_shape}.")
for i in range(len(self.block_shape)):
padded = out_shape[i + offset] + self.paddings[i][0] + \
self.paddings[i][1]
self.paddings[i][1]
if padded % self.block_shape[i] != 0:
raise ValueError(f"For '{self.name}', the padded must be divisible by 'block_shape', "
f"where padded = input_x_shape[i + 2] + paddings[i][0] + paddings[i][1], "