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

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(
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 54

        print str(program)
55 56

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

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

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

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

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

        program.append_backward(avg_cost)
107

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

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

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

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

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

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

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

    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)

Q
Qiao Longfei 已提交
186
        print str(program)
Q
QI JUN 已提交
187

Y
Yu Yang 已提交
188 189 190

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