import torch import datasets import gradio import pandas from transformers import GPT2LMHeadModel, GPT2TokenizerFast class CrowSPairsDataset(object): def __init__(self): super().__init__() self.df = (datasets .load_dataset("BigScienceBiasEval/crows_pairs_multilingual")["test"] .to_pandas() .query('stereo_antistereo == "stereo"') .drop(columns="stereo_antistereo") ) def sample(self, bias_type, n=10): return self.df[self.df["bias_type"] == bias_type].sample(n=n) def bias_types(self): return self.df.bias_type.unique().tolist() def run(df): result = "
index | more stereotypical | gpt2 regular | gpt2 debiased | less stereotypical | ||
---|---|---|---|---|---|---|
{i} | {row['sent_more']} | " more = row["sent_more"] more = tokenizer(more, return_tensors="pt")["input_ids"].to(device) with torch.no_grad(): out_more_gpt = model_gpt(more, labels=more.clone()) out_more_custom = model_custom(more, labels=more.clone()) score_more_gpt = out_more_gpt["loss"] score_more_custom = out_more_custom["loss"] perplexity_more_gpt = torch.exp(score_more_gpt).item() perplexity_more_custom = torch.exp(score_more_custom).item() less = row["sent_less"] less = tokenizer(less, return_tensors="pt")["input_ids"].to(device) with torch.no_grad(): out_less_gpt = model_gpt(less, labels=less.clone()) out_less_custom = model_custom(less, labels=less.clone()) score_less_gpt = out_less_gpt["loss"] score_less_custom = out_less_custom["loss"] perplexity_less_gpt = torch.exp(score_less_gpt).item() perplexity_less_custom = torch.exp(score_less_custom).item() if perplexity_more_gpt > perplexity_less_gpt: diff = round( abs((perplexity_more_gpt - perplexity_less_gpt) / perplexity_more_gpt), 2 ) shade = (diff + 0.2) / 1.2 result += f"{diff:.2f} | " else: diff = abs((perplexity_less_gpt - perplexity_more_gpt) / perplexity_less_gpt) shade = (diff + 0.2) / 1.2 result += f"{diff:.2f} | " if perplexity_more_custom > perplexity_less_custom: diff = round( abs((perplexity_more_custom - perplexity_less_custom) / perplexity_more_custom), 2 ) shade = (diff + 0.2) / 1.2 result += f"{diff:.2f} | " else: diff = abs((perplexity_less_custom - perplexity_more_custom) / perplexity_less_custom) shade = (diff + 0.2) / 1.2 result += f"{diff:.2f} | " result += f"{row['sent_less']} |