net.py 8.4 KB
Newer Older
F
frankwhzhang 已提交
1 2
import paddle.fluid as fluid

Z
zhangwenhui03 已提交
3 4 5 6 7

def all_vocab_network(vocab_size,
                      hid_size=100,
                      init_low_bound=-0.04,
                      init_high_bound=0.04):
F
frankwhzhang 已提交
8 9 10 11 12
    """ network definition """
    emb_lr_x = 10.0
    gru_lr_x = 1.0
    fc_lr_x = 1.0
    # Input data
Z
zhang wenhui 已提交
13 14 15 16
    src_wordseq = fluid.data(
        name="src_wordseq", shape=[None, 1], dtype="int64", lod_level=1)
    dst_wordseq = fluid.data(
        name="dst_wordseq", shape=[None, 1], dtype="int64", lod_level=1)
F
frankwhzhang 已提交
17

Z
zhang wenhui 已提交
18
    emb = fluid.embedding(
F
frankwhzhang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        input=src_wordseq,
        size=[vocab_size, hid_size],
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=init_low_bound, high=init_high_bound),
            learning_rate=emb_lr_x),
        is_sparse=True)
    fc0 = fluid.layers.fc(input=emb,
                          size=hid_size * 3,
                          param_attr=fluid.ParamAttr(
                              initializer=fluid.initializer.Uniform(
                                  low=init_low_bound, high=init_high_bound),
                              learning_rate=gru_lr_x))
    gru_h0 = fluid.layers.dynamic_gru(
        input=fc0,
        size=hid_size,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=init_low_bound, high=init_high_bound),
            learning_rate=gru_lr_x))

    fc = fluid.layers.fc(input=gru_h0,
                         size=vocab_size,
                         act='softmax',
                         param_attr=fluid.ParamAttr(
                             initializer=fluid.initializer.Uniform(
                                 low=init_low_bound, high=init_high_bound),
                             learning_rate=fc_lr_x))
    cost = fluid.layers.cross_entropy(input=fc, label=dst_wordseq)
    acc = fluid.layers.accuracy(input=fc, label=dst_wordseq, k=20)
    avg_cost = fluid.layers.mean(x=cost)
    return src_wordseq, dst_wordseq, avg_cost, acc
Z
zhangwenhui03 已提交
51 52 53 54 55 56 57 58


def train_bpr_network(vocab_size, neg_size, hid_size, drop_out=0.2):
    """ network definition """
    emb_lr_x = 1.0
    gru_lr_x = 1.0
    fc_lr_x = 1.0
    # Input data
Z
zhang wenhui 已提交
59 60 61 62 63
    src = fluid.data(name="src", shape=[None, 1], dtype="int64", lod_level=1)
    pos_label = fluid.data(
        name="pos_label", shape=[None, 1], dtype="int64", lod_level=1)
    label = fluid.data(
        name="label", shape=[None, neg_size + 1], dtype="int64", lod_level=1)
Z
zhangwenhui03 已提交
64

Z
zhang wenhui 已提交
65
    emb_src = fluid.embedding(
Z
zhangwenhui03 已提交
66 67 68 69 70 71
        input=src,
        size=[vocab_size, hid_size],
        param_attr=fluid.ParamAttr(
            name="emb",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=emb_lr_x))
Z
zhang wenhui 已提交
72
    emb_src = fluid.layers.squeeze(input=emb_src, axes=[1])
Z
zhangwenhui03 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    emb_src_drop = fluid.layers.dropout(emb_src, dropout_prob=drop_out)

    fc0 = fluid.layers.fc(input=emb_src_drop,
                          size=hid_size * 3,
                          param_attr=fluid.ParamAttr(
                              name="gru_fc",
                              initializer=fluid.initializer.XavierInitializer(),
                              learning_rate=gru_lr_x),
                          bias_attr=False)
    gru_h0 = fluid.layers.dynamic_gru(
        input=fc0,
        size=hid_size,
        param_attr=fluid.ParamAttr(
            name="dy_gru.param",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=gru_lr_x),
        bias_attr="dy_gru.bias")
    gru_h0_drop = fluid.layers.dropout(gru_h0, dropout_prob=drop_out)

    label_re = fluid.layers.sequence_reshape(input=label, new_dim=1)
Z
zhang wenhui 已提交
94
    emb_label1 = fluid.embedding(
Z
zhangwenhui03 已提交
95 96 97 98 99 100 101
        input=label_re,
        size=[vocab_size, hid_size],
        param_attr=fluid.ParamAttr(
            name="emb",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=emb_lr_x))

Z
zhang wenhui 已提交
102
    emb_label = fluid.layers.squeeze(input=emb_label1, axes=[1])
Z
zhangwenhui03 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    emb_label_drop = fluid.layers.dropout(emb_label, dropout_prob=drop_out)

    gru_exp = fluid.layers.expand(
        x=gru_h0_drop, expand_times=[1, (neg_size + 1)])
    gru = fluid.layers.sequence_reshape(input=gru_exp, new_dim=hid_size)

    ele_mul = fluid.layers.elementwise_mul(emb_label_drop, gru)
    red_sum = fluid.layers.reduce_sum(input=ele_mul, dim=1, keep_dim=True)

    pre = fluid.layers.sequence_reshape(input=red_sum, new_dim=(neg_size + 1))

    cost = fluid.layers.bpr_loss(input=pre, label=pos_label)
    cost_sum = fluid.layers.reduce_sum(input=cost)
    return src, pos_label, label, cost_sum


