test_layers.py 5.9 KB
Newer Older
F
fengjiayi 已提交
1
import paddle.v2.framework.layers as layers
F
fengjiayi 已提交
2
import paddle.v2.framework.nets as nets
3
from paddle.v2.framework.framework import Program, g_main_program
Y
Yu Yang 已提交
4 5 6 7 8 9
import paddle.v2.framework.core as core
import unittest


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)
Y
Yu Yang 已提交
23 24 25
        print str(program)

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

        # Change g_program, so the rest layers use `g_program`
F
fengjiayi 已提交
29
        images = layers.data(
30 31 32 33
            name='pixel',
            shape=[784],
            data_type='float32',
            main_program=program)
F
fengjiayi 已提交
34
        label = layers.data(
35 36 37 38 39 40 41 42 43
            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 已提交
44 45 46
        predict = layers.fc(input=hidden2,
                            size=10,
                            act='softmax',
47 48 49 50
                            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 已提交
51
        self.assertIsNotNone(avg_cost)
F
fengjiayi 已提交
52
        print str(program)
53 54

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

F
fengjiayi 已提交
67
        print str(program)
Y
Yu Yang 已提交
68

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

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

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

        program.append_backward(avg_cost)
105 106 107

        print str(program)

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

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

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

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

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

        print str(program)

Y
Yu Yang 已提交
169 170 171

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