Kaggle知識點:BERT的五種Pooling方法

BERT模型可以用於(yu) 多個(ge) 任務,也是現在NLP模型必備的方法。在文本分類中我們(men) 會(hui) 使用[CLS]對應的輸出完成文本分類,當然也有其他的方法。

這樣可以使用每個(ge) token對應的輸出,通過pooling之後再進行分類。本文將介紹常見的幾種與(yu) BERT搭建使用的方法。

方法1:MeanPooling

將每個(ge) token對應的輸出計算均值,這裏需要考慮attention_mask,也就是需要考慮有效的輸入的token。

class MeanPooling(nn.Module): def __init__(self): super(MeanPooling, self).__init__() def forward(self, last_hidden_state, attention_mask): input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()         sum_embeddings = torch.sum(last_hidden_state * input_mask_expanded, 1)         sum_mask = input_mask_expanded.sum(1)         sum_mask = torch.clamp(sum_mask, min = 1e-9)         mean_embeddings = sum_embeddings/sum_mask return mean_embeddings

方法2:MaxPooling

將每個(ge) token對應的輸出計算最大值,這裏需要考慮attention_mask,也就是需要考慮有效的輸入的token。

class MaxPooling(nn.Module): def __init__(self): super(MaxPooling, self).__init__() def forward(self, last_hidden_state, attention_mask): input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()         embeddings = last_hidden_state.clone()         embeddings[input_mask_expanded == 0] = -1e4 max_embeddings, _ = torch.max(embeddings, dim = 1) return max_embeddings

方法3:MinPooling

將每個(ge) token對應的輸出計算最小值,這裏需要考慮attention_mask,也就是需要考慮有效的輸入的token。

class MinPooling(nn.Module): def __init__(self): super(MinPooling, self).__init__() def forward(self, last_hidden_state, attention_mask): input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()         embeddings = last_hidden_state.clone()         embeddings[input_mask_expanded == 0] = 1e-4 min_embeddings, _ = torch.min(embeddings, dim = 1) return min_embeddings

方法4:WeightedPooling

將每個(ge) token對應的輸出計算出權重,這裏的權重可以通過特征進行計算,也可以考慮通過IDF計算出權重。

class WeightedLayerPooling(nn.Module): def __init__(self, num_hidden_layers, layer_start: int = 4, layer_weights = None): super(WeightedLayerPooling, self).__init__()         self.layer_start = layer_start         self.num_hidden_layers = num_hidden_layers         self.layer_weights = layer_weights if layer_weights is not None else nn.Parameter(                 torch.tensor([1] * (num_hidden_layers+1 - layer_start), dtype=torch.float)             )

def forward(self, ft_all_layers): all_layer_embedding = torch.stack(ft_all_layers) all_layer_embedding = all_layer_embedding[self.layer_start:, :, :, :] weight_factor = self.layer_weights.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).expand(all_layer_embedding.size()) weighted_average = (weight_factor*all_layer_embedding).sum(dim=0) / self.layer_weights.sum() return weighted_average

方法5:AttentionPooling

將每個(ge) token的特征單獨加入一層,用於(yu) 注意力的計算,增加模型的建模能力。

class AttentionPooling(nn.Module): def __init__(self, in_dim): super().__init__()         self.attention = nn.Sequential(         nn.Linear(in_dim, in_dim),         nn.LayerNorm(in_dim),         nn.GELU(),         nn.Linear(in_dim, 1),         )

def forward(self, last_hidden_state, attention_mask): w = self.attention(last_hidden_state).float() w[attention_mask==0]=float('-inf') w = torch.softmax(w,1) attention_embeddings = torch.sum(w * last_hidden_state, dim=1) return attention_embeddings

總結

從(cong) 模型複雜度上:AttentionPooling > WeightedLayerPooling >  MeanPooling / MinPooling / MaxPooling

從(cong) 模型精度上:AttentionPooling > WeightedLayerPooling > MeanPooling > MaxPooling > MinPooling

使用多種Pooling的目的是增加BERT模型的多樣性,考慮在模型集成中使用。

【競賽報名/項目谘詢+微信:mollywei007】

上一篇

美國Top100學校托福單項成績要求

下一篇

Kaggle賽題解析:OTTO電商商品推薦

你也可能喜歡

  • 暫無相關文章!

評論已經被關(guan) 閉。

插入圖片
返回頂部