|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import paddle.fluid as fluid |
| 16 | +from paddlerec.core.utils import envs |
| 17 | +from paddlerec.core.model import ModelBase |
| 18 | +from paddlerec.core.metrics import RecallK |
| 19 | + |
| 20 | + |
| 21 | +class Model(ModelBase): |
| 22 | + def __init__(self, config): |
| 23 | + ModelBase.__init__(self, config) |
| 24 | + self.dict_size = 2000000 + 1 |
| 25 | + self.max_seq_len = 1024 |
| 26 | + self.emb_dim = 128 |
| 27 | + self.cnn_hid_dim = 128 |
| 28 | + self.cnn_win_size = 3 |
| 29 | + self.cnn_win_size2 = 5 |
| 30 | + self.hid_dim1 = 96 |
| 31 | + self.class_dim = 30 |
| 32 | + self.is_sparse = True |
| 33 | + |
| 34 | + def input_data(self, is_infer=False, **kwargs): |
| 35 | + |
| 36 | + text = fluid.data( |
| 37 | + name="text", shape=[None, self.max_seq_len, 1], dtype='int64') |
| 38 | + label = fluid.data(name="category", shape=[None, 1], dtype='int64') |
| 39 | + seq_len = fluid.data(name="seq_len", shape=[None], dtype='int64') |
| 40 | + return [text, label, seq_len] |
| 41 | + |
| 42 | + def net(self, inputs, is_infer=False): |
| 43 | + """ network definition """ |
| 44 | + #text label |
| 45 | + self.data = inputs[0] |
| 46 | + self.label = inputs[1] |
| 47 | + self.seq_len = inputs[2] |
| 48 | + emb = embedding(self.data, self.dict_size, self.emb_dim, |
| 49 | + self.is_sparse) |
| 50 | + concat = multi_convs(emb, self.seq_len, self.cnn_hid_dim, |
| 51 | + self.cnn_win_size, self.cnn_win_size2) |
| 52 | + self.fc_1 = full_connect(concat, self.hid_dim1) |
| 53 | + self.metrics(is_infer) |
| 54 | + |
| 55 | + def metrics(self, is_infer=False): |
| 56 | + """ classification and metrics """ |
| 57 | + # softmax layer |
| 58 | + prediction = fluid.layers.fc(input=[self.fc_1], |
| 59 | + size=self.class_dim, |
| 60 | + act="softmax", |
| 61 | + name="pretrain_fc_1") |
| 62 | + cost = fluid.layers.cross_entropy(input=prediction, label=self.label) |
| 63 | + avg_cost = fluid.layers.mean(x=cost) |
| 64 | + acc = fluid.layers.accuracy(input=prediction, label=self.label) |
| 65 | + #acc = RecallK(input=prediction, label=label, k=1) |
| 66 | + |
| 67 | + self._cost = avg_cost |
| 68 | + if is_infer: |
| 69 | + self._infer_results["acc"] = acc |
| 70 | + else: |
| 71 | + self._metrics["acc"] = acc |
| 72 | + |
| 73 | + |
| 74 | +def embedding(inputs, dict_size, emb_dim, is_sparse): |
| 75 | + """ embeding definition """ |
| 76 | + emb = fluid.layers.embedding( |
| 77 | + input=inputs, |
| 78 | + size=[dict_size, emb_dim], |
| 79 | + is_sparse=is_sparse, |
| 80 | + param_attr=fluid.ParamAttr( |
| 81 | + name='pretrain_word_embedding', |
| 82 | + initializer=fluid.initializer.Xavier())) |
| 83 | + return emb |
| 84 | + |
| 85 | + |
| 86 | +def multi_convs(input_layer, seq_len, cnn_hid_dim, cnn_win_size, |
| 87 | + cnn_win_size2): |
| 88 | + """conv and concat""" |
| 89 | + emb = fluid.layers.sequence_unpad( |
| 90 | + input_layer, length=seq_len, name="pretrain_unpad") |
| 91 | + conv = fluid.nets.sequence_conv_pool( |
| 92 | + param_attr=fluid.ParamAttr(name="pretrain_conv0_w"), |
| 93 | + bias_attr=fluid.ParamAttr(name="pretrain_conv0_b"), |
| 94 | + input=emb, |
| 95 | + num_filters=cnn_hid_dim, |
| 96 | + filter_size=cnn_win_size, |
| 97 | + act="tanh", |
| 98 | + pool_type="max") |
| 99 | + conv2 = fluid.nets.sequence_conv_pool( |
| 100 | + param_attr=fluid.ParamAttr(name="pretrain_conv1_w"), |
| 101 | + bias_attr=fluid.ParamAttr(name="pretrain_conv1_b"), |
| 102 | + input=emb, |
| 103 | + num_filters=cnn_hid_dim, |
| 104 | + filter_size=cnn_win_size2, |
| 105 | + act="tanh", |
| 106 | + pool_type="max") |
| 107 | + concat = fluid.layers.concat( |
| 108 | + input=[conv, conv2], axis=1, name="pretrain_concat") |
| 109 | + return concat |
| 110 | + |
| 111 | + |
| 112 | +def full_connect(input_layer, hid_dim1): |
| 113 | + """full connect layer""" |
| 114 | + fc_1 = fluid.layers.fc(name="pretrain_fc_0", |
| 115 | + input=input_layer, |
| 116 | + size=hid_dim1, |
| 117 | + act="tanh") |
| 118 | + return fc_1 |
0 commit comments