test_trainable.py 2.7 KB
Newer Older
C
chengduo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

import unittest
16 17 18 19
from collections import Counter

from simple_nets import init_data

20
import paddle
21
from paddle import fluid
C
chengduo 已提交
22 23 24


def test_trainable():
G
GGBond8488 已提交
25 26
    x = paddle.static.data(name='image', shape=[-1, 784], dtype='float32')
    label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
C
Charles-hit 已提交
27 28
    feature = paddle.static.nn.fc(
        x, size=10, weight_attr=fluid.ParamAttr(trainable=False)
29
    )
30 31 32
    loss = paddle.nn.functional.cross_entropy(
        input=feature, label=label, reduction='none', use_softmax=False
    )
33
    loss = paddle.mean(loss)
C
chengduo 已提交
34 35 36 37
    return loss


class TestTrainable(unittest.TestCase):
38 39 40
    def check_trainable(
        self, model, feed_dict, op_count, optimizer=fluid.optimizer.Adam()
    ):
C
chengduo 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)

        main = fluid.Program()
        startup = fluid.Program()

        with fluid.program_guard(main, startup):
            loss = model()
            optimizer.minimize(loss)

            # The number of adam should be one.
            ops = Counter([op.type for op in main.global_block().ops])
            for op in op_count:
                if op_count[op] == 0:
                    assert op not in ops
                else:
                    assert ops[op] == op_count[op]

            exe.run(fluid.default_startup_program())
            exe.run(feed=feed_dict)

    def test_trainable(self):
        batch_size = 2
        img, label = init_data(batch_size, img_shape=[784], label_range=9)
        feed_dict = {'image': img, 'label': label}
        # Note that, because the Weight of FC is not trainable and the x is stop_gradient,
        # so the 'mul_grad' should not be appended.
        self.check_trainable(
            test_trainable,
            feed_dict,
71 72 73 74 75
            op_count={'adam': 1, 'scale': 0, 'mul_grad': 0},
        )
        self.check_trainable(
            test_trainable,
            feed_dict,
L
LoneRanger 已提交
76 77
            op_count={'adamax': 1, 'scale': 1, 'mul_grad': 1},
            optimizer=paddle.optimizer.Adamax(learning_rate=0.2),
78
        )
C
chengduo 已提交
79 80 81 82


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