test_layer.py 11.0 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Q
qiaolongfei 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
D
dzhwinter 已提交
14

Q
qiaolongfei 已提交
15 16 17 18 19 20
import unittest

import paddle.v2.activation as activation
import paddle.v2.attr as attr
import paddle.v2.data_type as data_type
import paddle.v2.layer as layer
L
Luo Tao 已提交
21
import paddle.v2.pooling as pooling
Y
Yu Yang 已提交
22
import paddle.v2.networks as networks
Y
Yu Yang 已提交
23
import paddle.v2.evaluator as evaluator
Q
qiaolongfei 已提交
24

L
Luo Tao 已提交
25
pixel = layer.data(name='pixel', type=data_type.dense_vector(128))
Q
qiaolongfei 已提交
26
label = layer.data(name='label', type=data_type.integer_value(10))
27
weight = layer.data(name='weight', type=data_type.dense_vector(1))
Y
Yu Yang 已提交
28 29
combine_weight = layer.data(
    name='weight_combine', type=data_type.dense_vector(10))
Q
qiaolongfei 已提交
30
score = layer.data(name='score', type=data_type.dense_vector(1))
L
Luo Tao 已提交
31

Q
qiaolongfei 已提交
32 33 34 35 36
hidden = layer.fc(input=pixel,
                  size=100,
                  act=activation.Sigmoid(),
                  param_attr=attr.Param(name='hidden'))
inference = layer.fc(input=hidden, size=10, act=activation.Softmax())
L
Luo Tao 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
conv = layer.img_conv(
    input=pixel,
    filter_size=1,
    filter_size_y=1,
    num_channels=8,
    num_filters=16,
    act=activation.Linear())


class ImageLayerTest(unittest.TestCase):
    def test_conv_layer(self):
        conv_shift = layer.conv_shift(a=pixel, b=score)
        print layer.parse_network(conv, conv_shift)

    def test_pooling_layer(self):
        maxpool = layer.img_pool(
            input=conv,
            pool_size=2,
            num_channels=16,
            padding=1,
            pool_type=pooling.Max())
        spp = layer.spp(input=conv,
                        pyramid_height=2,
                        num_channels=16,
                        pool_type=pooling.Max())
        maxout = layer.maxout(input=conv, num_channels=16, groups=4)
D
dangqingqing 已提交
63
        print layer.parse_network([maxpool, spp, maxout])
L
Luo Tao 已提交
64 65 66 67 68

    def test_norm_layer(self):
        norm1 = layer.img_cmrnorm(input=conv, size=5)
        norm2 = layer.batch_norm(input=conv)
        norm3 = layer.sum_to_one_norm(input=conv)
D
dangqingqing 已提交
69
        print layer.parse_network([norm1, norm2, norm3])
L
Luo Tao 已提交
70 71 72 73


class AggregateLayerTest(unittest.TestCase):
    def test_aggregate_layer(self):
Y
Yu Yang 已提交
74
        pool = layer.pooling(
L
Luo Tao 已提交
75 76
            input=pixel,
            pooling_type=pooling.Avg(),
L
Luo Tao 已提交
77
            agg_level=layer.AggregateLevel.TO_SEQUENCE)
L
Luo Tao 已提交
78 79 80 81
        last_seq = layer.last_seq(input=pixel)
        first_seq = layer.first_seq(input=pixel)
        concat = layer.concat(input=[last_seq, first_seq])
        seq_concat = layer.seq_concat(a=last_seq, b=first_seq)
D
dangqingqing 已提交
82 83
        print layer.parse_network(
            [pool, last_seq, first_seq, concat, seq_concat])
L
Luo Tao 已提交
84 85 86 87 88


class MathLayerTest(unittest.TestCase):
    def test_math_layer(self):
        addto = layer.addto(input=[pixel, pixel])
Y
Yu Yang 已提交
89 90
        linear_comb = layer.linear_comb(
            weights=combine_weight, vectors=hidden, size=10)
L
Luo Tao 已提交
91 92 93 94 95 96 97 98 99
        interpolation = layer.interpolation(
            input=[hidden, hidden], weight=score)
        bilinear = layer.bilinear_interp(input=conv, out_size_x=4, out_size_y=4)
        power = layer.power(input=pixel, weight=score)
        scaling = layer.scaling(input=pixel, weight=score)
        slope = layer.slope_intercept(input=pixel)
        tensor = layer.tensor(a=pixel, b=pixel, size=1000)
        cos_sim = layer.cos_sim(a=pixel, b=pixel)
        trans = layer.trans(input=tensor)
D
dangqingqing 已提交
100 101 102 103
        print layer.parse_network([
            addto, linear_comb, interpolation, power, scaling, slope, tensor,
            cos_sim, trans
        ])
