test_layers.py 6.4 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(
12 13
            name='x', shape=[13], data_type='float32', main_program=program)
        y_predict = layers.fc(input=x, size=1, act=None, main_program=program)
Y
Yu Yang 已提交
14

F
fengjiayi 已提交
15
        y = layers.data(
16
            name='y', shape=[1], data_type='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 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(
31 32 33 34
            name='pixel',
            shape=[784],
            data_type='float32',
            main_program=program)
F
fengjiayi 已提交
35
        label = layers.data(
36 37 38 39 40 41 42 43 44
            name='label', shape=[1], data_type='int32', main_program=program)
        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 已提交
45 46 47
        predict = layers.fc(input=hidden2,
                            size=10,
                            act='softmax',
48 49 50 51
                            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 已提交
52
        self.assertIsNotNone(avg_cost)
Q
Qiao Longfei 已提交
53
        # print str(program)
54 55

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

Q
Qiao Longfei 已提交
68
        # print str(program)
Y
Yu Yang 已提交
69

F
fengjiayi 已提交
70
    def test_recognize_digits_conv(self):
F
fengjiayi 已提交
71
        program = Program()
F
fengjiayi 已提交
72

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

        predict = layers.fc(input=conv_pool_2,
                            size=10,
                            act="softmax",
100 101 102 103
                            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 已提交
104 105

        program.append_backward(avg_cost)
106

Q
Qiao Longfei 已提交
107
        # print str(program)
108

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

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

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

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

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

Q
Qiao Longfei 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        # print str(program)

    def test_linear_chain_crf(self):
        program = Program()

        # Change g_program, so the rest layers use `g_program`
        images = layers.data(
            name='pixel',
            shape=[784],
            data_type='float32',
            main_program=program)
        label = layers.data(
            name='label', shape=[1], data_type='int32', main_program=program)
        hidden = layers.fc(input=images, size=128, main_program=program)
        crf = layers.linear_chain_crf(
            input=hidden, label=label, main_program=program)

        # print str(program)
Q
QI JUN 已提交
186

Y
Yu Yang 已提交
187 188 189

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