test_adagrad_op.py 1.8 KB
Newer Older
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


K
Kexin Zhao 已提交
6 7 8 9
class TestAdagradOp1(OpTest):
    ''' Test Adagrad operator with explicit attributes
    '''

10 11 12 13 14 15
    def setUp(self):
        self.op_type = "adagrad"

        param = np.random.random((123, 321)).astype("float32")
        grad = np.random.random((123, 321)).astype("float32")
        moment = np.zeros((123, 321)).astype("float32")
K
Kexin Zhao 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
        lr = 0.01
        epsilon = 1e-8

        self.inputs = {
            'Param': param,
            'Grad': grad,
            'Moment': moment,
            'LearningRate': np.array([lr]).astype("float32")
        }

        self.attrs = {'epsilon': epsilon}

        moment_out = moment + grad * grad
        param_out = param - lr * grad / (np.sqrt(moment_out) + epsilon)

        self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out}

    def test_check_output(self):
        self.check_output()


class TestAdagradOp2(OpTest):
    ''' Test Adagrad operator with default attributes
    '''
40

K
Kexin Zhao 已提交
41 42 43 44 45 46 47
    def setUp(self):
        self.op_type = "adagrad"

        param = np.random.random((123, 321)).astype("float32")
        grad = np.random.random((123, 321)).astype("float32")
        moment = np.zeros((123, 321)).astype("float32")
        lr = 0.01
48 49
        epsilon = 1e-6

K
Kexin Zhao 已提交
50 51 52 53 54 55
        self.inputs = {
            'Param': param,
            'Grad': grad,
            'Moment': moment,
            'LearningRate': np.array([lr]).astype("float32")
        }
56

K
Kexin Zhao 已提交
57
        self.attrs = {'epsilon': epsilon}
58 59

        moment_out = moment + grad * grad
60
        param_out = param - lr * grad / (np.sqrt(moment_out) + epsilon)
61

K
Kexin Zhao 已提交
62
        self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out}
63 64 65 66 67 68 69

    def test_check_output(self):
        self.check_output()


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