Views
No views yet
| Macro-F1 | Recall | Precision |
|---|---|---|
| 0.56 | 0.57 | 0.58 |
| Label | Recall | Precision |
|---|---|---|
| No Label | 0.34 | 0.64 |
| Pleasantness | 0.69 | 0.54 |
| Anticipated Effort | 0.46 | 0.46 |
| Certainty | 0.58 | 0.47 |
| Objective Experience | 0.58 | 0.69 |
| Self-Other Agency | 0.62 | 0.55 |
| Situational Control | 0.31 | 0.55 |
| Advice | 0.72 | 0.66 |
| Trope | 0.80 | 0.67 |
1import torch
2from openprompt.plms import load_plm
3from openprompt.prompts import ManualTemplate
4from openprompt.prompts import ManualVerbalizer
5from openprompt import PromptForClassification
6from openprompt.data_utils import InputExample
7from openprompt import PromptDataLoader
8
9checkpoint_file = 'your_path_to/empathy-appraisal-span.pt'
10
11plm, tokenizer, model_config, WrapperClass = load_plm('roberta', 'roberta-large')
12template_text = 'The sentence {"placeholder":"text_a"} has the label {"mask"}.'
13template = ManualTemplate(tokenizer=tokenizer, text=template_text)
14
15num_classes = 9
16label_words = [['No Label'], ['Pleasantness'], ['Anticipated Effort'], ['Certainty'], ['Objective Experience'], ['Self-Other Agency'], ['Situational Control'], ['Advice'], ['Trope']]
17verbalizer = ManualVerbalizer(tokenizer, num_classes=num_classes, label_words=label_words)
18prompt_model = PromptForClassification(plm=plm,template=template, verbalizer=verbalizer, freeze_plm=False).to('cuda')
19
20checkpoint = torch.load(checkpoint_file)
21state_dict = checkpoint['model_state_dict']
22
23# depend on the version of torch
24del state_dict['prompt_model.plm.roberta.embeddings.position_ids']
25
26prompt_model.load_state_dict(state_dict)
27
28# use the model
29dataset = [
30 InputExample(
31 guid = 0,
32 text_a = "I am sorry for your loss",
33 ),
34 InputExample(
35 guid = 1,
36 text_a = "It's not your fault",
37 ),
38]
39
40data_loader = PromptDataLoader(dataset=dataset,
41 template=template,
42 tokenizer=tokenizer,
43 tokenizer_wrapper_class=WrapperClass,
44 max_seq_length=512,
45 batch_size=2,
46 shuffle=False,
47 teacher_forcing=False,
48 predict_eos_token=False,
49 truncate_method='head')
50prompt_model.eval()
51with torch.no_grad():
52 for batch in data_loader:
53 logits = prompt_model(batch.to('cuda'))
54 preds = torch.argmax(logits, dim = -1)
55 print(preds) #[8, 5]