test_rmsprop_op.py 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
import unittest
import numpy as np
from op_test import OpTest


class TestRmspropOp(OpTest):
    def setUp(self):
        self.op_type = "rmsprop"

        param = np.random.random((123, 321)).astype("float32")
11 12
        mean_square = np.random.random((123, 321)).astype("float32")
        learning_rate = np.array([0.01]).astype("float32")
13 14 15 16
        grad = np.random.random((123, 321)).astype("float32")
        moment = np.zeros((123, 321)).astype("float32")

        epsilon = 1e-6
17 18
        decay = 0.9
        momentum = 0.0
19

K
Kavya Srinet 已提交
20 21
        self.inputs = {
            'Param': param,
22 23
            'MeanSquare': mean_square,
            'LearningRate': learning_rate,
K
Kavya Srinet 已提交
24 25
            'Grad': grad,
            'Moment': moment,
26 27
        }

28
        self.attrs = {'epsilon': epsilon, 'decay': decay, 'momentum': momentum}
K
Kavya Srinet 已提交
29

30 31 32 33
        ms_out = decay * mean_square + (1 - decay) * grad * grad
        moment_out = momentum * moment + \
            learning_rate * grad / np.sqrt(ms_out + epsilon)
        param_out = param - moment_out
34

35 36 37 38 39
        self.outputs = {
            'ParamOut': param_out,
            'MomentOut': moment_out,
            'MeanSquareOut': ms_out
        }
40 41 42 43 44 45 46

    def test_check_output(self):
        self.check_output()


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