lvwerra HF staff commited on
Commit
b05a1ce
1 Parent(s): 0fee27b

Update Space (evaluate main: 9be38a7c)

Browse files
Files changed (2) hide show
  1. README.md +6 -6
  2. perplexity.py +6 -6
README.md CHANGED
@@ -37,13 +37,13 @@ The metric takes a list of text as input, as well as the name of the model used
37
  ```python
38
  from evaluate import load
39
  perplexity = load("perplexity", module_type="metric")
40
- results = perplexity.compute(input_texts=input_texts, model_id='gpt2')
41
  ```
42
 
43
  ### Inputs
44
  - **model_id** (str): model used for calculating Perplexity. NOTE: Perplexity can only be calculated for causal language models.
45
  - This includes models such as gpt2, causal variations of bert, causal versions of t5, and more (the full list can be found in the AutoModelForCausalLM documentation here: https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )
46
- - **input_texts** (list of str): input text, each separate text snippet is one list entry.
47
  - **batch_size** (int): the batch size to run texts through the model. Defaults to 16.
48
  - **add_start_token** (bool): whether to add the start token to the texts, so the perplexity can include the probability of the first word. Defaults to True.
49
  - **device** (str): device to run on, defaults to 'cuda' when available
@@ -62,13 +62,13 @@ This metric's range is 0 and up. A lower score is better.
62
 
63
 
64
  ### Examples
65
- Calculating perplexity on input_texts defined here:
66
  ```python
67
  perplexity = evaluate.load("perplexity", module_type="metric")
68
  input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
69
  results = perplexity.compute(model_id='gpt2',
70
  add_start_token=False,
71
- input_texts=input_texts)
72
  print(list(results.keys()))
73
  >>>['perplexities', 'mean_perplexity']
74
  print(round(results["mean_perplexity"], 2))
@@ -76,7 +76,7 @@ print(round(results["mean_perplexity"], 2))
76
  print(round(results["perplexities"][0], 2))
77
  >>>11.11
78
  ```
79
- Calculating perplexity on input_texts loaded in from a dataset:
80
  ```python
81
  perplexity = evaluate.load("perplexity", module_type="metric")
82
  input_texts = datasets.load_dataset("wikitext",
@@ -84,7 +84,7 @@ input_texts = datasets.load_dataset("wikitext",
84
  split="test")["text"][:50]
85
  input_texts = [s for s in input_texts if s!='']
86
  results = perplexity.compute(model_id='gpt2',
87
- input_texts=input_texts)
88
  print(list(results.keys()))
89
  >>>['perplexities', 'mean_perplexity']
90
  print(round(results["mean_perplexity"], 2))
 
37
  ```python
38
  from evaluate import load
39
  perplexity = load("perplexity", module_type="metric")
40
+ results = perplexity.compute(predictions=predictions, model_id='gpt2')
41
  ```
42
 
43
  ### Inputs
44
  - **model_id** (str): model used for calculating Perplexity. NOTE: Perplexity can only be calculated for causal language models.
45
  - This includes models such as gpt2, causal variations of bert, causal versions of t5, and more (the full list can be found in the AutoModelForCausalLM documentation here: https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )
46
+ - **predictions** (list of str): input text, each separate text snippet is one list entry.
47
  - **batch_size** (int): the batch size to run texts through the model. Defaults to 16.
48
  - **add_start_token** (bool): whether to add the start token to the texts, so the perplexity can include the probability of the first word. Defaults to True.
49
  - **device** (str): device to run on, defaults to 'cuda' when available
 
62
 
63
 
64
  ### Examples
65
+ Calculating perplexity on predictions defined here:
66
  ```python
67
  perplexity = evaluate.load("perplexity", module_type="metric")
68
  input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
69
  results = perplexity.compute(model_id='gpt2',
70
  add_start_token=False,
71
+ predictions=input_texts)
72
  print(list(results.keys()))
73
  >>>['perplexities', 'mean_perplexity']
74
  print(round(results["mean_perplexity"], 2))
 
76
  print(round(results["perplexities"][0], 2))
77
  >>>11.11
78
  ```
79
+ Calculating perplexity on predictions loaded in from a dataset:
80
  ```python
81
  perplexity = evaluate.load("perplexity", module_type="metric")
