From 3b84584eb4d793c1119efdbd52aa4cda09075319 Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Tue, 7 Jan 2020 15:02:11 +0800 Subject: [PATCH] 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 --- python/paddle/fluid/framework.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index e365a584e08..907ac3209c1 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -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 -- GitLab