test_layers.py 6.2 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import unittest

Q
Qiao Longfei 已提交
3 4 5
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
from paddle.v2.fluid.framework import Program
Y
Yu Yang 已提交
6 7 8 9


class TestBook(unittest.TestCase):
    def test_fit_a_line(self):
10
        program = Program()
F
fengjiayi 已提交
11
        x = layers.data(
F
fengjiayi 已提交
12
            name='x', shape=[13], dtype='float32', main_program=program)
13
        y_predict = layers.fc(input=x, size=1, act=None, main_program=program)
Y
Yu Yang 已提交
14

F
fengjiayi 已提交
15
        y = layers.data(
F
fengjiayi 已提交
16
            name='y', shape=[1], dtype='float32', main_program=program)
F
fengjiayi 已提交
17
        cost = layers.square_error_cost(
18
            input=y_predict, label=y, main_program=program)
Y
Yu Yang 已提交
19

20
        avg_cost = layers.mean(x=cost, main_program=program)
Y
Yu Yang 已提交
21
        self.assertIsNotNone(avg_cost)
F
fengjiayi 已提交
22
        program.append_backward(avg_cost)
Q
Qiao Longfei 已提交
23

Q
Qiao Longfei 已提交
24
        print str(program)
Y
Yu Yang 已提交
25 26

    def test_recognize_digits_mlp(self):
27
        program = Program()
Y
Yu Yang 已提交
28 29

        # Change g_program, so the rest layers use `g_program`
F
fengjiayi 已提交
30
        images = layers.data(
F
fengjiayi 已提交
31
            name='pixel', shape=[784], dtype='float32', main_program=program)
F
fengjiayi 已提交
32
        label = layers.data(
F
fengjiayi 已提交
33
            name='label', shape=[1], dtype='int32', main_program=program)
34 35 36 37 38 39 40 41
        hidden1 = layers.fc(input=images,
                            size=128,
                            act='relu',
                            main_program=program)
        hidden2 = layers.fc(input=hidden1,
                            size=64,
                            act='relu',
                            main_program=program)
F
fengjiayi 已提交
42 43 44
        predict = layers.fc(input=hidden2,
                            size=10,
                            act='softmax',
45 46 47 48
                            main_program=program)
        cost = layers.cross_entropy(
            input=predict, label=label, main_program=program)
        avg_cost = layers.mean(x=cost, main_program=program)
Y
Yu Yang 已提交
49
        self.assertIsNotNone(avg_cost)
Q
Qiao Longfei 已提交
50 51

        print str(program)
52 53

    def test_simple_conv2d(self):
F
fengjiayi 已提交
54 55
        program = Program()
        images = layers.data(
56 57
            name='pixel',
            shape=[3, 48, 48],
F
fengjiayi 已提交
58
            dtype='int32',
59
            main_program=program)
F
fengjiayi 已提交
60
        layers.conv2d(
61 62 63 64
            input=images,
            num_filters=3,
            filter_size=[4, 4],
            main_program=program)
65

Q
Qiao Longfei 已提交
66
        print str(program)
Y
Yu Yang 已提交
67

F
fengjiayi 已提交
68
    def test_recognize_digits_conv(self):
F
fengjiayi 已提交
69
        program = Program()
F
fengjiayi 已提交
70

F
fengjiayi 已提交
71
        images = layers.data(
F
fengjiayi 已提交
72 73
            name='pixel',
            shape=[1, 28, 28],
F
fengjiayi 已提交
74
            dtype='float32',
75
            main_program=program)
F
fengjiayi 已提交
76
        label = layers.data(
F
fengjiayi 已提交
77
            name='label', shape=[1], dtype='int32', main_program=program)
F
fengjiayi 已提交
78 79 80 81 82 83 84
        conv_pool_1 = nets.simple_img_conv_pool(
            input=images,
            filter_size=5,
            num_filters=2,
            pool_size=2,
            pool_stride=2,
            act="relu",
85
            main_program=program)
