From 9c6eb9b9a478ca96b6e235764fb656054e12553e Mon Sep 17 00:00:00 2001 From: l00591931 Date: Wed, 25 Nov 2020 11:45:23 +0800 Subject: [PATCH] Add backprop and add some comments --- mindspore/nn/layer/basic.py | 13 +++++++----- mindspore/ops/_grad/grad_array_ops.py | 30 +++++++++++++++++++++++++++ mindspore/ops/operations/array_ops.py | 13 +++++++----- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/mindspore/nn/layer/basic.py b/mindspore/nn/layer/basic.py index 696cf58153..862de34688 100644 --- a/mindspore/nn/layer/basic.py +++ b/mindspore/nn/layer/basic.py @@ -599,7 +599,7 @@ class Interpolate(Cell): 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 float32 or float64. + math:'(batch, channels, height, width)', with data type of float32 or float64. Outputs: Resized tensor. @@ -609,7 +609,7 @@ class Interpolate(Cell): scale_factor * width)' in float32 Supported Platforms: - ``Ascend`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> from mindspore.ops import operations as P @@ -703,7 +703,7 @@ class Tril(Cell): Tensor, has the same type as input `x`. Supported Platforms: - ``Ascend`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[1, 2], [3, 4]])) @@ -742,10 +742,13 @@ class Triu(Cell): Outputs: Tensor, has the same type as input `x`. + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + Examples: >>> x = Tensor(np.array([[1, 2], [3, 4]])) - >>> tril = nn.Tril() - >>> result = tril(x) + >>> triu = nn.Triu() + >>> result = triu(x) >>> print(result) [[1 2] [0 4]] diff --git a/mindspore/ops/_grad/grad_array_ops.py b/mindspore/ops/_grad/grad_array_ops.py index b7b64f5187..6320f05af1 100644 --- a/mindspore/ops/_grad/grad_array_ops.py +++ b/mindspore/ops/_grad/grad_array_ops.py @@ -51,6 +51,36 @@ def get_bprop_fill(self): return bprop +@bprop_getters.register(P.Ones) +def get_bprop_ones(self): + """Generate bprop for Ones""" + + def bprop(dims, dtype, out, dout): + return zeros_like(dims) + + return bprop + + +@bprop_getters.register(P.Zeros) +def get_bprop_zeros(self): + """Generate bprop for Zeros""" + + def bprop(dims, dtype, out, dout): + return zeros_like(dims) + + 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/array_ops.py b/mindspore/ops/operations/array_ops.py index 101685855b..9d6f778380 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -1129,14 +1129,14 @@ class Ones(PrimitiveWithInfer): Inputs: - **shape** (Union[tuple[int], int]) - The specified shape of output tensor. - Only constant positive int is allowed. + Only constant positive int is allowed. - **type** (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed. Outputs: Tensor, has the same type and shape as input shape value. Supported Platforms: - ``Ascend`` ``GPU`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> from mindspore.ops import operations as ops @@ -1182,14 +1182,14 @@ class Zeros(PrimitiveWithInfer): Inputs: - **shape** (Union[tuple[int], int]) - The specified shape of output tensor. - Only constant positive int is allowed. + Only constant positive int is allowed. - **type** (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed. Outputs: Tensor, has the same type and shape as input shape value. Supported Platforms: - ``Ascend`` ``GPU`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> from mindspore.ops import operations as ops @@ -1239,7 +1239,7 @@ class SequenceMask(PrimitiveWithInfer): Inputs: - **lengths** (Union[tuple[int], list[int]]) - Defines the first N elements that are retained. - Only constant value is allowed. + Only constant value is allowed. - **dtype** (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed. Outputs: @@ -1248,6 +1248,9 @@ class SequenceMask(PrimitiveWithInfer): 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()