nets.py 3.6 KB
Newer Older
R
root 已提交
1 2
import sys
import time
G
gmcather 已提交
3
import numpy as np
R
root 已提交
4

Y
Yibing Liu 已提交
5
import paddle
G
gmcather 已提交
6
import paddle.fluid as fluid
R
root 已提交
7

G
gmcather 已提交
8

G
gmcather 已提交
9 10
def bow_net(data,
            label,
R
root 已提交
11 12 13 14 15 16 17 18
            dict_dim,
            emb_dim=128,
            hid_dim=128,
            hid_dim2=96,
            class_dim=2):
    """
    bow net
    """
G
gmcather 已提交
19 20
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')
R
root 已提交
21
    bow_tanh = fluid.layers.tanh(bow)
G
gmcather 已提交
22 23 24
    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")
R
root 已提交
25 26 27
    cost = fluid.layers.cross_entropy(input=prediction, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc = fluid.layers.accuracy(input=prediction, label=label)
G
gmcather 已提交
28

R
root 已提交
29 30 31
    return avg_cost, acc, prediction


G
gmcather 已提交
32 33
def cnn_net(data,
            label,
R
root 已提交
34 35 36 37 38 39 40 41 42
            dict_dim,
            emb_dim=128,
            hid_dim=128,
            hid_dim2=96,
            class_dim=2,
            win_size=3):
    """
    conv net
    """
G
gmcather 已提交
43
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
R
root 已提交
44

G
gmcather 已提交
45 46
    conv_3 = fluid.nets.sequence_conv_pool(
        input=emb,
G
gmcather 已提交
47 48 49 50
        num_filters=hid_dim,
        filter_size=win_size,
        act="tanh",
        pool_type="max")
R
root 已提交
51

G
gmcather 已提交
52
    fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2)
R
root 已提交
53

G
gmcather 已提交
54
    prediction = fluid.layers.fc(input=[fc_1], size=class_dim, act="softmax")
R
root 已提交
55 56 57
    cost = fluid.layers.cross_entropy(input=prediction, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc = fluid.layers.accuracy(input=prediction, label=label)
G
gmcather 已提交
58

R
root 已提交
59 60 61
    return avg_cost, acc, prediction


G
gmcather 已提交
62 63 64 65 66 67 68 69
def lstm_net(data,
             label,
             dict_dim,
             emb_dim=128,
             hid_dim=128,
             hid_dim2=96,
             class_dim=2,
             emb_lr=30.0):
R
root 已提交
70 71 72
    """
    lstm net
    """
G
gmcather 已提交
73
    emb = fluid.layers.embedding(
G
gmcather 已提交
74
        input=data,
G
gmcather 已提交
75 76
        size=[dict_dim, emb_dim],
        param_attr=fluid.ParamAttr(learning_rate=emb_lr))
R
root 已提交
77

C
chengyao 已提交
78
    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)
R
root 已提交
79

G
gmcather 已提交
80 81
    lstm_h, c = fluid.layers.dynamic_lstm(
        input=fc0, size=hid_dim * 4, is_reverse=False)
R
root 已提交
82

G
gmcather 已提交
83
    lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')
R
root 已提交
84 85
    lstm_max_tanh = fluid.layers.tanh(lstm_max)

G
gmcather 已提交
86
    fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')
R
root 已提交
87

G
gmcather 已提交
88
    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')
R
root 已提交
89 90 91 92

    cost = fluid.layers.cross_entropy(input=prediction, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc = fluid.layers.accuracy(input=prediction, label=label)
G
gmcather 已提交
93

R
root 已提交
94 95 96
    return avg_cost, acc, prediction


G
gmcather 已提交
97 98
def gru_net(data,
            label,
R
root 已提交
99 100 101 102 103
            dict_dim,
            emb_dim=128,
            hid_dim=128,
            hid_dim2=96,
            class_dim=2,
104
            emb_lr=30.0):
R
root 已提交
105 106 107
    """
    gru net
    """
G
gmcather 已提交
108 109 110 111 112 113 114 115
    emb = fluid.layers.embedding(
        input=data,
        size=[dict_dim, emb_dim],
        param_attr=fluid.ParamAttr(learning_rate=emb_lr))

    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 3)
    gru_h = fluid.layers.dynamic_gru(input=fc0, size=hid_dim, is_reverse=False)
    gru_max = fluid.layers.sequence_pool(input=gru_h, pool_type='max')
R
root 已提交
116
    gru_max_tanh = fluid.layers.tanh(gru_max)
G
gmcather 已提交
117 118
    fc1 = fluid.layers.fc(input=gru_max_tanh, size=hid_dim2, act='tanh')
    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')
R
root 已提交
119 120 121 122

    cost = fluid.layers.cross_entropy(input=prediction, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc = fluid.layers.accuracy(input=prediction, label=label)
G
gmcather 已提交
123

R
root 已提交
124
    return avg_cost, acc, prediction