test_rmsprop_op.py 6.0 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 16
from __future__ import print_function

17
import unittest
18

19
import numpy as np
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
import paddle.fluid.core as core
from paddle.fluid.op import Operator


class TestBase(unittest.TestCase):
    def setup(self, centered, epsilon=1e-6):
        np.random.seed(5)  # fix seed

        self.param_name = "param"
        self.param = np.random.random((123, 321)).astype("float32")

        self.mean_square_name = "mean_square"
        self.mean_square = np.random.random((123, 321)).astype("float32")

        self.mean_grad_name = "mean_grad"
        self.mean_grad = np.random.random((123, 321)).astype("float32")

        self.lr_name = "lr"
        self.learning_rate = np.array([0.01]).astype("float32")

        self.grad_name = "grad"
        self.grad = np.random.random((123, 321)).astype("float32")

        self.moment_name = "moment"
        self.moment = np.zeros((123, 321)).astype("float32")

        self.epsilon = epsilon
        self.decay = 0.9
        self.momentum = 0.0
        self.centered = centered

        self.ms_out = self.decay * self.mean_square + (1 - self.decay
                                                       ) * self.grad * self.grad
        if centered:
            self.mg_out = self.decay * self.mean_grad + (1 - self.decay
                                                         ) * self.grad
            self.moment_out = self.momentum * self.moment + \
                              self.learning_rate * self.grad / np.sqrt(self.ms_out - np.square(self.mg_out) + self.epsilon)
        else:
            self.moment_out = self.momentum * self.moment + \
                              self.learning_rate * self.grad / np.sqrt(self.ms_out + self.epsilon)

        self.param_out = self.param - self.moment_out

    def check(self,
              actual_t,
              expect_t,
              place,
              out_name,
              atol=1e-5,
              equal_nan=False):
        self.assertTrue(
            np.allclose(
                actual_t, expect_t, atol=atol, equal_nan=equal_nan),
            "Output (" + out_name + ") has diff at " + str(place) + "\nExpect "
            + str(expect_t) + "\n" + "But Got" + str(actual_t))


class TestRmspropOp(TestBase):
    def check_with_place(self, place, centered, epsilon):
        self.setup(centered, epsilon)
        scope = core.Scope()

        # create and initialize Param Variable
        param = scope.var(self.param_name).get_tensor()
        param.set(self.param, place)

        mean_square = scope.var(self.mean_square_name).get_tensor()
        mean_square.set(self.mean_square, place)

        lr = scope.var(self.lr_name).get_tensor()
        lr.set(self.learning_rate, place)

        grad = scope.var(self.grad_name).get_tensor()
        grad.set(self.grad, place)

        moment = scope.var(self.moment_name).get_tensor()
        moment.set(self.moment, place)

        # create and run sgd operator

        if self.centered:
            mean_grad = scope.var(self.mean_grad_name).get_tensor()
            mean_grad.set(self.mean_grad, place)

            rmsprop_op = Operator(
                "rmsprop",
                Param=self.param_name,
                Grad=self.grad_name,
                MeanSquare=self.mean_square_name,
                MeanGrad=self.mean_grad_name,
                Moment=self.moment_name,
                LearningRate=self.lr_name,
                ParamOut=self.param_name,
                MeanSquareOut=self.mean_square_name,
                MomentOut=self.moment_name,
                MeanGradOut=self.mean_grad_name,
                epsilon=self.epsilon,
                decay=self.decay,
                momentum=self.momentum,
                centered=True)
        else:
            rmsprop_op = Operator(
                "rmsprop",
                Param=self.param_name,
                Grad=self.grad_name,
                MeanSquare=self.mean_square_name,
                Moment=self.moment_name,
                LearningRate=self.lr_name,
                ParamOut=self.param_name,
                MeanSquareOut=self.mean_square_name,
                MomentOut=self.moment_name,
                epsilon=self.epsilon,
                decay=self.decay,
                momentum=self.momentum,
                centered=False)

        rmsprop_op.run(scope, place)

        atol = 1e-5
        equal_nan = False

        if self.centered:
            atol = 1e-3
            equal_nan = True

        self.check(
            np.array(mean_square), self.ms_out, place, self.mean_square_name)
        self.check(
            np.array(moment),
            self.moment_out,
            place,
            self.moment_name,
            atol=atol,
            equal_nan=equal_nan)
        self.check(
            np.array(param),
            self.param_out,
            place,
            self.param_name,
            atol=atol,
            equal_nan=equal_nan)

        if self.centered:
            self.check(
                np.array(mean_grad), self.mg_out, place, self.mean_grad_name)

    def test_rmsprop(self):
        places = [core.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(core.CUDAPlace(0))
        for place in places:
            self.check_with_place(place, False, 1e-6)
            self.check_with_place(place, False, 1e-10)
            self.check_with_place(place, True, 1e-6)
            self.check_with_place(place, True, 1e-10)
176 177


178 179
if __name__ == "__main__":
    unittest.main()