|
| 1 | +# Copyright PaddlePaddle contributors. 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 | +import unittest |
| 15 | +import math |
| 16 | +import paddle.v2 as paddle |
| 17 | + |
| 18 | + |
| 19 | +def wordemb(inlayer): |
| 20 | + wordemb = paddle.layer.table_projection( |
| 21 | + input=inlayer, |
| 22 | + size=5, |
| 23 | + param_attr=paddle.attr.Param( |
| 24 | + name="_proj", initial_std=0.001, learning_rate=1, l2_rate=0)) |
| 25 | + return wordemb |
| 26 | + |
| 27 | + |
| 28 | +def train(): |
| 29 | + word_dict = paddle.dataset.imikolov.build_dict() |
| 30 | + dict_size = len(word_dict) |
| 31 | + # Every layer takes integer value of range [0, dict_size) |
| 32 | + firstword = paddle.layer.data( |
| 33 | + name="firstw", type=paddle.data_type.integer_value(dict_size)) |
| 34 | + secondword = paddle.layer.data( |
| 35 | + name="secondw", type=paddle.data_type.integer_value(dict_size)) |
| 36 | + thirdword = paddle.layer.data( |
| 37 | + name="thirdw", type=paddle.data_type.integer_value(dict_size)) |
| 38 | + fourthword = paddle.layer.data( |
| 39 | + name="fourthw", type=paddle.data_type.integer_value(dict_size)) |
| 40 | + nextword = paddle.layer.data( |
| 41 | + name="fifthw", type=paddle.data_type.integer_value(dict_size)) |
| 42 | + |
| 43 | + Efirst = wordemb(firstword) |
| 44 | + Esecond = wordemb(secondword) |
| 45 | + Ethird = wordemb(thirdword) |
| 46 | + Efourth = wordemb(fourthword) |
| 47 | + |
| 48 | + contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth]) |
| 49 | + hidden1 = paddle.layer.fc(name="fc1", |
| 50 | + input=contextemb, |
| 51 | + size=128, |
| 52 | + act=paddle.activation.Sigmoid(), |
| 53 | + layer_attr=paddle.attr.Extra(drop_rate=0.5), |
| 54 | + bias_attr=paddle.attr.Param(learning_rate=2), |
| 55 | + param_attr=paddle.attr.Param( |
| 56 | + initial_std=1. / math.sqrt(5 * 8), |
| 57 | + learning_rate=1, |
| 58 | + l2_rate=6e-4)) |
| 59 | + predictword = paddle.layer.fc(input=hidden1, |
| 60 | + size=dict_size, |
| 61 | + bias_attr=paddle.attr.Param(learning_rate=2), |
| 62 | + act=paddle.activation.Softmax()) |
| 63 | + |
| 64 | + return paddle.layer.classification_cost(input=predictword, label=nextword) |
| 65 | + |
| 66 | + |
| 67 | +class TestParamConfOrder(unittest.TestCase): |
| 68 | + def test_param_conf_order(self): |
| 69 | + paddle.init() |
| 70 | + cost = train() |
| 71 | + parameters = paddle.parameters.create(cost) |
| 72 | + adagrad = paddle.optimizer.AdaGrad( |
| 73 | + learning_rate=3e-3, |
| 74 | + regularization=paddle.optimizer.L2Regularization(rate=8e-4)) |
| 75 | + |
| 76 | + trainer = paddle.trainer.SGD(cost, parameters, adagrad) |
| 77 | + for p in trainer.get_topology_proto().parameters: |
| 78 | + if p.name == "_fc1.w0": |
| 79 | + self.assertEqual(p.decay_rate, 6e-4) |
| 80 | + else: |
| 81 | + self.assertEqual(p.decay_rate, 8e-4) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + unittest.main() |
0 commit comments