cyrilzhang commited on
Commit
dbf2014
1 Parent(s): 7945e24

rename classes

Browse files
Files changed (1) hide show
  1. automata.py +37 -36
automata.py CHANGED
@@ -32,10 +32,16 @@ version = major + 0.1*minor
32
  OLD_PY_VERSION = 1 if version < 3.8 else 0
33
 
34
  _CITATION = """\
 
 
 
 
 
 
35
  """
36
 
37
  _DESCRIPTION = """\
38
- Online dataset mockup.
39
  """
40
 
41
  _HOMEPAGE = ""
@@ -44,7 +50,7 @@ _LICENSE = ""
44
 
45
  _URLS = {}
46
 
47
- class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
48
  """TODO: Short description of my dataset."""
49
 
50
  VERSION = datasets.Version("0.0.0")
@@ -64,7 +70,7 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
64
  config['size'] = -1
65
 
66
  self.data_config = config
67
- self.sampler = dataset_map[config['name']](config)
68
 
69
  def _info(self):
70
  features = datasets.Features(
@@ -96,14 +102,13 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
96
  for i in itertools.count(start=0):
97
  if i == self.data_config['size']:
98
  break
99
- x, y = self.sampler.sample()
100
  yield i, {
101
  "input_ids": x,
102
  "label_ids": y
103
  }
104
 
105
-
106
- class AutomatonSampler:
107
  """
108
  This is a parent class that must be inherited.
109
  """
@@ -144,13 +149,10 @@ class AutomatonSampler:
144
  def help(self):
145
  print(self.__info__)
146
 
147
-
148
-
149
-
150
- class BinaryInputSampler(AutomatonSampler):
151
  """
152
  This is a parent class that must be inherited.
153
- Subclasses: ParitySampler, GridworldSampler, ABABSampler
154
 
155
  TODO: sample sequences with a given number of 1s
156
  """
@@ -171,7 +173,7 @@ class BinaryInputSampler(AutomatonSampler):
171
  x = self.np_rng.binomial(1, self.prob1, size=T)
172
  return x, self.f(x)
173
 
174
- class ParitySampler(BinaryInputSampler):
175
  def __init__(self, data_config):
176
  super().__init__(data_config)
177
  self.name = 'parity'
@@ -185,7 +187,7 @@ class ParitySampler(BinaryInputSampler):
185
  def f(self, x):
186
  return np.cumsum(x) % 2
187
 
188
- class GridworldSampler(BinaryInputSampler):
189
  """
190
  Note: gridworld currently doesn't include a no-op.
191
  """
@@ -234,7 +236,7 @@ class GridworldSampler(BinaryInputSampler):
234
  return np.array(states).astype(np.int64)
235
 
236
 
237
- class ABABSampler(BinaryInputSampler):
238
  def __init__(self, data_config):
239
  super().__init__(data_config)
240
  self.name = 'abab'
@@ -290,7 +292,7 @@ class ABABSampler(BinaryInputSampler):
290
  return super().sample()
291
 
292
 
293
- class AdderSampler(BinaryInputSampler):
294
  def __init__(self, data_config):
295
  super().__init__(data_config)
296
  self.name = 'addition'
@@ -351,7 +353,7 @@ class AdderSampler(BinaryInputSampler):
351
  return x_encode, self.f(x)
352
 
353
 
354
- class FlipFlopSampler(AutomatonSampler):
355
  def __init__(self, data_config):
356
  super().__init__(data_config)
357
  self.name = 'flipflop'
@@ -388,10 +390,10 @@ class FlipFlopSampler(AutomatonSampler):
388
 
389
 
390
 
391
- class PermutationSampler(AutomatonSampler):
392
  """
393
  This is a parent class that must be inherited.
394
- Subclasses: SymmetricSampler, AlternatingSampler
395
  """
396
  def __init__(self, data_config):
397
  super().__init__(data_config)
@@ -436,7 +438,7 @@ class PermutationSampler(AutomatonSampler):
436
  return x, self.f(x)
437
 
438
 
439
- class SymmetricSampler(PermutationSampler):
440
  """
441
  TODO: add options for labels as functions of states
442
  - parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
@@ -494,7 +496,7 @@ class SymmetricSampler(PermutationSampler):
494
  + self.__info__
495
 
496
 
497
- class AlternatingSampler(PermutationSampler):
498
  """
499
  TODO: other choices of generators (currently using (12x))?
500
  """
@@ -537,7 +539,7 @@ class AlternatingSampler(PermutationSampler):
537
 
538
 
539
 
540
- class CyclicSampler(AutomatonSampler):
541
  def __init__(self, data_config):
542
  super().__init__(data_config)
543
 
@@ -575,7 +577,7 @@ class CyclicSampler(AutomatonSampler):
575
  return x, self.f(x)
576
 
577
 
578
- class DihedralSampler(AutomatonSampler):
579
  def __init__(self, data_config):
580
  super().__init__(data_config)
581
 
@@ -665,7 +667,7 @@ class DihedralSampler(AutomatonSampler):
665
 
666
 
667
 
668
- class QuaternionSampler(AutomatonSampler):
669
  def __init__(self, data_config):
670
  super().__init__(data_config)
671
 
@@ -700,7 +702,7 @@ class QuaternionSampler(AutomatonSampler):
700
  return x, self.f(x)
701
 
702
 
703
- class PermutationResetSampler(AutomatonSampler):
704
  def __init__(self, data_config):
705
  super().__init__(data_config)
706
 
@@ -751,17 +753,16 @@ class PermutationResetSampler(AutomatonSampler):
751
 
752
 
753
  dataset_map = {
754
- 'abab': ABABSampler,
755
- 'add': AdderSampler,
756
- 'alternating': AlternatingSampler,
757
- 'cyclic': CyclicSampler,
758
- 'dihedral': DihedralSampler,
759
- 'flipflop': FlipFlopSampler,
760
- 'gridworld': GridworldSampler,
761
- 'parity': ParitySampler,
762
- 'quaternion': QuaternionSampler,
763
- 'symmetric': SymmetricSampler,
764
- 'permutation_reset': PermutationResetSampler
765
  # TODO: add Dyck
766
  }
767
-
 
32
  OLD_PY_VERSION = 1 if version < 3.8 else 0
33
 
34
  _CITATION = """\
35
+ @article{liu2022transformers,
36
+ title={Transformers learn shortcuts to automata},
37
+ author={Liu, Bingbin and Ash, Jordan T and Goel, Surbhi and Krishnamurthy, Akshay and Zhang, Cyril},
38
+ journal={arXiv preprint arXiv:2210.10749},
39
+ year={2022}
40
+ }
41
  """
42
 
43
  _DESCRIPTION = """\
44
+ Non-autoregressive automaton simulation datasets.
45
  """
46
 
47
  _HOMEPAGE = ""
 
50
 
51
  _URLS = {}
52
 
53
+ class AutomatonDataset(datasets.GeneratorBasedBuilder):
54
  """TODO: Short description of my dataset."""
55
 
56
  VERSION = datasets.Version("0.0.0")
 
70
  config['size'] = -1
71
 
72
  self.data_config = config
73
+ self.automaton = dataset_map[config['name']](config)
74
 
75
  def _info(self):
76
  features = datasets.Features(
 
102
  for i in itertools.count(start=0):
103
  if i == self.data_config['size']:
104
  break
105
+ x, y = self.automaton.sample()
106
  yield i, {
107
  "input_ids": x,
108
  "label_ids": y
109
  }
110
 
111
+ class Automaton:
 
112
  """
113
  This is a parent class that must be inherited.
114
  """
 
149
  def help(self):
150
  print(self.__info__)
151
 
152
+ class BinaryInputAutomaton(Automaton):
 
 
 
153
  """
154
  This is a parent class that must be inherited.
155
+ Subclasses: ParityAutomaton, GridworldAutomaton, ABABAutomaton
156
 
157
  TODO: sample sequences with a given number of 1s
158
  """
 
173
  x = self.np_rng.binomial(1, self.prob1, size=T)
174
  return x, self.f(x)
175
 
176
+ class ParityAutomaton(BinaryInputAutomaton):
177
  def __init__(self, data_config):
178
  super().__init__(data_config)
179
  self.name = 'parity'
 
187
  def f(self, x):
188
  return np.cumsum(x) % 2
189
 
190
+ class GridworldAutomaton(BinaryInputAutomaton):
191
  """
192
  Note: gridworld currently doesn't include a no-op.
193
  """
 
236
  return np.array(states).astype(np.int64)
237
 
238
 
239
+ class ABABAutomaton(BinaryInputAutomaton):
240
  def __init__(self, data_config):
241
  super().__init__(data_config)
242
  self.name = 'abab'
 
292
  return super().sample()
293
 
294
 
295
+ class AdderAutomaton(BinaryInputAutomaton):
296
  def __init__(self, data_config):
297
  super().__init__(data_config)
298
  self.name = 'addition'
 
353
  return x_encode, self.f(x)
354
 
355
 
356
+ class FlipFlopAutomaton(Automaton):
357
  def __init__(self, data_config):
358
  super().__init__(data_config)
359
  self.name = 'flipflop'
 
390
 
391
 
392
 
393
+ class PermutationAutomaton(Automaton):
394
  """
395
  This is a parent class that must be inherited.
396
+ Subclasses: SymmetricAutomaton, AlternatingAutomaton
397
  """
398
  def __init__(self, data_config):
399
  super().__init__(data_config)
 
438
  return x, self.f(x)
439
 
440
 
441
+ class SymmetricAutomaton(PermutationAutomaton):
442
  """
443
  TODO: add options for labels as functions of states
444
  - parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
 
496
  + self.__info__
497
 
498
 
499
+ class AlternatingAutomaton(PermutationAutomaton):
500
  """
501
  TODO: other choices of generators (currently using (12x))?
502
  """
 
539
 
540
 
541
 
542
+ class CyclicAutomaton(Automaton):
543
  def __init__(self, data_config):
544
  super().__init__(data_config)
545
 
 
577
  return x, self.f(x)
578
 
579
 
580
+ class DihedralAutomaton(Automaton):
581
  def __init__(self, data_config):
582
  super().__init__(data_config)
583
 
 
667
 
668
 
669
 
670
+ class QuaternionAutomaton(Automaton):
671
  def __init__(self, data_config):
672
  super().__init__(data_config)
673
 
 
702
  return x, self.f(x)
703
 
704
 
705
+ class PermutationResetAutomaton(Automaton):
706
  def __init__(self, data_config):
707
  super().__init__(data_config)
708
 
 
753
 
754
 
755
  dataset_map = {
756
+ 'abab': ABABAutomaton,
757
+ 'add': AdderAutomaton,
758
+ 'alternating': AlternatingAutomaton,
759
+ 'cyclic': CyclicAutomaton,
760
+ 'dihedral': DihedralAutomaton,
761
+ 'flipflop': FlipFlopAutomaton,
762
+ 'gridworld': GridworldAutomaton,
763
+ 'parity': ParityAutomaton,
764
+ 'quaternion': QuaternionAutomaton,
765
+ 'symmetric': SymmetricAutomaton,
766
+ 'permutation_reset': PermutationResetAutomaton
767
  # TODO: add Dyck
768
  }