fix Variable's gradient api in framework.py, test=develop (#21577)

* fix Variable's gradient api in framework.py, test=develop

* remove namescope, test=develop
release/1.7
zhongpu 5 years ago committed by liym27
parent 4b4a9cc88f
commit 3b84584eb4

@ -1090,6 +1090,7 @@ class Variable(object):
import paddle.fluid as fluid
import numpy as np
# example1: return ndarray
x = np.ones([2, 2], np.float32)
with fluid.dygraph.guard():
inputs2 = []
@ -1104,6 +1105,19 @@ class Variable(object):
loss2.backward(backward_strategy)
print(loss2.gradient())
# example2: return tuple of ndarray
with fluid.dygraph.guard():
embedding = fluid.dygraph.Embedding(
size=[20, 32],
param_attr='emb.w',
is_sparse=True)
x_data = np.arange(12).reshape(4, 3).astype('int64')
x_data = x_data.reshape((-1, 3, 1))
x = fluid.dygraph.base.to_variable(x_data)
out = embedding(x)
out.backward()
print(embedding.weight.gradient())
"""
pass

Loading…
Cancel
Save