diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 5a68fe449ce54ff506c8833642dfe0599b168823..45cca7ad588cf86778fa26987d9a5a8388e32125 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -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(): diff --git a/python/paddle/fluid/tests/unittests/test_variable.py b/python/paddle/fluid/tests/unittests/test_variable.py index d72511ee0f09451f7b8ade57c71c85821c3bf3b9..79e73aa8ea5a1d96803c2e12cb62fe872a775250 100644 --- a/python/paddle/fluid/tests/unittests/test_variable.py +++ b/python/paddle/fluid/tests/unittests/test_variable.py @@ -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,