ClaraBing commited on
Commit
d262e6f
1 Parent(s): aca0f6d

add gridworld (and a parent class 'BinaryInputSampler')

Browse files
Files changed (1) hide show
  1. automata.py +58 -11
automata.py CHANGED
@@ -21,6 +21,13 @@ import itertools
21
 
22
  import datasets
23
  import numpy as np
 
 
 
 
 
 
 
24
 
25
  # Local imports
26
  # from symmetric import SymmetricSampler
@@ -52,9 +59,9 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
52
  """
53
  if 'name' not in config:
54
  config['name'] = 'parity'
55
- if 'length' not in config:
56
  config['length'] = 20
57
- if 'size' not in config:
58
  config['size'] = -1
59
 
60
  self.data_config = config
@@ -119,25 +126,65 @@ class AutomatonSampler:
119
  raise NotImplementedError()
120
 
121
 
122
- class ParitySampler(AutomatonSampler):
123
  def __init__(self, data_config):
124
  super().__init__(data_config)
125
- self.name = 'parity'
126
- self.data_config = data_config
 
 
127
 
128
  def f(self, x):
129
- return np.cumsum(x) % 2
130
 
131
  def sample(self):
132
- x = self.np_rng.binomial(1,0.5,size=self.T)
133
  return x, self.f(x)
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  class FlipFlopSampler(AutomatonSampler):
137
  def __init__(self, data_config):
138
  super().__init__(data_config)
139
  self.name = 'flipflop'
140
- self.data_config = data_config
141
 
142
  if 'n' not in data_config:
143
  data_config['n'] = 2
@@ -170,7 +217,6 @@ class SymmetricSampler(AutomatonSampler):
170
  def __init__(self, data_config):
171
  super().__init__(data_config)
172
  self.name = 'symmetric'
173
- self.data_config = data_config
174
 
175
  if 'n' not in data_config:
176
  data_config['n'] = 5 # Default to S5
@@ -180,7 +226,7 @@ class SymmetricSampler(AutomatonSampler):
180
  # Options: 'state', 'first_chair'
181
  data_config['label_type'] = 'state'
182
 
183
- self.n = data_config['n']
184
  self.label_type = data_config['label_type']
185
 
186
  """
@@ -242,8 +288,9 @@ class SymmetricSampler(AutomatonSampler):
242
 
243
 
244
  dataset_map = {
245
- 'parity': ParitySampler,
246
  'flipflop': FlipFlopSampler,
 
247
  'symmetric': SymmetricSampler,
248
  # TODO: more datasets
249
  }
 
21
 
22
  import datasets
23
  import numpy as np
24
+ from copy import copy
25
+
26
+ # check python version
27
+ import sys
28
+ major, minor = sys.version_info[:2]
29
+ version = major + 0.1*minor
30
+ OLD_PY_VERSION = 1 if version < 3.8 else 0
31
 
32
  # Local imports
33
  # from symmetric import SymmetricSampler
 
59
  """
60
  if 'name' not in config:
61
  config['name'] = 'parity'
62
+ if 'length' not in config: # sequence length
63
  config['length'] = 20
64
+ if 'size' not in config: # number of sequences
65
  config['size'] = -1
66
 
67
  self.data_config = config
 
126
  raise NotImplementedError()
127
 
128
 
129
+ class BinaryInputSampler(AutomatonSampler):
130
  def __init__(self, data_config):
131
  super().__init__(data_config)
132
+
133
+ if 'prob1' not in data_config:
134
+ data_config['prob1'] = 0.5
135
+ self.prob1 = data_config['prob1']
136
 
137
  def f(self, x):
138
+ raise NotImplementedError()
139
 
140
  def sample(self):
141
+ x = self.np_rng.binomial(1, self.prob1, size=self.T)
142
  return x, self.f(x)
143
 
144
+ class ParitySampler(BinaryInputSampler):
145
+ def __init__(self, data_config):
146
+ super().__init__(data_config)
147
+ self.name = 'parity'
148
+
149
+ def f(self, x):
150
+ return np.cumsum(x) % 2
151
+
152
+ class GridworldSampler(BinaryInputSampler):
153
+ """
154
+ Note: gridworld currently doesn't include a no-op.
155
+ """
156
+ def __init__(self, data_config):
157
+ super().__init__(data_config)
158
+ self.name = 'gridworld'
159
+
160
+ if 'n' not in data_config:
161
+ data_config['n'] = 9
162
+ """
163
+ NOTE: n is the number of states, and S is the id (0-indexing) of the rightmost state.
164
+ i.e. the states are 0,1,2,...,S, where S=n-1.
165
+ """
166
+ self.n = data_config['n']
167
+ self.S = self.n - 1
168
+
169
+ def f(self, x):
170
+ x = copy(x)
171
+ x[x == 0] = -1
172
+ if OLD_PY_VERSION:
173
+ # NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
174
+ x = np.concatenate([np.array([0]), x]).astype(np.int64)
175
+ states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0)))
176
+ states = states[1:]
177
+ else:
178
+ states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0), initial=0))
179
+ states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
180
+ return np.array(states).astype(np.int64)
181
+
182
+
183
 
184
  class FlipFlopSampler(AutomatonSampler):
185
  def __init__(self, data_config):
186
  super().__init__(data_config)
187
  self.name = 'flipflop'
 
188
 
189
  if 'n' not in data_config:
190
  data_config['n'] = 2
 
217
  def __init__(self, data_config):
218
  super().__init__(data_config)
219
  self.name = 'symmetric'
 
220
 
221
  if 'n' not in data_config:
222
  data_config['n'] = 5 # Default to S5
 
226
  # Options: 'state', 'first_chair'
227
  data_config['label_type'] = 'state'
228
 
229
+ self.n = data_config['n'] # the symmetric group Sn
230
  self.label_type = data_config['label_type']
231
 
232
  """
 
288
 
289
 
290
  dataset_map = {
291
+ 'gridworld': GridworldSampler,
292
  'flipflop': FlipFlopSampler,
293
+ 'parity': ParitySampler,
294
  'symmetric': SymmetricSampler,
295
  # TODO: more datasets
296
  }