From d387fbc6412bf8da86369f524b9a656f78d30295 Mon Sep 17 00:00:00 2001 From: l00591931 Date: Sat, 28 Nov 2020 14:12:24 +0800 Subject: [PATCH] Delete sequence mask and fix comment for Interpolate --- mindspore/nn/layer/basic.py | 20 ++++----- mindspore/ops/_grad/grad_array_ops.py | 10 ----- mindspore/ops/operations/__init__.py | 3 +- mindspore/ops/operations/array_ops.py | 62 --------------------------- tests/ut/python/ops/test_array_ops.py | 22 ---------- 5 files changed, 10 insertions(+), 107 deletions(-) diff --git a/mindspore/nn/layer/basic.py b/mindspore/nn/layer/basic.py index 8df3b18501..996e5513d9 100644 --- a/mindspore/nn/layer/basic.py +++ b/mindspore/nn/layer/basic.py @@ -589,17 +589,15 @@ class Interpolate(Cell): Samples the input tensor to the given size or scale_factor. Now, only support bilinear interpolation. - Args: - size (Union[tuple[int], list[int]]): A tuple or list of 2 int elements '(new_height, new_width)', - the new size of the tensor. Default: None. - scale_factor (int): The scale factor of new size of the tensor. The value should be positive integer. - Default: None. - align_corners (bool): If true, rescale input by '(new_height - 1) / (height - 1)', which exactly aligns - the 4 corners of images and resized images. If false, rescale by 'new_height / height'. Default: False. - Inputs: - **x** (Tensor) - Tensor to be resized. Input tensor must be a 4-D tensor with shape: math:`(batch, channels, height, width)`, with data type of float16 or float32. + - **size** (Union[tuple[int], list[int]]): A tuple or list of 2 int elements '(new_height, new_width)', + the new size of the tensor. One and only one of size and scale_factor can be set to None. Default: None. + - **scale_factor** (int): The scale factor of new size of the tensor. The value should be positive integer. + One and only one of size and scale_factor can be set to None. Default: None. + - **align_corners** (bool): If true, rescale input by '(new_height - 1) / (height - 1)', which exactly aligns + the 4 corners of images and resized images. If false, rescale by 'new_height / height'. Default: False. Outputs: Resized tensor. @@ -609,14 +607,14 @@ class Interpolate(Cell): scale_factor * width)` in float32 Supported Platforms: - ``Ascend`` ``GPU`` ``CPU`` + ``Ascend`` Examples: - >>> from mindspore.ops import operations as P >>> tensor = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mindspore.float32) >>> interpolate = nn.Interpolate() >>> result = interpolate(tensor, size=(5,5)) - >>> assert result.shape == (1, 1, 5, 5) + >>> print(result.shape) + (1, 1, 5, 5) """ def __init__(self): super(Interpolate, self).__init__() diff --git a/mindspore/ops/_grad/grad_array_ops.py b/mindspore/ops/_grad/grad_array_ops.py index 6320f05af1..7788613452 100644 --- a/mindspore/ops/_grad/grad_array_ops.py +++ b/mindspore/ops/_grad/grad_array_ops.py @@ -71,16 +71,6 @@ def get_bprop_zeros(self): return bprop -@bprop_getters.register(P.SequenceMask) -def get_bprop_sequence_mask(self): - """Generate bprop for SequenceMask""" - - def bprop(lengths, dtype, max_length, out, dout): - return zeros_like(dims), zeros_like(max_length) - - return bprop - - @bprop_getters.register(P.DType) def get_bprop_dtype(self): """Generate bprop for DType""" diff --git a/mindspore/ops/operations/__init__.py b/mindspore/ops/operations/__init__.py index 25233947e1..6dadec293b 100644 --- a/mindspore/ops/operations/__init__.py +++ b/mindspore/ops/operations/__init__.py @@ -22,7 +22,7 @@ A collection of operators to build neural networks or to compute functions. from .image_ops import (CropAndResize) from .array_ops import (Argmax, Argmin, Cast, Concat, Pack, Unpack, Diag, DiagPart, DType, ExpandDims, Eye, - Fill, Ones, Zeros, SequenceMask, GatherNd, GatherV2, SparseGatherV2, InvertPermutation, + Fill, Ones, Zeros, GatherNd, GatherV2, SparseGatherV2, InvertPermutation, IsInstance, IsSubClass, ArgMaxWithValue, OnesLike, ZerosLike, Rank, Reshape, ResizeNearestNeighbor, ArgMinWithValue, Meshgrid, SameTypeShape, ScatterAdd, ScatterSub, ScatterMul, ScatterDiv, ScatterMax, ScatterMin, @@ -182,7 +182,6 @@ __all__ = [ 'Fill', 'Ones', 'Zeros', - 'SequenceMask', 'OnesLike', 'ZerosLike', 'Select', diff --git a/mindspore/ops/operations/array_ops.py b/mindspore/ops/operations/array_ops.py index f86f6bc8d5..dab0f7dbe6 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -1227,68 +1227,6 @@ class Zeros(PrimitiveWithInfer): return out -class SequenceMask(PrimitiveWithInfer): - r""" - Generates sequence mask according to input lengths. - - Creates a mask tensor which retains the first N elements in tensor by setting the values - to be True or one. The rest values in mask are set to False or zero. - - Args: - max_length (int): Nonnegative integer, size of the last dimension in mask. Default: None. - - Inputs: - - **lengths** (Union[tuple[int], list[int]]) - Defines the first N elements that are retained. - Only constant value is allowed. - - **dtype** (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed. - - Outputs: - Tensor. - If max_length is set, the shape of the output is (lengths.shape, max_length). - If max_length is not set and the biggest value in lengths is x. Then, the shape of - the output is (lengths.shape, x). - - Supported Platforms: - ``Ascend`` ``GPU`` ``CPU`` - - Examples: - >>> from mindspore.ops import operations as P - >>> sequence_mask = P.SequenceMask() - >>> mask = sequence_mask([2, 2, 4], mindspore.int32) - >>> print(mask) - [[1, 1, 0, 0], - [1, 1, 0, 0], - [1, 1, 1, 1]] - - """ - - @prim_attr_register - def __init__(self): - """Initialize SequenceMask""" - - def __infer__(self, lengths, dtype, max_length=None): - validator.check_value_type("shape", lengths['value'], [tuple, list], self.name) - valid_types = [mstype.bool_, mstype.int8, mstype.int16, mstype.int32, mstype.int64, - mstype.uint8, mstype.uint16, mstype.uint32, mstype.uint64, - mstype.float16, mstype.float32, mstype.float64] - validator.check_subclass("dtype", dtype['value'], valid_types, self.name) - nptype = mstype.dtype_to_nptype(dtype['value']) - if max_length is None: - max_length = np.max(lengths['value']) - else: - validator.check_non_negative_int(max_length['value']) - max_length = max_length['value'] - row_vector = np.arange(0, max_length) - col_matrix = np.expand_dims(lengths['value'], -1) - result = (row_vector < col_matrix).astype(nptype) - out = { - 'value': Tensor(result), - 'shape': result.shape, - 'dtype': dtype['value'] - } - return out - - class OnesLike(PrimitiveWithInfer): """ Creates a new tensor. The values of all elements are 1. diff --git a/tests/ut/python/ops/test_array_ops.py b/tests/ut/python/ops/test_array_ops.py index b0808828af..3992508265 100644 --- a/tests/ut/python/ops/test_array_ops.py +++ b/tests/ut/python/ops/test_array_ops.py @@ -42,28 +42,6 @@ def test_expand_dims(): assert output.asnumpy().shape == (1, 2, 2) -def test_sequence_mask(): - list_ = [2, 2, 4] - sequence_mask = P.SequenceMask() - mask1 = sequence_mask(list_, mstype.int32) - mask2 = sequence_mask(list_, mstype.int32, 5) - assert mask1.shape == (3, 4) - assert mask1.dtype == mstype.int32 - assert mask2.shape == (3, 5) - assert mask2.dtype == mstype.int32 - - -def test_sequence_mask_1(): - list_ = [[2, 2, 4], [3, 4, 4]] - sequence_mask = P.SequenceMask() - mask1 = sequence_mask(list_, mstype.bool_) - mask2 = sequence_mask(list_, mstype.bool_, 5) - assert mask1.shape == (2, 3, 4) - assert mask1.dtype == mstype.bool_ - assert mask2.shape == (2, 3, 5) - assert mask2.dtype == mstype.bool_ - - def test_cast(): input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) input_x = Tensor(input_np)