82
  input_texts = datasets.load_dataset("wikitext",
 
84
  split="test")["text"][:50]
85
  input_texts = [s for s in input_texts if s!='']
86
  results = perplexity.compute(model_id='gpt2',
87
+ predictions=input_texts)
88
  print(list(results.keys()))
89
  >>>['perplexities', 'mean_perplexity']
90
  print(round(results["mean_perplexity"], 2))
perplexity.py CHANGED
@@ -43,7 +43,7 @@ Args:
43
  in the AutoModelForCausalLM documentation here:
44
  https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )
45
 
46
- input_texts (list of str): input text, each separate text snippet
47
  is one list entry.
48
  batch_size (int): the batch size to run texts through the model. Defaults to 16.
49
  add_start_token (bool): whether to add the start token to the texts,
@@ -60,7 +60,7 @@ Examples:
60
  >>> input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
61
  >>> results = perplexity.compute(model_id='gpt2',
62
  ... add_start_token=False,
63
- ... input_texts=input_texts) # doctest:+ELLIPSIS
64
  >>> print(list(results.keys()))
65
  ['perplexities', 'mean_perplexity']
66
  >>> print(round(results["mean_perplexity"], 2))
@@ -74,7 +74,7 @@ Examples:
74
  >>> input_texts = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")["text"][:10] # doctest: +SKIP
75
  >>> input_texts = [s for s in input_texts if s!='']
76
  >>> results = perplexity.compute(model_id='gpt2',
77
- ... input_texts=input_texts)
78
  >>> print(list(results.keys()))
79
  ['perplexities', 'mean_perplexity']
80
  >>> print(round(results["mean_perplexity"], 2)) # doctest: +SKIP
@@ -94,13 +94,13 @@ class Perplexity(evaluate.EvaluationModule):
94
  inputs_description=_KWARGS_DESCRIPTION,
95
  features=datasets.Features(
96
  {
97
- "input_texts": datasets.Value("string"),
98
  }
99
  ),
100
  reference_urls=["https://huggingface.co/docs/transformers/perplexity"],
101
  )
102
 
103
- def _compute(self, input_texts, model_id, batch_size: int = 16, add_start_token: bool = True, device=None):
104
 
105
  if device is not None:
106
  assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu."
@@ -136,7 +136,7 @@ class Perplexity(evaluate.EvaluationModule):
136
  max_tokenized_len = model.config.max_length
137
 
138
  encodings = tokenizer(
139
- input_texts,
140
  add_special_tokens=False,
141
  padding=True,
142
  truncation=True,
 
43
  in the AutoModelForCausalLM documentation here:
44
  https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )
45
 
46
+ predictions (list of str): input text, each separate text snippet
47
  is one list entry.
48
  batch_size (int): the batch size to run texts through the model. Defaults to 16.
49
  add_start_token (bool): whether to add the start token to the texts,
 
60
  >>> input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
61
  >>> results = perplexity.compute(model_id='gpt2',
62
  ... add_start_token=False,
63
+ ... predictions=input_texts) # doctest:+ELLIPSIS
64
  >>> print(list(results.keys()))
65
  ['perplexities', 'mean_perplexity']
66
  >>> print(round(results["mean_perplexity"], 2))
 
74
  >>> input_texts = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")["text"][:10] # doctest: +SKIP
75
  >>> input_texts = [s for s in input_texts if s!='']
76
  >>> results = perplexity.compute(model_id='gpt2',
77
+ ... predictions=input_texts)
78
  >>> print(list(results.keys()))
79
  ['perplexities', 'mean_perplexity']
80
  >>> print(round(results["mean_perplexity"], 2)) # doctest: +SKIP
 
94
  inputs_description=_KWARGS_DESCRIPTION,
95
  features=datasets.Features(
96
  {
97
+ "predictions": datasets.Value("string"),
98
  }
99
  ),
100
  reference_urls=["https://huggingface.co/docs/transformers/perplexity"],
101
  )
102
 
103
+ def _compute(self, predictions, model_id, batch_size: int = 16, add_start_token: bool = True, device=None):
104
 
105
  if device is not None:
106
  assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu."
 
136
  max_tokenized_len = model.config.max_length
137
 
138
  encodings = tokenizer(
139
+ predictions,
140
  add_special_tokens=False,
141
  padding=True,
142
  truncation=True,