simple_nets.py 3.9 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 16
import numpy as np

17
import paddle
18 19 20
import paddle.fluid as fluid


Z
Zeng Jinle 已提交
21
def simple_fc_net_with_inputs(img, label, class_num=10):
22
    hidden = img
23
    for _ in range(2):
C
Charles-hit 已提交
24
        hidden = paddle.static.nn.fc(
25
            hidden,
26
            size=100,
C
Charles-hit 已提交
27
            activation='relu',
28 29 30 31
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=1.0)
            ),
        )
C
Charles-hit 已提交
32 33 34
    prediction = paddle.static.nn.fc(
        hidden, size=class_num, activation='softmax'
    )
35 36 37
    loss = paddle.nn.functional.cross_entropy(
        input=prediction, label=label, reduction='none', use_softmax=False
    )
38
    loss = paddle.mean(loss)
39 40 41
    return loss


Z
Zeng Jinle 已提交
42
def simple_fc_net(use_feed=None):
G
GGBond8488 已提交
43 44
    img = paddle.static.data(name='image', shape=[-1, 784], dtype='float32')
    label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
Z
Zeng Jinle 已提交
45 46 47
    return simple_fc_net_with_inputs(img, label, class_num=10)


48
def batchnorm_fc_with_inputs(img, label, class_num=10):
49 50
    hidden = img
    for _ in range(2):
C
Charles-hit 已提交
51
        hidden = paddle.static.nn.fc(
52 53
            hidden,
            size=200,
C
Charles-hit 已提交
54
            activation='relu',
55 56 57 58
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=1.0)
            ),
        )
59

60
        hidden = paddle.static.nn.batch_norm(input=hidden)
61

C
Charles-hit 已提交
62 63 64
    prediction = paddle.static.nn.fc(
        hidden, size=class_num, activation='softmax'
    )
65 66 67
    loss = paddle.nn.functional.cross_entropy(
        input=prediction, label=label, reduction='none', use_softmax=False
    )
68
    loss = paddle.mean(loss)
69 70 71
    return loss


72
def fc_with_batchnorm(use_feed=None):
G
GGBond8488 已提交
73 74
    img = paddle.static.data(name='image', shape=[-1, 784], dtype='float32')
    label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
75 76 77
    return batchnorm_fc_with_inputs(img, label, class_num=10)


78 79 80 81 82 83 84 85 86
def bow_net(
    use_feed,
    dict_dim,
    is_sparse=False,
    emb_dim=128,
    hid_dim=128,
    hid_dim2=96,
    class_dim=2,
):
87 88 89 90 91
    """
    BOW net
    This model is from https://github.com/PaddlePaddle/models:
    fluid/PaddleNLP/text_classification/nets.py
    """
G
GGBond8488 已提交
92 93
    data = paddle.static.data(
        name="words", shape=[-1, 1], dtype="int64", lod_level=1
94
    )
G
GGBond8488 已提交
95
    label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64")
96 97 98
    emb = fluid.layers.embedding(
        input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
    )
99
    bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')
100
    bow_tanh = paddle.tanh(bow)
C
Charles-hit 已提交
101 102 103 104 105
    fc_1 = paddle.static.nn.fc(x=bow_tanh, size=hid_dim, activation="tanh")
    fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim2, activation="tanh")
    prediction = paddle.static.nn.fc(
        x=[fc_2], size=class_dim, activation="softmax"
    )
106 107 108
    cost = paddle.nn.functional.cross_entropy(
        input=prediction, label=label, reduction='none', use_softmax=False
    )
109
    avg_cost = paddle.mean(x=cost)
110 111 112 113

    return avg_cost


114 115 116 117 118
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)
119 120 121 122 123
    label = (
        np.array([np.random.randint(0, label_range) for _ in range(batch_size)])
        .reshape((-1, 1))
        .astype("int64")
    )
124
    return img, label