test_adagrad_op.py 849 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
import unittest
import numpy as np
from op_test import OpTest


class TestAdagradOp(OpTest):
    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")

14
        lr = np.array([0.01]).astype("float32")
15 16 17 18 19 20 21
        epsilon = 1e-6

        self.inputs = {'param': param, 'grad': grad, 'moment': moment}

        self.attrs = {'learning_rate': learning_rate, 'epsilon': epsilon}

        moment_out = moment + grad * grad
22
        param_out = param - lr * grad / (np.sqrt(moment_out) + epsilon)
23 24 25 26 27 28 29 30 31

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

    def test_check_output(self):
        self.check_output()


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