Add grad_name Property for Class Variable (#20991)

custom_op_abi
Huihuang Zheng 6 years ago committed by GitHub
parent 1d1a07937a
commit 4cf96cd307
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1131,6 +1131,26 @@ class Variable(object):
else:
return cpt.to_text(self.desc.name())
@property
def grad_name(self):
"""
Indicating name of the gradient Variable of current Variable.
**Notes: This is a read-only property. It simply returns name of
gradient Variable from a naming convention but doesn't guarantee
the gradient exists.**
Examples:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.data(name="x", shape=[-1, 23, 48], dtype='float32')
print(x.grad_name) # output is "x@GRAD"
"""
return self.name + "@GRAD"
@name.setter
def name(self, new_name):
if in_dygraph_mode():

@ -44,12 +44,14 @@ class TestVariable(unittest.TestCase):
self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
self.assertEqual((784, 100), w.shape)
self.assertEqual("fc.w", w.name)
self.assertEqual("fc.w@GRAD", w.grad_name)
self.assertEqual(0, w.lod_level)
w = b.create_var(name='fc.w')
self.assertEqual(core.VarDesc.VarType.FP64, w.dtype)
self.assertEqual((784, 100), w.shape)
self.assertEqual("fc.w", w.name)
self.assertEqual("fc.w@GRAD", w.grad_name)
self.assertEqual(0, w.lod_level)
self.assertRaises(ValueError,

Loading…
Cancel
Save