F
fengjiayi 已提交
86 87 88 89 90 91 92
        conv_pool_2 = nets.simple_img_conv_pool(
            input=conv_pool_1,
            filter_size=5,
            num_filters=4,
            pool_size=2,
            pool_stride=2,
            act="relu",
93
            main_program=program)
F
fengjiayi 已提交
94 95 96 97

        predict = layers.fc(input=conv_pool_2,
                            size=10,
                            act="softmax",
98 99 100 101
                            main_program=program)
        cost = layers.cross_entropy(
            input=predict, label=label, main_program=program)
        avg_cost = layers.mean(x=cost, main_program=program)
F
fengjiayi 已提交
102 103

        program.append_backward(avg_cost)
104

Q
Qiao Longfei 已提交
105
        print str(program)
106

Q
QI JUN 已提交
107 108 109 110 111
    def test_word_embedding(self):
        program = Program()
        dict_size = 10000
        embed_size = 32
        first_word = layers.data(
F
fengjiayi 已提交
112
            name='firstw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
113
        second_word = layers.data(
F
fengjiayi 已提交
114
            name='secondw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
115
        third_word = layers.data(
F
fengjiayi 已提交
116
            name='thirdw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
117
        forth_word = layers.data(
F
fengjiayi 已提交
118
            name='forthw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
119
        next_word = layers.data(
F
fengjiayi 已提交
120
            name='nextw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
121 122 123 124

        embed_first = layers.embedding(
            input=first_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
125
            dtype='float32',
Y
Yu Yang 已提交
126
            param_attr={'name': 'shared_w'},
127
            main_program=program)
Q
QI JUN 已提交
128 129 130
        embed_second = layers.embedding(
            input=second_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
131
            dtype='float32',
Y
Yu Yang 已提交
132
            param_attr={'name': 'shared_w'},
133
            main_program=program)
Q
QI JUN 已提交
134 135 136 137

        embed_third = layers.embedding(
            input=third_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
138
            dtype='float32',
Y
Yu Yang 已提交
139
            param_attr={'name': 'shared_w'},
140
            main_program=program)
Q
QI JUN 已提交
141 142 143
        embed_forth = layers.embedding(
            input=forth_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
144
            dtype='float32',
Y
Yu Yang 已提交
145
            param_attr={'name': 'shared_w'},
146
            main_program=program)
Q
QI JUN 已提交
147 148 149 150

        concat_embed = layers.concat(
            input=[embed_first, embed_second, embed_third, embed_forth],
            axis=1,
151
            main_program=program)
Q
QI JUN 已提交
152 153 154 155

        hidden1 = layers.fc(input=concat_embed,
                            size=256,
                            act='sigmoid',
156
                            main_program=program)
Q
QI JUN 已提交
157 158 159
        predict_word = layers.fc(input=hidden1,
                                 size=dict_size,
                                 act='softmax',
160
                                 main_program=program)
Q
QI JUN 已提交
161
        cost = layers.cross_entropy(
162 163
            input=predict_word, label=next_word, main_program=program)
        avg_cost = layers.mean(x=cost, main_program=program)
Q
QI JUN 已提交
164 165
        self.assertIsNotNone(avg_cost)

Q
Qiao Longfei 已提交
166
        print str(program)
Q
Qiao Longfei 已提交
167 168 169 170 171 172

    def test_linear_chain_crf(self):
        program = Program()

        # Change g_program, so the rest layers use `g_program`
        images = layers.data(
F
fengjiayi 已提交
173
            name='pixel', shape=[784], dtype='float32', main_program=program)
Q
Qiao Longfei 已提交
174
        label = layers.data(
F
fengjiayi 已提交
175
            name='label', shape=[1], dtype='int32', main_program=program)
Q
Qiao Longfei 已提交
176 177 178 179
        hidden = layers.fc(input=images, size=128, main_program=program)
        crf = layers.linear_chain_crf(
            input=hidden, label=label, main_program=program)

Q
Qiao Longfei 已提交
180
        print str(program)
Q
QI JUN 已提交
181

Y
Yu Yang 已提交
182 183 184

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