コード例 #1
0
    def __init__(self, config):
        super(BertForTokenClassification, self).__init__(config)
        self.sequence_domainslot_labels = 2
        self.token_domainslot_labels = 2

        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        # self.classifier_domain = nn.Linear(config.hidden_size, self.domain_num_labels)
        # self.classifier_dependcy = nn.Linear(config.hidden_size, self.dependcy_num_labels)
        self.tokenstart_numlabels = 2
        self.tokenend_numlabels = 2
        self.sentence_domainslot_numlabels = 2
        self.classifier_tokens_domainslot = 2
        self.classifier_tokenstart = nn.Sequential(
            nn.Linear(config.hidden_size, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.tokenstart_numlabels))
        self.classifier_tokensend = nn.Sequential(
            nn.Linear(config.hidden_size, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.tokenend_numlabels))
        self.classifier_sentence_domainslot = nn.Sequential(
            nn.Linear(config.hidden_size * 2, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.sentence_domainslot_numlabels))
        self.classifier_tokens_domainslot = nn.Sequential(
            nn.Linear(config.hidden_size * 2, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.classifier_tokens_domainslot))
        self.dropout = nn.Dropout(0.2)
        self.softmax = nn.Softmax(dim=2)

        self.apply(self.init_weights)
コード例 #2
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32
        self.hidden_size = config.hidden_size

        self.num_layers = 2

        n_filters = 200
        filter_sizes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        self.classifier = nn.Linear(
            len(filter_sizes) * n_filters * 3 + config.hidden_size * 3 +
            config.hidden_size * 3, config.num_labels)
        self.convs = TextCNN1D(config.hidden_size, n_filters, filter_sizes)
        self.resnet = ResnetCNN1D(config.hidden_size)
        # 最后的全连接层
        self.fc = nn.Sequential(
            nn.Linear(
                len(filter_sizes) * n_filters * 3 * 2, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.num_labels))
        self.layer_norm = nn.LayerNorm(self.hidden_size)
        self.FeedForward2 = nn.Sequential(

            # nn.BatchNorm1d(num_features=self.channel_size),
            nn.Linear(self.hidden_size, self.hidden_size),
            nn.ReLU(inplace=True),
            nn.Dropout(0.1))
        self.triple_attention = TripleAttentionHighWay(
            hidden_size=self.hidden_size)
        self.self_attention = ScaledDotProductAttention()
        self.apply(self.init_weights)
コード例 #3
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32

        rnn_hidden_size = 768
        num_layers = 2
        dropout = 0.2

        self.rnn = nn.LSTM(config.hidden_size,
                           rnn_hidden_size,
                           num_layers,
                           bidirectional=True,
                           batch_first=True,
                           dropout=dropout)

        self.cafe = CAFE(rnn_hidden_size)
        self.classifier = nn.Linear(rnn_hidden_size * 2, config.num_labels)

        # 最后的全连接层
        self.fc = nn.Sequential(
            nn.Linear(rnn_hidden_size * 2, self.num_labels),
            nn.BatchNorm1d(self.num_labels), nn.ReLU(inplace=True),
            nn.Dropout(0.5), nn.Linear(self.num_labels, self.num_labels))

        self.apply(self.init_weights)
コード例 #4
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32

        rnn_hidden_size = 768
        num_layers = 2
        dropout = 0.2

        self.rnn = nn.LSTM(config.hidden_size,
                           rnn_hidden_size,
                           num_layers,
                           bidirectional=True,
                           batch_first=True,
                           dropout=dropout)

        # self.classifier = nn.Linear(rnn_hidden_size * 2, config.num_labels)

        n_filters = 200
        filter_sizes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        self.classifier = nn.Linear(
            len(filter_sizes) * n_filters * 2, config.num_labels)
        self.convs = Conv1d(config.hidden_size, n_filters, filter_sizes)
        # 最后的全连接层
        self.fc = nn.Sequential(
            nn.Linear(rnn_hidden_size * 2, self.num_labels),
            nn.BatchNorm1d(self.num_labels), nn.ReLU(inplace=True),
            nn.Dropout(0.5), nn.Linear(self.num_labels, self.num_labels))

        self.apply(self.init_weights)
コード例 #5
0
    def __init__(self, config):
        super(BertForTokenClassification, self).__init__(config)
        self.domain_num_labels = 8
        self.dependcy_num_labels = 4

        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        # self.classifier_domain = nn.Linear(config.hidden_size, self.domain_num_labels)
        # self.classifier_dependcy = nn.Linear(config.hidden_size, self.dependcy_num_labels)
        self.classifier_domain = nn.Sequential(
            nn.Linear(config.hidden_size,config.hidden_size),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(config.hidden_size,self.domain_num_labels)
        )
        self.classifier_dependcy = nn.Sequential(
            nn.Linear(config.hidden_size, config.hidden_size),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.dependcy_num_labels)
        )



        self.apply(self.init_weights)
