Skip to content

Commit 2778fcd

Browse files
authored
fix shape api (#37412)
1 parent 0fa96e9 commit 2778fcd

File tree

1 file changed

+11
-1
lines changed
  • python/paddle/fluid/layers

1 file changed

+11
-1
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11384,6 +11384,8 @@ def shape(input):
1138411384

1138511385
import paddle.fluid as fluid
1138611386
import numpy as np
11387+
import paddle
11388+
paddle.enable_static()
1138711389

1138811390
inputs = fluid.data(name="x", shape=[3, 100, 100], dtype="float32")
1138911391
output = fluid.layers.shape(inputs)
@@ -11396,14 +11398,22 @@ def shape(input):
1139611398
res = exe.run(fluid.default_main_program(), feed={'x':img}, fetch_list=[output])
1139711399
print(res) # [array([ 3, 100, 100], dtype=int32)]
1139811400
"""
11401+
if in_dygraph_mode():
11402+
out = _C_ops.shape(input)
11403+
out.stop_gradient = True
11404+
return out
11405+
1139911406
check_variable_and_dtype(input, 'input', [
1140011407
'bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'complex64',
1140111408
'complex128'
1140211409
], 'shape')
1140311410
helper = LayerHelper('shape', **locals())
1140411411
out = helper.create_variable_for_type_inference(dtype='int32')
1140511412
helper.append_op(
11406-
type='shape', inputs={'Input': input}, outputs={'Out': out})
11413+
type='shape',
11414+
inputs={'Input': input},
11415+
outputs={'Out': out},
11416+
stop_gradient=True)
1140711417

1140811418
return out
1140911419

0 commit comments

Comments
 (0)