L
Luo Tao 已提交
104 105 106 107 108 109 110 111 112


class ReshapeLayerTest(unittest.TestCase):
    def test_reshape_layer(self):
        block_expand = layer.block_expand(
            input=conv, num_channels=4, stride_x=1, block_x=1)
        expand = layer.expand(
            input=weight,
            expand_as=pixel,
L
Luo Tao 已提交
113
            expand_level=layer.ExpandLevel.FROM_NO_SEQUENCE)
L
Luo Tao 已提交
114 115 116
        repeat = layer.repeat(input=pixel, num_repeats=4)
        reshape = layer.seq_reshape(input=pixel, reshape_size=4)
        rotate = layer.rotate(input=pixel, height=16, width=49)
D
dangqingqing 已提交
117 118
        print layer.parse_network(
            [block_expand, expand, repeat, reshape, rotate])
L
Luo Tao 已提交
119 120 121 122 123 124 125 126


class RecurrentLayerTest(unittest.TestCase):
    def test_recurrent_layer(self):
        word = layer.data(name='word', type=data_type.integer_value(12))
        recurrent = layer.recurrent(input=word)
        lstm = layer.lstmemory(input=word)
        gru = layer.grumemory(input=word)
D
dangqingqing 已提交
127
        print layer.parse_network([recurrent, lstm, gru])
Q
qiaolongfei 已提交
128 129 130 131 132 133 134 135 136 137


class CostLayerTest(unittest.TestCase):
    def test_cost_layer(self):
        cost1 = layer.classification_cost(input=inference, label=label)
        cost2 = layer.classification_cost(
            input=inference, label=label, weight=weight)
        cost3 = layer.cross_entropy_cost(input=inference, label=label)
        cost4 = layer.cross_entropy_with_selfnorm_cost(
            input=inference, label=label)
C
caoying03 已提交
138 139 140
        cost5 = layer.square_error_cost(input=inference, label=label)
        cost6 = layer.square_error_cost(
            input=inference, label=label, weight=weight)
Q
qiaolongfei 已提交
141 142 143 144 145
        cost7 = layer.multi_binary_label_cross_entropy_cost(
            input=inference, label=label)
        cost8 = layer.rank_cost(left=score, right=score, label=score)
        cost9 = layer.lambda_cost(input=inference, score=score)
        cost10 = layer.sum_cost(input=inference)
L
Luo Tao 已提交
146 147
        cost11 = layer.huber_regression_cost(input=score, label=label)
        cost12 = layer.huber_classification_cost(input=score, label=label)
Q
qiaolongfei 已提交
148

D
dangqingqing 已提交
149 150 151
        print layer.parse_network([cost1, cost2])
        print layer.parse_network([cost3, cost4])
        print layer.parse_network([cost5, cost6])
L
Luo Tao 已提交
152
        print layer.parse_network([cost7, cost8, cost9, cost10, cost11, cost12])
L
Luo Tao 已提交
153 154 155 156 157 158 159 160

        crf = layer.crf(input=inference, label=label)
        crf_decoding = layer.crf_decoding(input=inference, size=3)
        ctc = layer.ctc(input=inference, label=label)
        warp_ctc = layer.warp_ctc(input=pixel, label=label)
        nce = layer.nce(input=inference, label=label, num_classes=3)
        hsigmoid = layer.hsigmoid(input=inference, label=label, num_classes=3)

D
dangqingqing 已提交
161 162
        print layer.parse_network(
            [crf, crf_decoding, ctc, warp_ctc, nce, hsigmoid])
L
Luo Tao 已提交
163 164 165 166 167 168 169


class OtherLayerTest(unittest.TestCase):
    def test_sampling_layer(self):
        maxid = layer.max_id(input=inference)
        sampling_id = layer.sampling_id(input=inference)
        eos = layer.eos(input=maxid, eos_id=5)
X
xuwei06 已提交
170
        layer.printer(maxid)
D
dangqingqing 已提交
171
        print layer.parse_network([maxid, sampling_id, eos])
L
Luo Tao 已提交
172 173 174 175

    def test_slicing_joining_layer(self):
        pad = layer.pad(input=conv, pad_c=[2, 3], pad_h=[1, 2], pad_w=[3, 1])
        print layer.parse_network(pad)
Q
qiaolongfei 已提交
176 177


L
Luo Tao 已提交
178
class ProjOpTest(unittest.TestCase):
D
dangqingqing 已提交
179
    def test_projection(self):
X
xuwei06 已提交
180
        input = layer.data(name='data2', type=data_type.dense_vector(784))
D
dangqingqing 已提交
181
        word = layer.data(
X
xuwei06 已提交
182
            name='word2', type=data_type.integer_value_sequence(10000))
