test_calc_gradient.py 6.4 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import unittest
16

17
import numpy as np
18 19

import paddle
20 21 22
import paddle.fluid as fluid
import paddle.fluid.layers as layers
from paddle.fluid.backward import calc_gradient
23 24 25 26


class TestCalcGradient(unittest.TestCase):
    def test_calc_gradient(self):
27 28 29
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(main, startup):
30 31
            x = paddle.create_parameter(dtype="float32", shape=[5, 10])
            y = paddle.create_parameter(dtype="float32", shape=[10, 8])
32
            mul_out = layers.mul(x=x, y=y)
33
            mean_out = paddle.mean(mul_out)
34 35
            a = calc_gradient(mean_out, mul_out)
            b = calc_gradient(mean_out, x)
36 37
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
38 39 40 41 42 43 44 45 46 47
        exe.run(startup)
        exe.run(main, feed={}, fetch_list=[a, b])


class TestDoubleGrad(unittest.TestCase):
    def test1(self):
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(main, startup):
            net = lambda x: x * x
48
            x = paddle.create_parameter(
49 50 51
                name='x',
                shape=[1],
                dtype='float32',
52 53 54
                default_initializer=fluid.initializer.Constant(3),
            )
            (grad1,) = fluid.gradients(net(x), x)  # 2x = 6
55
            z = net(x - grad1)
56
            (grad2,) = fluid.gradients(z, x)  # gradients( (x - 2x)^2) = 2x = 6
57 58 59 60 61 62 63 64 65 66 67 68

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(startup)
        out = exe.run(main, fetch_list=[grad1.name, grad2.name])
        self.assertEqual(6, out[0][0])
        self.assertEqual(6, out[1][0])

    def test2(self):
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(main, startup):
69
            x = paddle.create_parameter(
70 71 72
                name='x',
                shape=[1],
                dtype='float32',
73 74
                default_initializer=fluid.initializer.Constant(1),
            )
75
            y = x * x
76
            (dx1,) = fluid.gradients(y, x)
77
            z = dx1 * dx1 + y * y
78
            (dx2,) = fluid.gradients(z, x)
79 80 81 82

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(startup)
83
        (out,) = exe.run(main, fetch_list=[dx2])
84
        self.assertEqual(12, out[0])
85 86


87 88
class TestGradientWithPrune(unittest.TestCase):
    def test_prune(self):
X
xiongkun 已提交
89 90 91 92 93 94
        with paddle.fluid.scope_guard(paddle.static.Scope()):
            x = fluid.data(name='x', shape=[3], dtype='float32')
            x.stop_gradient = False
            x1, x2, x3 = fluid.layers.split(x, dim=0, num_or_sections=3)
            y = x1 * 2
            x1_grad = fluid.gradients(y, x)
95

X
xiongkun 已提交
96 97 98
            exe = fluid.Executor(fluid.CPUPlace())
            main = fluid.default_main_program()
            exe.run(fluid.default_startup_program())
99 100 101 102 103
            out = exe.run(
                main,
                feed={'x': np.ones([3]).astype('float32')},
                fetch_list=[x1_grad],
            )
104
            np.testing.assert_array_equal(out[0], [2.0, 0.0, 0.0])
105 106


107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
class TestDoubleGradient(unittest.TestCase):
    def build_program(self):
        start_prog = paddle.static.Program()
        main_prog = paddle.static.Program()

        with paddle.static.program_guard(main_prog, start_prog):
            x = paddle.static.data('x', shape=[2, 2])
            x.stop_gradient = False
            y = x * x

            v = paddle.ones([2, 2])
            v.stop_gradient = False

            grad_y = paddle.zeros_like(y)
            grad_y.stop_gradient = False
            grad_x = paddle.static.gradients(y, x, grad_y)
            # test with single targets
            jvp = paddle.static.gradients(grad_x, grad_y, v)

        return start_prog, main_prog, [grad_x, jvp]

    def test_calc_gradient(self):
129 130 131 132
        with paddle.fluid.scope_guard(paddle.static.Scope()):
            start_prog, main_prog, fetch_list = self.build_program()
            exe = paddle.static.Executor()
            exe.run(start_prog)
133 134 135 136 137
            ans = exe.run(
                main_prog,
                feed={'x': np.ones([2, 2]).astype(np.float32)},
                fetch_list=fetch_list,
            )
138
            self.assertEqual(len(ans), 2)
139 140
            self.assertListEqual(ans[0].tolist(), [[0.0, 0.0], [0.0, 0.0]])
            self.assertListEqual(ans[1].tolist(), [[2.0, 2.0], [2.0, 2.0]])
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161


class TestDoubleGradient2(unittest.TestCase):
    def build_program(self):
        start_prog = paddle.static.Program()
        main_prog = paddle.static.Program()

        with paddle.static.program_guard(main_prog, start_prog):
            x = paddle.static.data('x', shape=[2, 2])
            x.stop_gradient = False
            y = x * x
            y2 = y + x

            v = paddle.ones([2, 2])
            v.stop_gradient = False

            grad_y = paddle.zeros_like(y)
            grad_y.stop_gradient = False
            grad_x = paddle.static.gradients(y, x, grad_y)
            grad_x2 = paddle.static.gradients(y2, x, grad_y)
            # test with multi targets
162 163 164
            jvp = paddle.static.gradients(
                [grad_x[0], grad_x2[0]], grad_y, [v, v]
            )
165 166 167 168

        return start_prog, main_prog, [grad_x, jvp]

    def test_calc_gradient(self):
169 170 171 172
        with paddle.fluid.scope_guard(paddle.static.Scope()):
            start_prog, main_prog, fetch_list = self.build_program()
            exe = paddle.static.Executor()
            exe.run(start_prog)
173 174 175 176 177
            ans = exe.run(
                main_prog,
                feed={'x': np.ones([2, 2]).astype(np.float32)},
                fetch_list=fetch_list,
            )
178
            self.assertEqual(len(ans), 2)
179 180
            self.assertListEqual(ans[0].tolist(), [[0.0, 0.0], [0.0, 0.0]])
            self.assertListEqual(ans[1].tolist(), [[5.0, 5.0], [5.0, 5.0]])
181 182


183 184
if __name__ == "__main__":
    unittest.main()