Datasets:

Modalities:
Text
Libraries:
Datasets
Jon Gauthier commited on
Commit
7e951ff
·
1 Parent(s): 2b2f744

working example with half-implemented evaluation

Browse files
Files changed (3) hide show
  1. syntaxgym/prediction.py +8 -1
  2. syntaxgym/syntaxgym.py +51 -6
  3. test.py +12 -0
syntaxgym/prediction.py CHANGED
@@ -4,7 +4,14 @@ from typing import Union, Optional as TOptional, List as TList
4
  from pyparsing import *
5
  import numpy as np
6
 
7
- from syntaxgym.utils import METRICS
 
 
 
 
 
 
 
8
 
9
 
10
  # Enable parser packrat (caching)
 
4
  from pyparsing import *
5
  import numpy as np
6
 
7
+ METRICS = {
8
+ 'sum': sum,
9
+ 'mean': np.mean,
10
+ 'median': np.median,
11
+ 'range': np.ptp,
12
+ 'max': max,
13
+ 'min': min
14
+ }
15
 
16
 
17
  # Enable parser packrat (caching)
syntaxgym/syntaxgym.py CHANGED
@@ -5,12 +5,16 @@ SyntaxGym dataset as used in Hu et al. (2020).
5
  """
6
 
7
 
 
8
  import json
9
  from pathlib import Path
 
10
  from typing import List
11
 
12
  import datasets
13
 
 
 
14
 
15
  _CITATION = """
16
  @inproceedings{Hu:et-al:2020,
@@ -28,11 +32,11 @@ _PROJECT_URL = "https://syntaxgym.org"
28
  _DOWNLOAD_URL = "https://github.com/cpllab/syntactic-generalization"
29
 
30
 
31
- print("_____HERE")
32
  SUITE_JSONS = []
33
  for suite_f in Path("test_suites").glob("*.json"):
34
  with suite_f.open() as f:
35
  SUITE_JSONS.append(json.load(f))
 
36
 
37
 
38
  class SyntaxGymSuiteConfig(datasets.BuilderConfig):
@@ -51,11 +55,12 @@ class SyntaxGymSuiteConfig(datasets.BuilderConfig):
51
  class SyntaxGym(datasets.GeneratorBasedBuilder):
52
 
53
  BUILDER_CONFIGS = [SyntaxGymSuiteConfig(suite_json)
54
- for suite_json in SUITE_JSONS]
55
 
56
  def _info(self):
57
  condition_spec = {
58
  "condition_name": datasets.Value("string"),
 
59
  "regions": datasets.Sequence({
60
  "region_number": datasets.Value("int32"),
61
  "content": datasets.Value("string")
@@ -85,10 +90,50 @@ class SyntaxGym(datasets.GeneratorBasedBuilder):
85
 
86
  def _generate_examples(self, name):
87
  # DEV: NB suite jsons already loaded because BUILDER_CONFIGS is static
88
- suite_jsons = SUITE_JSONS
89
-
90
- suite_json = next(suite for suite in SUITE_JSONS
91
- if suite["meta"]["name"] == name)
92
 
93
  for item in suite_json["items"]:
 
 
 
 
 
 
 
94
  yield item["item_number"], item
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  """
6
 
7
 
8
+ from copy import deepcopy
9
  import json
10
  from pathlib import Path
11
+ import re
12
  from typing import List
13
 
14
  import datasets
15
 
16
+ from syntaxgym.prediction import Prediction
17
+
18
 
19
  _CITATION = """
20
  @inproceedings{Hu:et-al:2020,
 
32
  _DOWNLOAD_URL = "https://github.com/cpllab/syntactic-generalization"
33
 
34
 
 
35
  SUITE_JSONS = []
36
  for suite_f in Path("test_suites").glob("*.json"):
37
  with suite_f.open() as f:
38
  SUITE_JSONS.append(json.load(f))
39
+ SUITE_JSONS = {suite["meta"]["name"]: suite for suite in SUITE_JSONS}
40
 
41
 
42
  class SyntaxGymSuiteConfig(datasets.BuilderConfig):
 
55
  class SyntaxGym(datasets.GeneratorBasedBuilder):
56
 
57
  BUILDER_CONFIGS = [SyntaxGymSuiteConfig(suite_json)
58
+ for suite_json in SUITE_JSONS.values()]
59
 
60
  def _info(self):
61
  condition_spec = {
62
  "condition_name": datasets.Value("string"),
63
+ "content": datasets.Value("string"),
64
  "regions": datasets.Sequence({
65
  "region_number": datasets.Value("int32"),
66
  "content": datasets.Value("string")
 
90
 
91
  def _generate_examples(self, name):
92
  # DEV: NB suite jsons already loaded because BUILDER_CONFIGS is static
93
+ suite_json = SUITE_JSONS[name]
 
 
 
94
 
95
  for item in suite_json["items"]:
96
+ # Convert to sentence input.
97
+ for cond in item["conditions"]:
98
+ cond["content"] = " ".join([region["content"].lstrip()
99
+ for region in cond["regions"]
100
+ if region["content"].strip() != ""])
101
+ cond["content"] = re.sub(r"\s+,", ",", cond["content"])
102
+
103
  yield item["item_number"], item
104
+
105
+
106
+ class SyntaxGymMetric(datasets.Metric):
107
+ """
108
+ SyntaxGym prediction evaluation metric.
109
+ """
110
+
111
+ def __init__(self, *args, **kwargs):
112
+ super().__init__(*args, **kwargs)
113
+ self.suite = SUITE_JSONS[self.config_name]
114
+ self.predictions = [
115
+ Prediction(idx, p["formula"], "sum")
116
+ for idx, p in enumerate(self.suite["predictions"])
117
+ ]
118
+
119
+ def _info(self):
120
+ features = datasets.Features({
121
+ "conditions": datasets.Sequence({
122
+ "condition_name": datasets.Value("string"),
123
+ "regions": datasets.Sequence({
124
+ "region_number": datasets.Value("int32"),
125
+ "metric_value": {
126
+ "sum": datasets.Value("float32")
127
+ },
128
+ }),
129
+ })
130
+ })
131
+ return datasets.MetricInfo(
132
+ description="TODO",
133
+ citation=_CITATION,
134
+ inputs_description="TODO",
135
+ features=features,
136
+ )
137
+
138
+ def _compute(self, region_surprisals):
139
+ suite_with_results = deepcopy(self.suite)
test.py CHANGED
@@ -1,4 +1,16 @@
1
  import datasets
 
2
 
3
 
4
  dataset = datasets.load_dataset("syntaxgym", "mvrr_mod")
 
 
 
 
 
 
 
 
 
 
 
 
1
  import datasets
2
+ import transformers
3
 
4
 
5
  dataset = datasets.load_dataset("syntaxgym", "mvrr_mod")
6
+ metric = datasets.load_metric("syntaxgym", "mvrr_mod")
7
+
8
+ model = transformers.AutoModelForCausalLM.from_pretrained("gpt2")
9
+
10
+
11
+ for item in dataset["test"]:
12
+ # TODO evaluate surprisals
13
+
14
+ print(item)
15
+
16
+ break