Views
No views yet
1from transformers import MT5Tokenizer, GPT2Model
2import torch
3from huggingface_hub import hf_hub_download
4
5class ModelNSP(torch.nn.Module):
6 def __init__(self, pretrained_model="THUMT/mGPT"):
7 super(ModelNSP, self).__init__()
8 self.core_model = GPT2Model.from_pretrained(pretrained_model)
9 self.nsp_head = torch.nn.Sequential(torch.nn.Linear(self.core_model.config.hidden_size, 300),
10 torch.nn.Linear(300, 300), torch.nn.Linear(300, 2))
11
12 def forward(self, input_ids, attention_mask=None):
13 return self.nsp_head(self.core_model(input_ids, attention_mask=attention_mask)[0].mean(dim=1)).softmax(dim=-1)
14
15model = torch.nn.DataParallel(ModelNSP().eval())
16model.load_state_dict(torch.load(hf_hub_download(repo_id="tolga-ozturk/mGPT-nsp", filename="model_weights.bin")))
17tokenizer = MT5Tokenizer.from_pretrained("tolga-ozturk/mGPT-nsp")1batch_texts = [("In Italy, pizza is presented unsliced.", "The sky is blue."),
2 ("In Italy, pizza is presented unsliced.", "However, it is served sliced in Turkey.")]
3encoded_dict = tokenizer.batch_encode_plus(batch_text_or_text_pairs=batch_texts, truncation="longest_first",padding=True, return_tensors="pt", return_attention_mask=True, max_length=256)
4print(torch.argmax(model(encoded_dict.input_ids, attention_mask=encoded_dict.attention_mask), dim=-1))
1@misc{title={How Different Is Stereotypical Bias Across Languages?},
2 author={Ibrahim Tolga Öztürk and Rostislav Nedelchev and Christian Heumann and Esteban Garces Arias and Marius Roger and Bernd Bischl and Matthias Aßenmacher},
3 year={2023},
4 eprint={2307.07331},
5 archivePrefix={arXiv},
6 primaryClass={cs.CL}
7}