未验证 提交 4cf96cd3 编写于 作者: H Huihuang Zheng 提交者: GitHub

Add grad_name Property for Class Variable (#20991)

上级 1d1a0793
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册