|
| 1 | +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | +import paddle.fluid as fluid |
| 20 | + |
| 21 | + |
| 22 | +class TestExponentialMovingAverage(unittest.TestCase): |
| 23 | + def setUp(self): |
| 24 | + self._places = [fluid.CPUPlace()] |
| 25 | + if fluid.core.is_compiled_with_cuda(): |
| 26 | + self._places.append(fluid.CUDAPlace(0)) |
| 27 | + self._ema_decay = 0.999 |
| 28 | + self._param_name = "fc.weight" |
| 29 | + |
| 30 | + self._train_program = fluid.Program() |
| 31 | + self._startup_prog = fluid.Program() |
| 32 | + with fluid.program_guard(self._train_program, self._startup_prog): |
| 33 | + with fluid.unique_name.guard(): |
| 34 | + data = fluid.data(name='x', shape=[-1, 5], dtype='float32') |
| 35 | + hidden = fluid.layers.fc(input=data, |
| 36 | + size=10, |
| 37 | + param_attr=self._param_name) |
| 38 | + cost = fluid.layers.mean(hidden) |
| 39 | + |
| 40 | + self._test_program = fluid.default_main_program().clone( |
| 41 | + for_test=True) |
| 42 | + |
| 43 | + optimizer = fluid.optimizer.Adam(learning_rate=0.001) |
| 44 | + optimizer.minimize(cost) |
| 45 | + |
| 46 | + self._ema = fluid.optimizer.ExponentialMovingAverage( |
| 47 | + self._ema_decay) |
| 48 | + self._ema.update() |
| 49 | + |
| 50 | + def train(self, place): |
| 51 | + exe = fluid.Executor(place) |
| 52 | + exe.run(self._startup_prog) |
| 53 | + |
| 54 | + params = [] |
| 55 | + for pass_id in range(2): |
| 56 | + for batch_id in range(3): |
| 57 | + data = np.random.random(size=(10, 5)).astype('float32') |
| 58 | + tmp_param = np.array(fluid.global_scope().find_var( |
| 59 | + self._param_name).get_tensor()) |
| 60 | + exe.run(program=self._train_program, feed={'x': data}) |
| 61 | + tmp_param = np.array(fluid.global_scope().find_var( |
| 62 | + self._param_name).get_tensor()) |
| 63 | + params.append(tmp_param) |
| 64 | + |
| 65 | + with self._ema.apply(exe): |
| 66 | + final_ema = np.array(fluid.global_scope().find_var(self._param_name) |
| 67 | + .get_tensor()) |
| 68 | + data = np.random.random(size=(10, 5)).astype('float32') |
| 69 | + exe.run(program=self._test_program, feed={'x': data}) |
| 70 | + return params, final_ema |
| 71 | + |
| 72 | + def test_check_ema(self): |
| 73 | + for place in self._places: |
| 74 | + params, final_ema = self.train(place) |
| 75 | + manu_ema = np.zeros_like(final_ema) |
| 76 | + if len(params) > 0: |
| 77 | + for param in params: |
| 78 | + manu_ema = self._ema_decay * manu_ema + (1 - self._ema_decay |
| 79 | + ) * param |
| 80 | + manu_ema = manu_ema / (1.0 - self._ema_decay**len(params)) |
| 81 | + self.assertTrue(np.allclose(manu_ema, final_ema)) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + unittest.main() |
0 commit comments