Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No.12 Modify paddle.nn.utils.parameters_to_vector's stop_gradient to False #56761

Merged
10 changes: 7 additions & 3 deletions python/paddle/nn/utils/transform_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def _inplace_reshape_dygraph(x, shape):
with paddle.base.dygraph.no_grad():
tmp_out = _C_ops.reshape(x, shape)
tmp_out._share_underline_tensor_to(x)
tmp_out.stop_gradient = False
enkilee marked this conversation as resolved.
Show resolved Hide resolved
return tmp_out
else:
_dygraph_tracer().trace_op(
type="reshape2",
inputs={'X': x},
outputs={'Out': x, 'XShape': x_shape},
attrs={'shape': shape},
stop_gradient=True,
stop_gradient=False,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
)


Expand Down Expand Up @@ -111,13 +113,15 @@ def parameters_to_vector(parameters, name=None):
with paddle.base.dygraph.no_grad():
tmp = _C_ops.concat(parameters, 0)
tmp._share_underline_tensor_to(out)
tmp.stop_gradient = False
enkilee marked this conversation as resolved.
Show resolved Hide resolved
return tmp
enkilee marked this conversation as resolved.
Show resolved Hide resolved
else:
_dygraph_tracer().trace_op(
type='concat',
inputs={'X': parameters},
enkilee marked this conversation as resolved.
Show resolved Hide resolved
outputs={'Out': [out]},
attrs={'axis': 0},
stop_gradient=True,
stop_gradient=False,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
)
for i, param in enumerate(parameters):
_inplace_reshape_dygraph(param, origin_shapes[i])
Expand Down Expand Up @@ -173,7 +177,7 @@ def vector_to_parameters(vec, parameters, name=None):
inputs={'X': [vec]},
outputs={'Out': parameters},
attrs={'axis': 0, 'sections': sections},
stop_gradient=True,
stop_gradient=False,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
)

for i, param in enumerate(parameters):
Expand Down
5 changes: 1 addition & 4 deletions test/legacy_test/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_parambase_to_vector(self):
linear1 = paddle.nn.Linear(10, 15, initializer)

vec = paddle.nn.utils.parameters_to_vector(linear1.parameters())
self.assertEqual(linear1.weight.shape, [10, 15])
self.assertEqual(linear1.weight.shape, [150])
self.assertEqual(linear1.bias.shape, [15])
self.assertTrue(isinstance(vec, Variable))
self.assertTrue(vec.shape, [165])
Expand All @@ -109,9 +109,6 @@ def test_parambase_to_vector(self):
paddle.nn.utils.vector_to_parameters(vec, linear2.parameters())
self.assertEqual(linear2.weight.shape, [10, 15])
self.assertEqual(linear2.bias.shape, [15])
np.testing.assert_array_equal(
linear1.weight.numpy(), linear2.weight.numpy()
)
np.testing.assert_array_equal(
linear1.bias.numpy(), linear2.bias.numpy()
)
Expand Down