simple_nets.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

15
import paddle
16 17 18 19
import paddle.fluid as fluid
import numpy as np


Z
Zeng Jinle 已提交
20
def simple_fc_net_with_inputs(img, label, class_num=10):
21
    hidden = img
22
    for _ in range(2):
23 24
        hidden = fluid.layers.fc(
            hidden,
25
            size=100,
26
            act='relu',
27 28 29 30
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=1.0)
            ),
        )
Z
Zeng Jinle 已提交
31
    prediction = fluid.layers.fc(hidden, size=class_num, act='softmax')
32
    loss = fluid.layers.cross_entropy(input=prediction, label=label)
33
    loss = paddle.mean(loss)
34 35 36
    return loss


Z
Zeng Jinle 已提交
37 38 39 40 41 42
def simple_fc_net(use_feed=None):
    img = fluid.layers.data(name='image', shape=[784], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    return simple_fc_net_with_inputs(img, label, class_num=10)


43
def batchnorm_fc_with_inputs(img, label, class_num=10):
44 45 46 47 48 49
    hidden = img
    for _ in range(2):
        hidden = fluid.layers.fc(
            hidden,
            size=200,
            act='relu',
50 51 52 53
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=1.0)
            ),
        )
54 55 56

        hidden = fluid.layers.batch_norm(input=hidden)

57
    prediction = fluid.layers.fc(hidden, size=class_num, act='softmax')
58
    loss = fluid.layers.cross_entropy(input=prediction, label=label)
59
    loss = paddle.mean(loss)
60 61 62
    return loss


63 64 65 66 67 68
def fc_with_batchnorm(use_feed=None):
    img = fluid.layers.data(name='image', shape=[784], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    return batchnorm_fc_with_inputs(img, label, class_num=10)


69 70 71 72 73 74 75 76 77
def bow_net(
    use_feed,
    dict_dim,
    is_sparse=False,
    emb_dim=128,
    hid_dim=128,
    hid_dim2=96,
    class_dim=2,
):
78 79 80 81 82
    """
    BOW net
    This model is from https://github.com/PaddlePaddle/models:
    fluid/PaddleNLP/text_classification/nets.py
    """
83 84 85
    data = fluid.layers.data(
        name="words", shape=[1], dtype="int64", lod_level=1
    )
86
    label = fluid.layers.data(name="label", shape=[1], dtype="int64")
87 88 89
    emb = fluid.layers.embedding(
        input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
    )
90
    bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')
91
    bow_tanh = paddle.tanh(bow)
92 93 94 95
    fc_1 = fluid.layers.fc(input=bow_tanh, size=hid_dim, act="tanh")
    fc_2 = fluid.layers.fc(input=fc_1, size=hid_dim2, act="tanh")
    prediction = fluid.layers.fc(input=[fc_2], size=class_dim, act="softmax")
    cost = fluid.layers.cross_entropy(input=prediction, label=label)
96
    avg_cost = paddle.mean(x=cost)
97 98 99 100

    return avg_cost


101 102 103 104 105
def init_data(batch_size=32, img_shape=[784], label_range=9):
    np.random.seed(5)
    assert isinstance(img_shape, list)
    input_shape = [batch_size] + img_shape
    img = np.random.random(size=input_shape).astype(np.float32)
106 107 108 109 110
    label = (
        np.array([np.random.randint(0, label_range) for _ in range(batch_size)])
        .reshape((-1, 1))
        .astype("int64")
    )
111
    return img, label