D
dangqingqing 已提交
183 184
        fc0 = layer.fc(input=input, size=100, act=activation.Sigmoid())
        fc1 = layer.fc(input=input, size=200, act=activation.Sigmoid())
D
dangqingqing 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        mixed0 = layer.mixed(
            size=256,
            input=[
                layer.full_matrix_projection(input=fc0),
                layer.full_matrix_projection(input=fc1)
            ])
        with layer.mixed(size=200) as mixed1:
            mixed1 += layer.full_matrix_projection(input=fc0)
            mixed1 += layer.identity_projection(input=fc1)

        table = layer.table_projection(input=word)
        emb0 = layer.mixed(size=512, input=table)
        with layer.mixed(size=512) as emb1:
            emb1 += table

        scale = layer.scaling_projection(input=fc0)
        scale0 = layer.mixed(size=100, input=scale)
        with layer.mixed(size=100) as scale1:
            scale1 += scale

        dotmul = layer.dotmul_projection(input=fc0)
        dotmul0 = layer.mixed(size=100, input=dotmul)
        with layer.mixed(size=100) as dotmul1:
            dotmul1 += dotmul

        context = layer.context_projection(input=fc0, context_len=5)
X
xuwei06 已提交
211 212
        context0 = layer.mixed(size=500, input=context)
        with layer.mixed(size=500) as context1:
D
dangqingqing 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            context1 += context

        conv = layer.conv_projection(
            input=input,
            filter_size=1,
            num_channels=1,
            num_filters=128,
            stride=1,
            padding=0)
        conv0 = layer.mixed(input=conv, bias_attr=True)
        with layer.mixed(bias_attr=True) as conv1:
            conv1 += conv

        print layer.parse_network(mixed0)
        print layer.parse_network(mixed1)
        print layer.parse_network(emb0)
        print layer.parse_network(emb1)
        print layer.parse_network(scale0)
        print layer.parse_network(scale1)
        print layer.parse_network(dotmul0)
        print layer.parse_network(dotmul1)
        print layer.parse_network(conv0)
        print layer.parse_network(conv1)

    def test_operator(self):
X
xuwei06 已提交
238 239
        ipt0 = layer.data(name='data1', type=data_type.dense_vector(784))
        ipt1 = layer.data(name='word1', type=data_type.dense_vector(128))
D
dangqingqing 已提交
240 241
        fc0 = layer.fc(input=ipt0, size=100, act=activation.Sigmoid())
        fc1 = layer.fc(input=ipt0, size=100, act=activation.Sigmoid())
D
dangqingqing 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

        dotmul_op = layer.dotmul_operator(a=fc0, b=fc1)
        dotmul0 = layer.mixed(input=dotmul_op)
        with layer.mixed() as dotmul1:
            dotmul1 += dotmul_op

        conv = layer.conv_operator(
            img=ipt0,
            filter=ipt1,
            filter_size=1,
            num_channels=1,
            num_filters=128,
            stride=1,
            padding=0)
        conv0 = layer.mixed(input=conv)
        with layer.mixed() as conv1:
            conv1 += conv

        print layer.parse_network(dotmul0)
        print layer.parse_network(dotmul1)
        print layer.parse_network(conv0)
        print layer.parse_network(conv1)

Q
qiaolongfei 已提交
265

Y
Yu Yang 已提交
266 267
class NetworkTests(unittest.TestCase):
    def test_vgg(self):
X
xuwei06 已提交
268
        img = layer.data(name='pixel1', type=data_type.dense_vector(784))
Y
Yu Yang 已提交
269 270 271 272 273
        vgg_out = networks.small_vgg(
            input_image=img, num_channels=1, num_classes=2)
        print layer.parse_network(vgg_out)


Y
Yu Yang 已提交
274 275
class EvaluatorTest(unittest.TestCase):
    def test_evaluator(self):
X
xuwei06 已提交
276
        img = layer.data(name='pixel2', type=data_type.dense_vector(784))
Y
Yu Yang 已提交
277 278 279 280
        output = layer.fc(input=img,
                          size=10,
                          act=activation.Softmax(),
                          name='fc_here')
X
xuwei06 已提交
281
        lbl = layer.data(name='label2', type=data_type.integer_value(10))
Y
Yu Yang 已提交
282 283
        cost = layer.cross_entropy_cost(input=output, label=lbl)

Y
Yu Yang 已提交
284
        evaluator.classification_error(input=output, label=lbl)
Y
Yu Yang 已提交
285 286 287 288
        print layer.parse_network(cost)
        print layer.parse_network(output)


Q
qiaolongfei 已提交
289 290
if __name__ == '__main__':
    unittest.main()