Text Generation
Transformers
PyTorch
Safetensors
xglm
VictoriaLinML commited on
Commit
19523cf
·
1 Parent(s): 405ac35

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +93 -1
README.md CHANGED
@@ -10,4 +10,96 @@ XGLM-4.5B is a multilingual autoregressive language model (with 4.5 billion para
10
 
11
  ## Model card
12
 
13
- For intended usage of the model, please refer to the [model card](https://github.com/pytorch/fairseq/blob/main/examples/xglm/model_card.md) released by the XGLM-4.5B development team.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  ## Model card
12
 
13
+ For intended usage of the model, please refer to the [model card](https://github.com/pytorch/fairseq/blob/main/examples/xglm/model_card.md) released by the XGLM-4.5B development team.
14
+
15
+ ## Example (COPA)
16
+ The following snippet shows how to evaluate our models (GPT-3 style, zero-shot) on the Choice of Plausible Alternatives (COPA) task, using examples in English, Chinese and Hindi.
17
+
18
+ ```python
19
+ import torch
20
+ import torch.nn.functional as F
21
+
22
+ from transformers import XGLMTokenizer, XGLMForCausalLM
23
+
24
+ tokenizer = XGLMTokenizer.from_pretrained("facebook/xglm-4.5B")
25
+ model = XGLMForCausalLM.from_pretrained("facebook/xglm-4.5B")
26
+
27
+ data_samples = {
28
+ 'en': [
29
+ {
30
+ "premise": "I wanted to conserve energy.",
31
+ "choice1": "I swept the floor in the unoccupied room.",
32
+ "choice2": "I shut off the light in the unoccupied room.",
33
+ "question": "effect",
34
+ "label": "1"
35
+ },
36
+ {
37
+ "premise": "The flame on the candle went out.",
38
+ "choice1": "I blew on the wick.",
39
+ "choice2": "I put a match to the wick.",
40
+ "question": "cause",
41
+ "label": "0"
42
+ }
43
+ ],
44
+ 'zh': [
45
+ {
46
+ "premise": "我想节约能源。",
47
+ "choice1": "我在空着的房间里扫了地板。",
48
+ "choice2": "我把空房间里的灯关了。",
49
+ "question": "effect",
50
+ "label": "1"
51
+ },
52
+ {
53
+ "premise": "蜡烛上的火焰熄灭了。",
54
+ "choice1": "我吹灭了灯芯。",
55
+ "choice2": "我把一根火柴放在灯芯上。",
56
+ "question": "cause",
57
+ "label": "0"
58
+ }
59
+ ],
60
+ 'hi': [
61
+ {
62
+ "premise": "M te vle konsève enèji.",
63
+ "choice1": "Mwen te fin baleye chanm lib la.",
64
+ "choice2": "Mwen te femen limyè nan chanm lib la.",
65
+ "question": "effect",
66
+ "label": "1"
67
+ },
68
+ {
69
+ "premise": "Flam bouji a te etenn.",
70
+ "choice1": "Mwen te soufle bouji a.",
71
+ "choice2": "Mwen te limen mèch bouji a.",
72
+ "question": "cause",
73
+ "label": "0"
74
+ }
75
+ ]
76
+ }
77
+
78
+ def get_logprobs(prompt):
79
+ inputs = tokenizer(prompt, return_tensors="pt")
80
+ input_ids, output_ids = inputs["input_ids"], inputs["input_ids"][:, 1:]
81
+ outputs = model(**inputs, labels=input_ids)
82
+ logits = outputs.logits
83
+ logprobs = torch.gather(F.log_softmax(logits, dim=2), 2, output_ids.unsqueeze(2))
84
+ return logprobs
85
+
86
+ # Zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task.
87
+ # A return value of 0 indicates that the first alternative is more plausible,
88
+ # while 1 indicates that the second alternative is more plausible.
89
+ def COPA_eval(prompt, alternative1, alternative2):
90
+ lprob1 = get_logprobs(prompt + "\n" + alternative1).sum()
91
+ lprob2 = get_logprobs(prompt + "\n" + alternative2).sum()
92
+ return 0 if lprob1 > lprob2 else 1
93
+
94
+ for lang in data_samples_long:
95
+ for idx, example in enumerate(data_samples_long[lang]):
96
+ predict = COPA_eval(example["premise"], example["choice1"], example["choice2"])
97
+ print(f'{lang}-{idx}', predict, example['label'])
98
+
99
+ # en-0 1 1
100
+ # en-1 0 0
101
+ # zh-0 1 1
102
+ # zh-1 0 0
103
+ # hi-0 1 1
104
+ # hi-1 0 0
105
+ ```