diff --git a/mindspore/python/mindspore/ops/function/spectral_func.py b/mindspore/python/mindspore/ops/function/spectral_func.py index aac7b72edad..34acbd3ae76 100644 --- a/mindspore/python/mindspore/ops/function/spectral_func.py +++ b/mindspore/python/mindspore/ops/function/spectral_func.py @@ -60,7 +60,7 @@ def blackman_window(window_length, periodic=True, *, dtype=None): ValueError: If the dimension of `window_length` is not 0. Supported Platforms: - ``Ascend`` ``CPU`` ``GPU`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> window_length = Tensor(10, mindspore.int32) diff --git a/mindspore/python/mindspore/ops/operations/__init__.py b/mindspore/python/mindspore/ops/operations/__init__.py index e5c6afa5685..52e3b90b069 100644 --- a/mindspore/python/mindspore/ops/operations/__init__.py +++ b/mindspore/python/mindspore/ops/operations/__init__.py @@ -113,7 +113,7 @@ from .nn_ops import (LSTM, SGD, Adam, AdamWeightDecay, FusedSparseAdam, FusedSpa PSROIPooling, Dilation2D, DataFormatVecPermute, DeformableOffsets, FractionalAvgPool, FractionalMaxPool, FractionalMaxPool3DWithFixedKsize, FractionalMaxPoolWithFixedKsize, GridSampler2D, TripletMarginLoss, UpsampleNearest3D, UpsampleTrilinear3D) -from .other_ops import (Assign, IOU, BartlettWindow, BlackmanWindow, BoundingBoxDecode, BoundingBoxEncode, +from .other_ops import (Assign, IOU, BoundingBoxDecode, BoundingBoxEncode, ConfusionMatrix, UpdateState, Load, StopGradient, CheckValid, Partial, Depend, identity, Push, Pull, PyFunc, _DynamicLossScale, SampleDistortedBoundingBoxV2) @@ -123,6 +123,7 @@ from .random_ops import (RandomChoiceWithMask, StandardNormal, Gamma, RandomGamm ParameterizedTruncatedNormal, RandomPoisson) from .rl_ops import (BufferAppend, BufferGetItem, BufferSample) from .sparse_ops import (SparseToDense, SparseTensorDenseMatmul, SparseTensorDenseAdd) +from .spectral_ops import (BartlettWindow, BlackmanWindow) __all__ = [ 'HSVToRGB', diff --git a/mindspore/python/mindspore/ops/operations/nn_ops.py b/mindspore/python/mindspore/ops/operations/nn_ops.py index 14b55759d0c..714931958eb 100644 --- a/mindspore/python/mindspore/ops/operations/nn_ops.py +++ b/mindspore/python/mindspore/ops/operations/nn_ops.py @@ -7999,7 +7999,7 @@ class CTCLossV2(Primitive): """ @prim_attr_register - def __init__(self, blank, reduction="none", zero_infinity=False): + def __init__(self, blank=0, reduction="none", zero_infinity=False): """Initialize CTCLossV2""" self.init_prim_io_names(inputs=["log_probs", "targets", "input_lengths", "target_lengths"], outputs=["neg_log_likelihood", "log_alpha"]) diff --git a/mindspore/python/mindspore/ops/operations/other_ops.py b/mindspore/python/mindspore/ops/operations/other_ops.py index e9d191652db..2db4a1e959a 100644 --- a/mindspore/python/mindspore/ops/operations/other_ops.py +++ b/mindspore/python/mindspore/ops/operations/other_ops.py @@ -154,33 +154,6 @@ class BoundingBoxEncode(PrimitiveWithInfer): validator.check_equal_int(len(stds), 4, "stds len", self.name) -class BartlettWindow(Primitive): - r""" - Bartlett window function. - - Refer to :func:`mindspore.ops.bartlett_window` for more details. - - Supported Platforms: - ``GPU`` - - Examples: - >>> window_length = Tensor(5, mstype.int32) - >>> bartlett_window = ops.BartlettWindow(periodic=True, dtype=mstype.float32) - >>> output = bartlett_window(window_length) - >>> print(output) - [0. 0.4 0.8 0.8 0.4] - """ - - @prim_attr_register - def __init__(self, periodic=True, dtype=mstype.float32): - """Initialize BartlettWindow""" - self.add_prim_attr("max_length", 1000000) - validator.check_value_type("periodic", periodic, [bool], self.name) - validator.check_value_type("dtype", dtype, [mstype.Type], self.name) - valid_values = (mstype.float16, mstype.float32, mstype.float64) - validator.check_type_name("dtype", dtype, valid_values, self.name) - - class BoundingBoxDecode(Primitive): """ Decodes bounding boxes locations. @@ -828,64 +801,3 @@ class PyFunc(PrimitiveWithInfer): logger.warning("The function output are empty tuple. Add a placeholder instead. " "Do not use it as it could be any uninitialized data.") return (mstype.int32,) - - -class BlackmanWindow(Primitive): - r""" - Blackman window function. - - The input "window_length" is a tensor that datatype must be a integer, which - controlling the returned window size. In particular, If "window_length" =1, - the returned window contains a single value 1. - Attr "periodic" determines whether the returned window trims off the last duplicate value - from the symmetric window and is ready to be used as a periodic window with functions. - Therefore, if attr "periodic" is true, the "N" in formula is in fact "window_length" + 1. - - .. math:: - - w[n] = 0.42 - 0.5 cos(\frac{2\pi n}{N - 1}) + 0.08 cos(\frac{4\pi n}{N - 1}) - - \text{where : N is the full window size.} - - Args: - periodic (bool): If True, returns a window to be used as periodic function. - If False, return a symmetric window. Default: True. - dtype (mindspore.dtype): the desired data type of returned tensor. - Only float16, float32 and float64 is allowed. Default: mindspore.float32. - - Inputs: - - **window_length** (Tensor) - the size of returned window, with data type int32, int64. - The input data should be an integer with a value of [0, 1000000]. - - Outputs: - A 1-D tensor of size "window_length" containing the window. Its datatype is set by the attr 'dtype'. - - Raises: - TypeError: If "window_length" is not a Tensor. - TypeError: If "periodic" is not a bool. - TypeError: If "dtype" is not one of: float16, float32, float64. - TypeError: If the type of "window_length" is not one of: int32, int64. - ValueError: If the value range of "window_length" is not [0,1000000]. - ValueError: If the dimension of "window_length" is not 0. - - Supported Platforms: - ``Ascend`` ``CPU`` - - Examples: - >>> window_length = Tensor(10, mindspore.int32) - >>> blackman_window = ops.BlackmanWindow(periodic = True, dtype = mindspore.float32) - >>> output = blackman_window(window_length) - >>> print(output) - [-2.9802322e-08 4.0212840e-02 2.0077014e-01 5.0978714e-01 - 8.4922993e-01 1.0000000e+00 8.4922981e-01 5.0978690e-01 - 2.0077008e-01 4.0212870e-02] - """ - - @prim_attr_register - def __init__(self, periodic=True, dtype=mstype.float32): - """Initialize BlackmanWindow""" - self.add_prim_attr("max_length", 1000000) - validator.check_value_type("periodic", periodic, [bool], self.name) - validator.check_value_type("dtype", dtype, [mstype.Type], self.name) - valid_values = (mstype.float16, mstype.float32, mstype.float64) - validator.check_type_name("dtype", dtype, valid_values, self.name) diff --git a/mindspore/python/mindspore/ops/operations/spectral_ops.py b/mindspore/python/mindspore/ops/operations/spectral_ops.py new file mode 100644 index 00000000000..802af8601b5 --- /dev/null +++ b/mindspore/python/mindspore/ops/operations/spectral_ops.py @@ -0,0 +1,75 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""Spectral operators.""" +from mindspore._checkparam import Validator as validator +from mindspore.common import dtype as mstype +from mindspore.ops.primitive import Primitive, prim_attr_register + + +class BartlettWindow(Primitive): + r""" + Bartlett window function. + + Refer to :func:`mindspore.ops.bartlett_window` for more details. + + Supported Platforms: + ``GPU`` + + Examples: + >>> window_length = Tensor(5, mstype.int32) + >>> bartlett_window = ops.BartlettWindow(periodic=True, dtype=mstype.float32) + >>> output = bartlett_window(window_length) + >>> print(output) + [0. 0.4 0.8 0.8 0.4] + """ + + @prim_attr_register + def __init__(self, periodic=True, dtype=mstype.float32): + """Initialize BartlettWindow""" + self.add_prim_attr("max_length", 1000000) + validator.check_value_type("periodic", periodic, [bool], self.name) + validator.check_value_type("dtype", dtype, [mstype.Type], self.name) + valid_values = (mstype.float16, mstype.float32, mstype.float64) + validator.check_type_name("dtype", dtype, valid_values, self.name) + + +class BlackmanWindow(Primitive): + r""" + Blackman window function. + + Refer to :func:`mindspore.ops.blackman_window` for more details. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> window_length = Tensor(10, mindspore.int32) + >>> blackman_window = ops.BlackmanWindow(periodic = True, dtype = mindspore.float32) + >>> output = blackman_window(window_length) + >>> print(output) + [-2.9802322e-08 4.0212840e-02 2.0077014e-01 5.0978714e-01 + 8.4922993e-01 1.0000000e+00 8.4922981e-01 5.0978690e-01 + 2.0077008e-01 4.0212870e-02] + """ + + @prim_attr_register + def __init__(self, periodic=True, dtype=mstype.float32): + """Initialize BlackmanWindow""" + self.add_prim_attr("max_length", 1000000) + validator.check_value_type("periodic", periodic, [bool], self.name) + validator.check_value_type("dtype", dtype, [mstype.Type], self.name) + valid_values = (mstype.float16, mstype.float32, mstype.float64) + validator.check_type_name("dtype", dtype, valid_values, self.name) diff --git a/tests/st/ops/gpu/test_bartlett_window_op.py b/tests/st/ops/gpu/test_bartlett_window_op.py index fdbbd3bad30..ba1b3921d31 100644 --- a/tests/st/ops/gpu/test_bartlett_window_op.py +++ b/tests/st/ops/gpu/test_bartlett_window_op.py @@ -17,7 +17,7 @@ import torch import pytest import mindspore.context as context import mindspore.nn as nn -import mindspore.ops.operations.other_ops as P +import mindspore.ops.operations.spectral_ops as P from mindspore import Tensor from mindspore.common import dtype as mstype from mindspore.common.api import jit diff --git a/tests/st/ops/gpu/test_blackman_window_op.py b/tests/st/ops/gpu/test_blackman_window_op.py index 5e357a3e75f..979b6a9e126 100644 --- a/tests/st/ops/gpu/test_blackman_window_op.py +++ b/tests/st/ops/gpu/test_blackman_window_op.py @@ -18,7 +18,7 @@ import pytest import mindspore.context as context import mindspore.nn as nn from mindspore.ops import functional as F -import mindspore.ops.operations.other_ops as P +import mindspore.ops.operations.spectral_ops as P from mindspore import Tensor from mindspore.common import dtype as mstype from mindspore.common.api import jit diff --git a/tests/ut/python/ops/test_ops.py b/tests/ut/python/ops/test_ops.py index e4586e99f24..dda3cc795c8 100644 --- a/tests/ut/python/ops/test_ops.py +++ b/tests/ut/python/ops/test_ops.py @@ -140,7 +140,7 @@ from mindspore.nn.loss.loss import MultiMarginLoss from mindspore.nn.loss.loss import MultilabelMarginLoss from mindspore.nn.loss.loss import TripletMarginLoss from mindspore.ops.operations.array_ops import Mvlgamma -from mindspore.ops.operations.other_ops import BartlettWindow +from mindspore.ops.operations.spectral_ops import BartlettWindow from mindspore.ops.operations.nn_ops import SparseSoftmaxCrossEntropyWithLogitsV2 from mindspore.ops.operations.nn_ops import NthElement from mindspore.ops.operations.nn_ops import Pdist @@ -188,7 +188,7 @@ from mindspore.ops.operations.sparse_ops import SparseSegmentMeanWithNumSegments from mindspore.ops.operations.sparse_ops import SparseSlice from mindspore.ops.operations.sparse_ops import SparseFillEmptyRows from mindspore.ops.operations._grad_ops import SparseSliceGrad -from mindspore.ops.operations.other_ops import BlackmanWindow +from mindspore.ops.operations.spectral_ops import BlackmanWindow from mindspore.ops.operations.nn_ops import SparseApplyCenteredRMSProp from mindspore.ops.operations.nn_ops import SparseApplyProximalGradientDescent from mindspore.ops.operations.sparse_ops import SparseReshape