コード例 #6
0
    def __init__(self, config):
        super(BertForTokenClassification, self).__init__(config)
        self.num_labels = config.num_labels

        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.classifier = nn.Linear(config.hidden_size, config.num_labels)

        self.apply(self.init_weights)
コード例 #7
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32

        rnn_hidden_size = 300
        num_layers = 2
        dropout = 0.2

        self.rnn = nn.LSTM(config.hidden_size,
                           rnn_hidden_size,
                           num_layers,
                           bidirectional=True,
                           batch_first=True,
                           dropout=dropout)

        self.classifier = nn.Linear(rnn_hidden_size * 2, config.num_labels)

        # region embedding
        self.region_embedding = nn.Sequential(
            nn.Conv1d(config.hidden_size,
                      rnn_hidden_size,
                      kernel_size=3,
                      padding=1), nn.BatchNorm1d(num_features=rnn_hidden_size),
            nn.ReLU(), nn.Dropout(0.2))
        # conv 模块
        self.conv_block = nn.Sequential(
            nn.BatchNorm1d(num_features=rnn_hidden_size),
            nn.ReLU(),
            nn.Conv1d(rnn_hidden_size,
                      rnn_hidden_size,
                      kernel_size=3,
                      padding=1),
            nn.BatchNorm1d(num_features=rnn_hidden_size),
            nn.ReLU(),
            nn.Conv1d(rnn_hidden_size,
                      rnn_hidden_size,
                      kernel_size=3,
                      padding=1),
        )
        # repeat 模块
        self.max_len = 128
        resnet_block_list = []
        while (self.max_len > 2):
            resnet_block_list.append(ResnetBlock(rnn_hidden_size))
            self.max_len = self.max_len // 2
        self.resnet_layer = nn.Sequential(*resnet_block_list)

        # 最后的全连接层
        self.fc = nn.Sequential(
            nn.Linear(rnn_hidden_size * self.max_len, self.num_labels),
            nn.BatchNorm1d(self.num_labels), nn.ReLU(inplace=True),
            nn.Dropout(0.5), nn.Linear(self.num_labels, self.num_labels))

        self.apply(self.init_weights)
コード例 #8
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32
        self.hidden_size = config.hidden_size

        rnn_hidden_size = 768
        num_layers = 2
        dropout = 0.2

        self.rnn = nn.LSTM(config.hidden_size * 3,
                           rnn_hidden_size,
                           num_layers,
                           bidirectional=True,
                           batch_first=True,
                           dropout=dropout)

        self.W2 = nn.Linear(config.hidden_size + 2 * rnn_hidden_size,
                            config.hidden_size)
        # self.classifier = nn.Linear(rnn_hidden_size * 2, config.num_labels)

        n_filters = 200
        filter_sizes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        self.classifier = nn.Linear(
            len(filter_sizes) * n_filters * 3 + config.hidden_size * 3 +
            config.hidden_size * 3, config.num_labels)
        self.convs = TextCNN1D(config.hidden_size * 3, n_filters, filter_sizes)

        # 最后的全连接层
        self.fc = nn.Sequential(
            nn.Linear(
                len(filter_sizes) * n_filters * 3 + config.hidden_size * 3 +
                config.hidden_size * 3, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.num_labels))
        self.layer_norm = nn.LayerNorm(self.hidden_size)
        self.FeedForward2 = nn.Sequential(

            # nn.BatchNorm1d(num_features=self.channel_size),
            nn.Linear(self.hidden_size, self.hidden_size),
            nn.ReLU(inplace=True),
            nn.Dropout(0.1))
        self.triple_attention = TripleAttention(hidden_size=self.hidden_size)

        self.kmax_pooling = KMaxPooling1D(k=5)

        self.self_attention = ScaledDotProductAttention()
        self.apply(self.init_weights)
コード例 #9
0
    def __init__(self, config):
        super(BertForSequenceClassification, self).__init__(config)
        self.num_labels = config.num_labels
        self.bert = BertModel(config)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.batch_size = 32
        self.hidden_size = config.hidden_size

        rnn_hidden_size = config.hidden_size
        num_layers = 2
        dropout = 0.2

        self.rnn = nn.LSTM(config.hidden_size,
                           rnn_hidden_size,
                           num_layers,
                           bidirectional=True,
                           batch_first=True,
                           dropout=dropout)

        self.W2 = nn.Linear(config.hidden_size * 2, config.hidden_size)

        # 最后的全连接层
        self.classfier = nn.Sequential(
            nn.Linear(config.hidden_size * 6, config.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.5),
            nn.Linear(config.hidden_size, self.num_labels))
        self.layer_norm = nn.LayerNorm(self.hidden_size)
        self.FeedForward = nn.Sequential(
            nn.Linear(self.hidden_size * 2, self.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.1))
        self.FeedForward2 = nn.Sequential(
            nn.Linear(self.hidden_size, self.hidden_size),
            nn.ReLU(inplace=True), nn.Dropout(0.1))
        self.triple_attention = TripleAttention(hidden_size=self.hidden_size)

        self.apply(self.init_weights)