test_layers.py 6.6 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

68 69 70 71 72 73 74 75 76
    def test_conv2d_transpose(self):
        program = Program()
        kwargs = {'main_program': program}
        img = layers.data(
            name='pixel', shape=[3, 2, 2], dtype='float32', **kwargs)
        layers.conv2d_transpose(
            input=img, num_filters=10, output_size=28, **kwargs)
        print str(program)

F
fengjiayi 已提交
77
    def test_recognize_digits_conv(self):
F
fengjiayi 已提交
78
        program = Program()
F
fengjiayi 已提交
79

F
fengjiayi 已提交
80
        images = layers.data(
F
fengjiayi 已提交
81 82
            name='pixel',
            shape=[1, 28, 28],
F
fengjiayi 已提交
83
            dtype='float32',
84
            main_program=program)
F
fengjiayi 已提交
85
        label = layers.data(
F
fengjiayi 已提交
86
            name='label', shape=[1], dtype='int32', main_program=program)
F
fengjiayi 已提交
87 88 89 90 91 92 93
        conv_pool_1 = nets.simple_img_conv_pool(
            input=images,
            filter_size=5,
            num_filters=2,
            pool_size=2,
            pool_stride=2,
            act="relu",
94
            main_program=program)
F
fengjiayi 已提交
95 96 97 98 99 100 101
        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",
102
            main_program=program)
F
fengjiayi 已提交
103 104 105 106

        predict = layers.fc(input=conv_pool_2,
                            size=10,
                            act="softmax",
107 108 109 110
                            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 已提交
111 112

        program.append_backward(avg_cost)
113

Q
Qiao Longfei 已提交
114
        print str(program)
115

Q
QI JUN 已提交
116 117 118 119 120
    def test_word_embedding(self):
        program = Program()
        dict_size = 10000
        embed_size = 32
        first_word = layers.data(
F
fengjiayi 已提交
121
            name='firstw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
122
        second_word = layers.data(
F
fengjiayi 已提交
123
            name='secondw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
124
        third_word = layers.data(
F
fengjiayi 已提交
125
            name='thirdw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
126
        forth_word = layers.data(
F
fengjiayi 已提交
127
            name='forthw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
128
        next_word = layers.data(
F
fengjiayi 已提交
129
            name='nextw', shape=[1], dtype='int64', main_program=program)
Q
QI JUN 已提交
130 131 132 133

        embed_first = layers.embedding(
            input=first_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
134
            dtype='float32',
Y
Yu Yang 已提交
135
            param_attr={'name': 'shared_w'},
136
            main_program=program)
Q
QI JUN 已提交
137 138 139
        embed_second = layers.embedding(
            input=second_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
140
            dtype='float32',
Y
Yu Yang 已提交
141
            param_attr={'name': 'shared_w'},
142
            main_program=program)
Q
QI JUN 已提交
143 144 145 146

        embed_third = layers.embedding(
            input=third_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
147
            dtype='float32',
Y
Yu Yang 已提交
148
            param_attr={'name': 'shared_w'},
149
            main_program=program)
Q
QI JUN 已提交
150 151 152
        embed_forth = layers.embedding(
            input=forth_word,
            size=[dict_size, embed_size],
F
fengjiayi 已提交
153
            dtype='float32',
Y
Yu Yang 已提交
154
            param_attr={'name': 'shared_w'},
155
            main_program=program)
Q
QI JUN 已提交
156 157 158 159

        concat_embed = layers.concat(
            input=[embed_first, embed_second, embed_third, embed_forth],
            axis=1,
160
            main_program=program)
Q
QI JUN 已提交
161 162 163 164

        hidden1 = layers.fc(input=concat_embed,
                            size=256,
                            act='sigmoid',
165
                            main_program=program)
Q
QI JUN 已提交
166 167 168
        predict_word = layers.fc(input=hidden1,
                                 size=dict_size,
                                 act='softmax',
169
                                 main_program=program)
Q
QI JUN 已提交
170
        cost = layers.cross_entropy(
171 172
            input=predict_word, label=next_word, main_program=program)
        avg_cost = layers.mean(x=cost, main_program=program)
Q
QI JUN 已提交
173 174
        self.assertIsNotNone(avg_cost)

Q
Qiao Longfei 已提交
175
        print str(program)
Q
Qiao Longfei 已提交
176 177 178 179 180 181

    def test_linear_chain_crf(self):
        program = Program()

        # Change g_program, so the rest layers use `g_program`
        images = layers.data(
F
fengjiayi 已提交
182
            name='pixel', shape=[784], dtype='float32', main_program=program)
Q
Qiao Longfei 已提交
183
        label = layers.data(
F
fengjiayi 已提交
184
            name='label', shape=[1], dtype='int32', main_program=program)
Q
Qiao Longfei 已提交
185 186 187 188
        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 已提交
189
        print str(program)
Q
QI JUN 已提交
190

Y
Yu Yang 已提交
191 192 193

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