|
|
|
@ -12,12 +12,23 @@
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
|
import unittest
|
|
|
|
|
import sys
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
from paddle.fluid import core
|
|
|
|
|
from paddle.fluid.layers.nn import FC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def new_program_scope():
|
|
|
|
|
prog = fluid.Program()
|
|
|
|
|
startup_prog = fluid.Program()
|
|
|
|
|
scope = fluid.core.Scope()
|
|
|
|
|
with fluid.scope_guard(scope):
|
|
|
|
|
with fluid.program_guard(prog, startup_prog):
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyLayer(fluid.imperative.PyLayer):
|
|
|
|
@ -30,6 +41,23 @@ class MyLayer(fluid.imperative.PyLayer):
|
|
|
|
|
return [fluid.layers.elementwise_mul(x, x)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MLP(fluid.imperative.PyLayer):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(MLP, self).__init__()
|
|
|
|
|
self._fc1 = FC(3,
|
|
|
|
|
fluid.ParamAttr(
|
|
|
|
|
initializer=fluid.initializer.Constant(value=0.1)))
|
|
|
|
|
self._fc2 = FC(4,
|
|
|
|
|
fluid.ParamAttr(
|
|
|
|
|
initializer=fluid.initializer.Constant(value=0.1)))
|
|
|
|
|
|
|
|
|
|
def forward(self, inputs):
|
|
|
|
|
x = self._fc1(inputs[0])
|
|
|
|
|
x = self._fc2(x)
|
|
|
|
|
x = fluid.layers.reduce_sum(x)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestImperative(unittest.TestCase):
|
|
|
|
|
def test_layer(self):
|
|
|
|
|
with fluid.imperative.guard():
|
|
|
|
@ -39,13 +67,56 @@ class TestImperative(unittest.TestCase):
|
|
|
|
|
l.forward([])
|
|
|
|
|
|
|
|
|
|
def test_layer_in_out(self):
|
|
|
|
|
np_inp = np.array([1.0, 2.0, -1.0], dtype=np.float32)
|
|
|
|
|
with fluid.imperative.guard():
|
|
|
|
|
l = MyLayer()
|
|
|
|
|
x = l(np.array([1.0, 2.0, -1.0], dtype=np.float32))[0]
|
|
|
|
|
x = l(np_inp)[0]
|
|
|
|
|
self.assertIsNotNone(x)
|
|
|
|
|
sys.stderr.write("%s output: %s\n" % (x, x._numpy()))
|
|
|
|
|
dy_out = x._numpy()
|
|
|
|
|
x._backward()
|
|
|
|
|
sys.stderr.write("grad %s\n" % l._x_for_debug._gradient())
|
|
|
|
|
dy_grad = l._x_for_debug._gradient()
|
|
|
|
|
|
|
|
|
|
with new_program_scope():
|
|
|
|
|
inp = fluid.layers.data(
|
|
|
|
|
name="inp", shape=[3], append_batch_size=False)
|
|
|
|
|
l = MyLayer()
|
|
|
|
|
x = l(inp)[0]
|
|
|
|
|
param_grads = fluid.backward.append_backward(
|
|
|
|
|
x, parameter_list=[l._x_for_debug.name])[0]
|
|
|
|
|
exe = fluid.Executor(fluid.CPUPlace())
|
|
|
|
|
|
|
|
|
|
static_out, static_grad = exe.run(
|
|
|
|
|
feed={inp.name: np_inp},
|
|
|
|
|
fetch_list=[x.name, param_grads[1].name])
|
|
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(dy_out, static_out))
|
|
|
|
|
self.assertTrue(np.allclose(dy_grad, static_grad))
|
|
|
|
|
|
|
|
|
|
def test_mlp(self):
|
|
|
|
|
np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
|
|
|
|
|
with fluid.imperative.guard():
|
|
|
|
|
mlp = MLP()
|
|
|
|
|
out = mlp(np_inp)
|
|
|
|
|
dy_out = out._numpy()
|
|
|
|
|
out._backward()
|
|
|
|
|
dy_grad = mlp._fc1._w._gradient()
|
|
|
|
|
|
|
|
|
|
with new_program_scope():
|
|
|
|
|
inp = fluid.layers.data(
|
|
|
|
|
name="inp", shape=[2, 2], append_batch_size=False)
|
|
|
|
|
mlp = MLP()
|
|
|
|
|
out = mlp(inp)
|
|
|
|
|
param_grads = fluid.backward.append_backward(
|
|
|
|
|
out, parameter_list=[mlp._fc1._w.name])[0]
|
|
|
|
|
exe = fluid.Executor(fluid.CPUPlace())
|
|
|
|
|
exe.run(fluid.default_startup_program())
|
|
|
|
|
|
|
|
|
|
static_out, static_grad = exe.run(
|
|
|
|
|
feed={inp.name: np_inp},
|
|
|
|
|
fetch_list=[out.name, param_grads[1].name])
|
|
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(dy_out, static_out))
|
|
|
|
|
self.assertTrue(np.allclose(dy_grad, static_grad))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|