Spaces:
Running
Running
File size: 7,180 Bytes
9df4cc0 |
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 |
import warnings
warnings.filterwarnings("ignore")
from sklearn.metrics import accuracy_score,f1_score
from datasets import load_dataset, load_from_disk, Dataset
from tqdm import tqdm
import datasets
import torch
from torch.utils.data import DataLoader
from functools import partial
from pathlib import Path
dic = {
0:"negative",
1:'neutral',
2:'positive',
}
with open(Path(__file__).parent / 'sentiment_templates.txt') as f:
templates = [l.strip() for l in f.readlines()]
def format_example(example: dict) -> dict:
context = f"Instruction: {example['instruction']}\n"
if example.get("input"):
context += f"Input: {example['input']}\n"
context += "Answer: "
target = example["output"]
return {"context": context, "target": target}
def change_target(x):
if 'positive' in x or 'Positive' in x:
return 'positive'
elif 'negative' in x or 'Negative' in x:
return 'negative'
else:
return 'neutral'
def vote_output(x):
output_dict = {'positive': 0, 'negative': 0, 'neutral': 0}
for i in range(len(templates)):
pred = change_target(x[f'out_text_{i}'].lower())
output_dict[pred] += 1
if output_dict['positive'] > output_dict['negative']:
return 'positive'
elif output_dict['negative'] > output_dict['positive']:
return 'negative'
else:
return 'neutral'
def test_fpb(args, model, tokenizer, prompt_fun=None):
batch_size = args.batch_size
# instructions = load_dataset("financial_phrasebank", "sentences_50agree")
instructions = load_from_disk(Path(__file__).parent.parent / "data/financial_phrasebank-sentences_50agree/")
instructions = instructions["train"]
instructions = instructions.train_test_split(seed = 42)['test']
instructions = instructions.to_pandas()
instructions.columns = ["input", "output"]
instructions["output"] = instructions["output"].apply(lambda x:dic[x])
if prompt_fun is None:
instructions["instruction"] = "What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}."
else:
instructions["instruction"] = instructions.apply(prompt_fun, axis = 1)
instructions[["context","target"]] = instructions.apply(format_example, axis = 1, result_type="expand")
# print example
print(f"\n\nPrompt example:\n{instructions['context'][0]}\n\n")
context = instructions['context'].tolist()
total_steps = instructions.shape[0]//batch_size + 1
print(f"Total len: {len(context)}. Batchsize: {batch_size}. Total steps: {total_steps}")
out_text_list = []
for i in tqdm(range(total_steps)):
tmp_context = context[i* batch_size:(i+1)* batch_size]
tokens = tokenizer(tmp_context, return_tensors='pt', padding=True, max_length=512, return_token_type_ids=False)
for k in tokens.keys():
tokens[k] = tokens[k].cuda()
res = model.generate(**tokens, max_length=512, eos_token_id=tokenizer.eos_token_id)
res_sentences = [tokenizer.decode(i, skip_special_tokens=True) for i in res]
# print(f'{i}: {res_sentences[0]}')
out_text = [o.split("Answer: ")[1] for o in res_sentences]
out_text_list += out_text
torch.cuda.empty_cache()
instructions["out_text"] = out_text_list
instructions["new_target"] = instructions["target"].apply(change_target)
instructions["new_out"] = instructions["out_text"].apply(change_target)
acc = accuracy_score(instructions["new_target"], instructions["new_out"])
f1_macro = f1_score(instructions["new_target"], instructions["new_out"], average = "macro")
f1_micro = f1_score(instructions["new_target"], instructions["new_out"], average = "micro")
f1_weighted = f1_score(instructions["new_target"], instructions["new_out"], average = "weighted")
print(f"Acc: {acc}. F1 macro: {f1_macro}. F1 micro: {f1_micro}. F1 weighted (BloombergGPT): {f1_weighted}. ")
return instructions
def test_fpb_mlt(args, model, tokenizer):
batch_size = args.batch_size
# dataset = load_dataset("financial_phrasebank", "sentences_50agree")
dataset = load_from_disk(Path(__file__).parent.parent / 'data/financial_phrasebank-sentences_50agree/')
dataset = dataset["train"]#.select(range(300))
dataset = dataset.train_test_split(seed=42)['test']
dataset = dataset.to_pandas()
dataset.columns = ["input", "output"]
dataset["output"] = dataset["output"].apply(lambda x: dic[x])
dataset["text_type"] = dataset.apply(lambda x: 'news', axis=1)
dataset["output"] = dataset["output"].apply(change_target)
dataset = dataset[dataset["output"] != 'neutral']
out_texts_list = [[] for _ in range(len(templates))]
def collate_fn(batch):
inputs = tokenizer(
[f["context"] for f in batch], return_tensors='pt',
padding=True, max_length=args.max_length,
return_token_type_ids=False
)
return inputs
for i, template in enumerate(templates):
dataset = dataset[['input', 'output', "text_type"]]
dataset["instruction"] = dataset['text_type'].apply(lambda x: template.format(type=x) + "\nOptions: positive, negative")
# dataset["instruction"] = dataset['text_type'].apply(lambda x: template.format(type=x) + "\nOptions: negative, positive")
dataset[["context", "target"]] = dataset.apply(format_example, axis=1, result_type="expand")
dataloader = DataLoader(Dataset.from_pandas(dataset), batch_size=args.batch_size, collate_fn=collate_fn, shuffle=False)
log_interval = len(dataloader) // 5
for idx, inputs in enumerate(tqdm(dataloader)):
inputs = {key: value.to(model.device) for key, value in inputs.items()}
res = model.generate(**inputs, do_sample=False, max_length=args.max_length, eos_token_id=tokenizer.eos_token_id, max_new_tokens=10)
res_sentences = [tokenizer.decode(i, skip_special_tokens=True) for i in res]
tqdm.write(f'{idx}: {res_sentences[0]}')
# if (idx + 1) % log_interval == 0:
# tqdm.write(f'{idx}: {res_sentences[0]}')
out_text = [o.split("Answer: ")[1] for o in res_sentences]
out_texts_list[i] += out_text
torch.cuda.empty_cache()
for i in range(len(templates)):
dataset[f"out_text_{i}"] = out_texts_list[i]
dataset[f"out_text_{i}"] = dataset[f"out_text_{i}"].apply(change_target)
dataset["new_out"] = dataset.apply(vote_output, axis=1, result_type="expand")
dataset.to_csv('tmp.csv')
for k in [f"out_text_{i}" for i in range(len(templates))] + ["new_out"]:
acc = accuracy_score(dataset["target"], dataset[k])
f1_macro = f1_score(dataset["target"], dataset[k], average="macro")
f1_micro = f1_score(dataset["target"], dataset[k], average="micro")
f1_weighted = f1_score(dataset["target"], dataset[k], average="weighted")
print(f"Acc: {acc}. F1 macro: {f1_macro}. F1 micro: {f1_micro}. F1 weighted (BloombergGPT): {f1_weighted}. ")
return dataset |