You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.7 KiB
91 lines
2.7 KiB
7 years ago
|
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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
|
||
7 years ago
|
#
|
||
7 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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.
|
||
|
|
||
8 years ago
|
import unittest
|
||
|
import numpy as np
|
||
|
from op_test import OpTest
|
||
|
|
||
|
|
||
7 years ago
|
class TestMomentumOp1(OpTest):
|
||
8 years ago
|
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
|
||
7 years ago
|
use_nesterov = False
|
||
8 years ago
|
|
||
|
self.inputs = {
|
||
|
'Param': param,
|
||
|
'Grad': grad,
|
||
|
'Velocity': velocity,
|
||
|
'LearningRate': learning_rate
|
||
|
}
|
||
|
|
||
|
self.attrs = {'mu': mu}
|
||
|
|
||
7 years ago
|
velocity_out = mu * velocity + grad
|
||
7 years ago
|
if use_nesterov:
|
||
|
param_out = param - grad * learning_rate + \
|
||
|
velocity_out * mu * learning_rate
|
||
|
else:
|
||
|
param_out = param - learning_rate * velocity_out
|
||
|
|
||
|
self.outputs = {'ParamOut': param_out, 'VelocityOut': velocity_out}
|
||
|
|
||
|
def test_check_output(self):
|
||
|
self.check_output()
|
||
|
|
||
|
|
||
|
class TestMomentumOp2(OpTest):
|
||
7 years ago
|
'''Test Momentum with default values for attributes
|
||
7 years ago
|
'''
|
||
|
|
||
|
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
|
||
|
use_nesterov = True
|
||
|
|
||
|
self.inputs = {
|
||
|
'Param': param,
|
||
|
'Grad': grad,
|
||
|
'Velocity': velocity,
|
||
|
'LearningRate': learning_rate
|
||
|
}
|
||
|
|
||
7 years ago
|
self.attrs = {'mu': mu, 'use_nesterov': use_nesterov}
|
||
7 years ago
|
|
||
|
velocity_out = mu * velocity + grad
|
||
|
if use_nesterov:
|
||
|
param_out = param - grad * learning_rate + \
|
||
|
velocity_out * mu * learning_rate
|
||
|
else:
|
||
|
param_out = param - learning_rate * velocity_out
|
||
8 years ago
|
|
||
|
self.outputs = {'ParamOut': param_out, 'VelocityOut': velocity_out}
|
||
|
|
||
|
def test_check_output(self):
|
||
|
self.check_output()
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
unittest.main()
|