From 076f7f5339cec1639d2fc0e901290922c3ba1025 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Thu, 18 Nov 2021 10:41:16 +0000 Subject: [PATCH 1/4] shape api should not backward --- python/paddle/fluid/layers/nn.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index dd0abd212e834..50866cb0a63de 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11401,7 +11401,8 @@ def shape(input): 'complex128' ], 'shape') helper = LayerHelper('shape', **locals()) - out = helper.create_variable_for_type_inference(dtype='int32') + out = helper.create_variable_for_type_inference( + dtype='int32', stop_gradient=True) helper.append_op( type='shape', inputs={'Input': input}, outputs={'Out': out}) From 84b23f0d60702f23976ea2de2ebaa70179ac8012 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Thu, 18 Nov 2021 11:18:56 +0000 Subject: [PATCH 2/4] fix stop_gradient --- python/paddle/fluid/layers/nn.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 50866cb0a63de..482729e5a981d 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11401,10 +11401,12 @@ def shape(input): 'complex128' ], 'shape') helper = LayerHelper('shape', **locals()) - out = helper.create_variable_for_type_inference( - dtype='int32', stop_gradient=True) + out = helper.create_variable_for_type_inference(dtype='int32') helper.append_op( - type='shape', inputs={'Input': input}, outputs={'Out': out}) + type='shape', + inputs={'Input': input}, + outputs={'Out': out}, + stop_gradient=True) return out From 5ca2cc86f2b7bf4b00d018b2b87eefd0fcf77de9 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Fri, 19 Nov 2021 09:27:11 +0000 Subject: [PATCH 3/4] update --- python/paddle/fluid/layers/nn.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 482729e5a981d..5abf464a0e3d2 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11396,6 +11396,11 @@ def shape(input): res = exe.run(fluid.default_main_program(), feed={'x':img}, fetch_list=[output]) print(res) # [array([ 3, 100, 100], dtype=int32)] """ + if in_dygraph_mode(): + out = _C_ops.shape(input) + out.stop_gradient = True + return out + check_variable_and_dtype(input, 'input', [ 'bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'complex64', 'complex128' From be524b4f67aeb5d91963e4583eeb370d631260dd Mon Sep 17 00:00:00 2001 From: jiweibo Date: Sun, 21 Nov 2021 14:54:10 +0000 Subject: [PATCH 4/4] update doc --- python/paddle/fluid/layers/nn.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 5abf464a0e3d2..28d649947b57e 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11384,6 +11384,8 @@ def shape(input): import paddle.fluid as fluid import numpy as np + import paddle + paddle.enable_static() inputs = fluid.data(name="x", shape=[3, 100, 100], dtype="float32") output = fluid.layers.shape(inputs)