models.py 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 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.
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear, Embedding
X
Xiaoyao Xi 已提交
16
# from paddle.fluid.dygraph.base import to_variable
17 18 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
import numpy as np
from hapi.model import Model
from hapi.text.text import GRUEncoderLayer as BiGRUEncoder
from hapi.text.text import BOWEncoder, CNNEncoder, GRUEncoder, LSTMEncoder

class CNN(Model):
    def __init__(self,  dict_dim, seq_len):
        super(CNN, self).__init__()
        self.dict_dim = dict_dim
        self.emb_dim = 128
        self.hid_dim = 128
        self.fc_hid_dim = 96
        self.class_dim = 3
        self.channels = 1
        self.win_size = [3, self.hid_dim]
        self.seq_len = seq_len
        self._encoder = CNNEncoder(
            dict_size=self.dict_dim + 1,
            emb_dim=self.emb_dim,
            seq_len=self.seq_len,
            filter_size= self.win_size,
            num_filters= self.hid_dim,
            hidden_dim= self.hid_dim,
            padding_idx=None,
            act='tanh')
        self._fc1 = Linear(input_dim = self.hid_dim*self.seq_len, output_dim=self.fc_hid_dim, act="softmax")
        self._fc_prediction = Linear(input_dim = self.fc_hid_dim,
                                 output_dim = self.class_dim,
                                 act="softmax")

    def forward(self, inputs):
        conv_3 = self._encoder(inputs)
        fc_1 = self._fc1(conv_3)
        prediction = self._fc_prediction(fc_1)
        return prediction


class BOW(Model):
    def __init__(self, dict_dim, seq_len):
        super(BOW, self).__init__()
        self.dict_dim = dict_dim
        self.emb_dim = 128
        self.hid_dim = 128
        self.fc_hid_dim = 96
        self.class_dim = 3
        self.seq_len = seq_len
        self._encoder = BOWEncoder(
            dict_size=self.dict_dim + 1,
            emb_dim=self.emb_dim,
            padding_idx=None,
            bow_dim=self.hid_dim,
            seq_len=self.seq_len)
        self._fc1 = Linear(input_dim = self.hid_dim, output_dim=self.hid_dim, act="tanh")
        self._fc2 = Linear(input_dim = self.hid_dim, output_dim=self.fc_hid_dim, act="tanh")
        self._fc_prediction = Linear(input_dim = self.fc_hid_dim,
                                 output_dim = self.class_dim,
                                 act="softmax")

    def forward(self, inputs):
        bow_1 = self._encoder(inputs)
        bow_1 = fluid.layers.tanh(bow_1)
        fc_1 = self._fc1(bow_1)
        fc_2 = self._fc2(fc_1)
        prediction = self._fc_prediction(fc_2)
        return prediction


class GRU(Model):
    def __init__(self, dict_dim, seq_len):
        super(GRU, self).__init__()
        self.dict_dim = dict_dim
        self.emb_dim = 128
        self.hid_dim = 128
        self.fc_hid_dim = 96
        self.class_dim = 3
        self.seq_len = seq_len
        self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh")
        self._fc_prediction = Linear(input_dim=self.fc_hid_dim,
                                 output_dim=self.class_dim,
                                 act="softmax")
        self._encoder = GRUEncoder(
            dict_size=self.dict_dim + 1,
            emb_dim=self.emb_dim,
            gru_dim=self.hid_dim,
            hidden_dim=self.hid_dim,
            padding_idx=None,
            seq_len=self.seq_len)

    def forward(self, inputs):
        emb = self._encoder(inputs)
        fc_1 = self._fc1(emb)
        prediction = self._fc_prediction(fc_1)
        return prediction

        
class BiGRU(Model):
    def __init__(self, dict_dim, batch_size, seq_len):
        super(BiGRU, self).__init__()
        self.dict_dim = dict_dim
        self.emb_dim = 128
        self.hid_dim = 128
        self.fc_hid_dim = 96
        self.class_dim = 3
        self.batch_size = batch_size
        self.seq_len = seq_len
        self.embedding = Embedding(
            size=[self.dict_dim + 1, self.emb_dim],
            dtype='float32',
            param_attr=fluid.ParamAttr(learning_rate=30),
            is_sparse=False)
X
Xiaoyao Xi 已提交
127 128
        # h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32")
        # h_0 = to_variable(h_0)
129 130 131 132 133 134 135 136
        self._fc1 = Linear(input_dim = self.hid_dim, output_dim=self.hid_dim*3)
        self._fc2 = Linear(input_dim = self.hid_dim*2, output_dim=self.fc_hid_dim, act="tanh")
        self._fc_prediction = Linear(input_dim=self.fc_hid_dim,
                                 output_dim=self.class_dim,
                                 act="softmax")
        self._encoder = BiGRUEncoder(
            grnn_hidden_dim=self.hid_dim,
            input_dim=self.hid_dim * 3,
X
Xiaoyao Xi 已提交
137
            # h_0=h_0,
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
            init_bound=0.1,
            is_bidirection=True)

    def forward(self, inputs):
        emb = self.embedding(inputs)
        emb = fluid.layers.reshape(emb, shape=[self.batch_size, -1, self.hid_dim])
        fc_1 = self._fc1(emb)
        encoded_vector = self._encoder(fc_1)
        encoded_vector = fluid.layers.tanh(encoded_vector)
        encoded_vector = fluid.layers.reduce_max(encoded_vector, dim=1)
        fc_2 = self._fc2(encoded_vector)
        prediction = self._fc_prediction(fc_2)
        return prediction

class LSTM(Model):
    def __init__(self, dict_dim, seq_len):
        super(LSTM, self).__init__()
        self.seq_len = seq_len,
        self.dict_dim = dict_dim,
        self.emb_dim = 128,
        self.hid_dim = 128,
        self.fc_hid_dim = 96,
        self.class_dim = 3,
        self.emb_lr = 30.0,
        self._encoder = LSTMEncoder(
            dict_size=dict_dim + 1,
            emb_dim=self.emb_dim,
            lstm_dim=self.hid_dim,
            hidden_dim=self.hid_dim,
            seq_len=self.seq_len,
            padding_idx=None,
            is_reverse=False)

        self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh")
        self._fc_prediction = Linear(input_dim=self.fc_hid_dim,
                                 output_dim=self.class_dim,
                                 act="softmax")
    def forward(self, inputs):
        emb = self._encoder(inputs)
        fc_1 = self._fc1(emb)
        prediction = self._fc_prediction(fc_1)
        return prediction