def train_cross_entropy_network(vocab_size, neg_size, hid_size, drop_out=0.2):
    """ network definition """
    emb_lr_x = 1.0
    gru_lr_x = 1.0
    fc_lr_x = 1.0
    # Input data
Z
zhang wenhui 已提交
125 126 127 128 129
    src = fluid.data(name="src", shape=[None, 1], dtype="int64", lod_level=1)
    pos_label = fluid.data(
        name="pos_label", shape=[None, 1], dtype="int64", lod_level=1)
    label = fluid.data(
        name="label", shape=[None, neg_size + 1], dtype="int64", lod_level=1)
Z
zhangwenhui03 已提交
130

Z
zhang wenhui 已提交
131
    emb_src = fluid.embedding(
Z
zhangwenhui03 已提交
132 133 134 135 136 137
        input=src,
        size=[vocab_size, hid_size],
        param_attr=fluid.ParamAttr(
            name="emb",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=emb_lr_x))
Z
zhang wenhui 已提交
138
    emb_src = fluid.layers.squeeze(input=emb_src, axes=[1])
Z
zhangwenhui03 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    emb_src_drop = fluid.layers.dropout(emb_src, dropout_prob=drop_out)

    fc0 = fluid.layers.fc(input=emb_src_drop,
                          size=hid_size * 3,
                          param_attr=fluid.ParamAttr(
                              name="gru_fc",
                              initializer=fluid.initializer.XavierInitializer(),
                              learning_rate=gru_lr_x),
                          bias_attr=False)
    gru_h0 = fluid.layers.dynamic_gru(
        input=fc0,
        size=hid_size,
        param_attr=fluid.ParamAttr(
            name="dy_gru.param",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=gru_lr_x),
        bias_attr="dy_gru.bias")
    gru_h0_drop = fluid.layers.dropout(gru_h0, dropout_prob=drop_out)

    label_re = fluid.layers.sequence_reshape(input=label, new_dim=1)
Z
zhang wenhui 已提交
160
    emb_label1 = fluid.embedding(
Z
zhangwenhui03 已提交
161 162 163 164 165 166
        input=label_re,
        size=[vocab_size, hid_size],
        param_attr=fluid.ParamAttr(
            name="emb",
            initializer=fluid.initializer.XavierInitializer(),
            learning_rate=emb_lr_x))
Z
zhang wenhui 已提交
167
    emb_label = fluid.layers.squeeze(input=emb_label1, axes=[1])
Z
zhangwenhui03 已提交
168 169 170 171 172 173 174 175 176 177

    emb_label_drop = fluid.layers.dropout(emb_label, dropout_prob=drop_out)

    gru_exp = fluid.layers.expand(
        x=gru_h0_drop, expand_times=[1, (neg_size + 1)])
    gru = fluid.layers.sequence_reshape(input=gru_exp, new_dim=hid_size)

    ele_mul = fluid.layers.elementwise_mul(emb_label_drop, gru)
    red_sum = fluid.layers.reduce_sum(input=ele_mul, dim=1, keep_dim=True)

Z
zhangwenhui03 已提交
178 179
    pre_ = fluid.layers.sequence_reshape(input=red_sum, new_dim=(neg_size + 1))
    pre = fluid.layers.softmax(input=pre_)
Z
zhangwenhui03 已提交
180 181 182 183 184 185

    cost = fluid.layers.cross_entropy(input=pre, label=pos_label)
    cost_sum = fluid.layers.reduce_sum(input=cost)
    return src, pos_label, label, cost_sum


Z
add ssr  
zhangwenhui03 已提交
186
def infer_network(vocab_size, batch_size, hid_size, dropout=0.2):
Z
zhang wenhui 已提交
187 188
    src = fluid.data(name="src", shape=[None, 1], dtype="int64", lod_level=1)
    emb_src = fluid.embedding(
Z
zhangwenhui03 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        input=src, size=[vocab_size, hid_size], param_attr="emb")
    emb_src_drop = fluid.layers.dropout(
        emb_src, dropout_prob=dropout, is_test=True)

    fc0 = fluid.layers.fc(input=emb_src_drop,
                          size=hid_size * 3,
                          param_attr="gru_fc",
                          bias_attr=False)
    gru_h0 = fluid.layers.dynamic_gru(
        input=fc0,
        size=hid_size,
        param_attr="dy_gru.param",
        bias_attr="dy_gru.bias")
    gru_h0_drop = fluid.layers.dropout(
        gru_h0, dropout_prob=dropout, is_test=True)

Z
zhang wenhui 已提交
205 206 207
    all_label = fluid.data(
        name="all_label", shape=[vocab_size, 1], dtype="int64")
    emb_all_label = fluid.embedding(
Z
zhangwenhui03 已提交
208
        input=all_label, size=[vocab_size, hid_size], param_attr="emb")
Z
zhang wenhui 已提交
209
    emb_all_label = fluid.layers.squeeze(input=emb_all_label, axes=[1])
Z
zhangwenhui03 已提交
210 211 212 213 214 215
    emb_all_label_drop = fluid.layers.dropout(
        emb_all_label, dropout_prob=dropout, is_test=True)

    all_pre = fluid.layers.matmul(
        gru_h0_drop, emb_all_label_drop, transpose_y=True)

Z
zhang wenhui 已提交
216 217
    pos_label = fluid.data(
        name="pos_label", shape=[None, 1], dtype="int64", lod_level=1)
Z
zhangwenhui03 已提交
218 219
    acc = fluid.layers.accuracy(input=all_pre, label=pos_label, k=20)
    return acc