test_momentum_op.py 901 字节
Newer Older
S
sidgoyal78 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import unittest
import numpy as np
from op_test import OpTest


class TestMomentumOp(OpTest):
    def setUp(self):
        self.op_type = "momentum"

        param = np.random.random((123, 321)).astype("float32")
        grad = np.random.random((123, 321)).astype("float32")
        velocity = np.zeros((123, 321)).astype("float32")
        learning_rate = np.array([0.001]).astype("float32")
        mu = 0.0001

        self.inputs = {
            'Param': param,
            'Grad': grad,
            'Velocity': velocity,
            'LearningRate': learning_rate
        }

        self.attrs = {'mu': mu}

S
sidgoyal78 已提交
25 26
        velocity_out = mu * velocity + grad
        param_out = param - learning_rate * velocity_out
S
sidgoyal78 已提交
27 28 29 30 31 32 33 34 35

        self.outputs = {'ParamOut': param_out, 'VelocityOut': velocity_out}

    def test_check_output(self):
        self.check_output()


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