File size: 7,041 Bytes
ed540d3 7511825 ed540d3 5d81f4d ed540d3 5d81f4d ed540d3 5d81f4d ed540d3 7511825 ed540d3 5d81f4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 127 128 129 130 131 132 133 134 135 136 137 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import torch
import logging
from pathlib import Path
import os
from transformers import BertModel, BertForTokenClassification
import pandas as pd
import evaluate
from datasets import load_dataset
from transformers import BertTokenizerFast
from torch.utils.data import DataLoader
from tqdm import tqdm
def tokenize(dataset):
BERT_MAX_LEN = 512
tokenizer = BertTokenizerFast.from_pretrained(
"neuralmind/bert-base-portuguese-cased", max_length=BERT_MAX_LEN)
dataset = dataset.map(lambda example: tokenizer(
example["text"], truncation=True, padding="max_length", max_length=BERT_MAX_LEN))
return dataset
def create_dataloader(dataset, shuffle=True):
return DataLoader(dataset, batch_size=8, shuffle=shuffle, num_workers=8, drop_last=True)
CURRENT_PATH = Path(__file__).parent
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s', datefmt='%H:%M:%S', filename=os.path.join(CURRENT_PATH, 'out', 'debug_embeddings.txt'), filemode='w')
class LanguageIdentifer(torch.nn.Module):
def __init__(self, mode='horizontal_stacking', pos_layers_to_freeze=0, bertimbau_layers_to_freeze=0):
super().__init__()
self.labels = ['pt-PT', 'pt-BR']
self.portuguese_model = BertModel.from_pretrained(
"neuralmind/bert-base-portuguese-cased")
self.portuguese_pos_tagging_model = BertForTokenClassification.from_pretrained(
"lisaterumi/postagger-portuguese")
for layer in range(bertimbau_layers_to_freeze):
for name, param in self.portuguese_model.named_parameters():
if f".{layer}" in name:
print(f"Freezing Layer {name} of Bertimbau")
param.requires_grad = False
for layer in range(pos_layers_to_freeze):
for name, param in self.portuguese_pos_tagging_model.named_parameters():
if f".{layer}" in name:
print(f"Freezing Layer {name} of POS")
param.requires_grad = False
self.portuguese_pos_tagging_model.classifier = torch.nn.Identity()
self.mode = mode
if self.mode == 'horizontal_stacking':
self.linear = self.common_network(torch.nn.Linear(
self.portuguese_pos_tagging_model.config.hidden_size + self.portuguese_model.config.hidden_size, 512))
elif self.mode == 'bertimbau_only' or self.mode == 'pos_only' or self.mode == 'vertical_sum':
self.linear = self.common_network(torch.nn.Linear(
self.portuguese_model.config.hidden_size, 512))
else:
raise NotImplementedError
def common_network(self, custom_linear):
return torch.nn.Sequential(
custom_linear,
torch.nn.ReLU(),
torch.nn.Dropout(0.2),
torch.nn.Linear(512, 1),
)
def forward(self, input_ids, attention_mask):
#(Batch_Size,Sequence Length, Hidden_Size)
outputs_bert = self.portuguese_model(
input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0, :]
#(Batch_Size,Sequence Length, Hidden_Size)
outputs_pos = self.portuguese_pos_tagging_model(
input_ids=input_ids, attention_mask=attention_mask).logits[:, 0, :]
if self.mode == 'horizontal_stacking':
outputs = torch.cat((outputs_bert, outputs_pos), dim=1)
elif self.mode == 'bertimbau_only':
outputs = outputs_bert
elif self.mode == 'pos_only':
outputs = outputs_pos
elif self.mode == 'vertical_sum':
outputs = outputs_bert + outputs_pos
outputs = torch.nn.functional.normalize(outputs, p=2, dim=1)
return self.linear(outputs)
def load_models():
models = []
for domain in ['politics', 'news', 'law', 'social_media', 'literature', 'web']:
logging.info(f"Loading {domain} model...")
model = LanguageIdentifer(mode='pos_only')
model.load_state_dict(torch.load(os.path.join(
CURRENT_PATH, 'models', 'embeddings', f'{domain}.pt')))
models.append({
'model': model,
'train_domain': domain,
})
return models
def benchmark(model, debug=False):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
df_result = pd.DataFrame(
columns=['train_domain', 'test_domain', 'accuracy', 'f1', 'precision', 'recall'])
train_domain = model['train_domain']
model = model['model']
model.to(device)
model.eval()
for test_domain in ['politics', 'news', 'law', 'social_media', 'literature', 'web']:
dataset = load_dataset(
'arubenruben/Portuguese_Language_Identification', test_domain, split='test')
if debug:
logging.info("Debug mode: using only 100 samples")
dataset = dataset.shuffle().select(range(100))
else:
dataset = dataset.shuffle().select(range(min(50_000, len(dataset))))
dataset = tokenize(dataset)
dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
dataset = create_dataloader(dataset)
y = []
with torch.no_grad():
for batch in tqdm(dataset):
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
y.extend(model(input_ids, attention_mask).cpu().detach().numpy())
y = [1 if y_ > 0.5 else 0 for y_ in y]
accuracy = evaluate.load('accuracy').compute(
predictions=y, references=dataset['label'])['accuracy']
f1 = evaluate.load('f1').compute(
predictions=y, references=dataset['label'])['f1']
precision = evaluate.load('precision').compute(
predictions=y, references=dataset['label'])['precision']
recall = evaluate.load('recall').compute(
predictions=y, references=dataset['label'])['recall']
df_result = pd.concat([df_result, pd.DataFrame({
'train_domain': [train_domain],
'test_domain': [test_domain],
'accuracy': [accuracy],
'f1': [f1],
'precision': [precision],
'recall': [recall],
})], ignore_index=True)
return df_result
def test():
DEBUG = False
models = load_models()
df_results = pd.DataFrame(
columns=['train_domain', 'test_domain', 'accuracy', 'f1', 'precision', 'recall'])
for model in models:
logging.info(f"Train Domain {model['train_domain']}...")
df_results = pd.concat(
[df_results, benchmark(model, debug=DEBUG)], ignore_index=True)
logging.info("Saving Results...")
df_results.to_json(os.path.join(CURRENT_PATH, 'out', 'embeddings.json'),
orient='records', indent=4, force_ascii=False)
if __name__ == '__main__':
test() |