From cc1a9f4238a4b0ed475de4adb5955772ceaf63cf Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Wed, 8 Jan 2020 11:13:53 +0800 Subject: [PATCH] fix sample code in paddle/fluid/imperative/README.md (#22141) * fix sample code, test=develop * polish code style, test=develop --- paddle/fluid/imperative/README.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/imperative/README.md b/paddle/fluid/imperative/README.md index 4c4d619b35a..6db74394f3e 100644 --- a/paddle/fluid/imperative/README.md +++ b/paddle/fluid/imperative/README.md @@ -169,30 +169,32 @@ with fluid.imperative.guard(): dy_grad = var_inp._gradient() -class MLP(fluid.imperative.Layer): - def __init__(self): +class MLP(fluid.Layer): + def __init__(self, input_size): super(MLP, self).__init__() - self._fc1 = FC(3, + self._linear1 = Linear(input_size, + 3, fluid.ParamAttr( initializer=fluid.initializer.Constant(value=0.1))) - self._fc2 = FC(4, + self._linear2 = Linear(3, + 4, fluid.ParamAttr( initializer=fluid.initializer.Constant(value=0.1))) def forward(self, inputs): - x = self._fc1(inputs) - x = self._fc2(x) + x = self._linear1(inputs) + x = self._linear2(x) x = fluid.layers.reduce_sum(x) return x np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) - with fluid.imperative.guard(): - var_inp = fluid.imperative.base.to_variable(np_inp) - mlp = MLP() + with fluid.dygraph.guard(): + var_inp = fluid.dygraph.base.to_variable(np_inp) + mlp = MLP(input_size=2) out = mlp(var_inp) - dy_out = out._numpy() - out._backward() + dy_out = out.numpy() + out.backward() ``` # Plan -- GitLab