ClaraBing commited on
Commit
470476a
1 Parent(s): 59a70ca

add cyclic groups; fix random seeds (should use self.np_rng, otherwise the seed is not fixed)

Browse files
Files changed (1) hide show
  1. automata.py +102 -48
automata.py CHANGED
@@ -103,60 +103,69 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
103
 
104
 
105
  class AutomatonSampler:
106
- def __init__(self, data_config):
107
- self.data_config = data_config
 
 
 
108
 
109
- if 'seed' in self.data_config:
110
- self.np_rng = np.random.default_rng(self.data_config['seed'])
111
- else:
112
- self.np_rng = np.random.default_rng()
113
 
114
- if 'length' not in data_config: # sequence length
115
- data_config['length'] = 20
116
- self.T = self.data_config['length']
117
 
118
- if 'random_length' not in data_config:
119
- data_config['random_length'] = 0
120
- self.random_length = data_config['random_length']
121
 
122
- self.__info__ = " - T (int): sequence length.\n" \
123
- + " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
124
 
125
- def f(self, x):
126
- """
127
- Get output sequence given an input seq
128
- """
129
- raise NotImplementedError()
130
 
131
- def sample(self):
132
- raise NotImplementedError()
133
 
134
- def sample_length(self):
135
- if self.random_length:
136
- return self.np_rng.choice(range(1, self.T+1))
137
- return self.T
138
 
139
- def help(self):
140
  print(self.__info__)
141
 
142
 
 
 
143
  class BinaryInputSampler(AutomatonSampler):
144
- def __init__(self, data_config):
145
- super().__init__(data_config)
 
 
 
 
146
 
147
- if 'prob1' not in data_config:
148
- data_config['prob1'] = 0.5
149
- self.prob1 = data_config['prob1']
150
- self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
151
- + self.__info__
152
 
153
- def f(self, x):
154
- raise NotImplementedError()
155
 
156
- def sample(self):
157
- T = self.sample_length()
158
- x = self.np_rng.binomial(1, self.prob1, size=T)
159
- return x, self.f(x)
160
 
161
  class ParitySampler(BinaryInputSampler):
162
  def __init__(self, data_config):
@@ -220,6 +229,7 @@ class GridworldSampler(BinaryInputSampler):
220
  states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
221
  return np.array(states).astype(np.int64)
222
 
 
223
  class ABABSampler(BinaryInputSampler):
224
  def __init__(self, data_config):
225
  super().__init__(data_config)
@@ -265,7 +275,7 @@ class ABABSampler(BinaryInputSampler):
265
  return labels
266
 
267
  def sample(self):
268
- pos_sample = np.random.random() < self.prob_abab_pos_sample
269
  if pos_sample:
270
  T = self.sample_length()
271
  x = [0,1,0,1] * (T//4)
@@ -299,23 +309,25 @@ class FlipFlopSampler(AutomatonSampler):
299
 
300
  def f(self, x):
301
  state, states = 0, []
302
- for action in x:
303
- state = self.transition[state, action]
304
  states += state,
305
  return np.array(states)
306
 
307
  def sample(self):
308
  T = self.sample_length()
309
- rand = np.random.uniform(size=T)
310
  nonzero_pos = (rand < 0.5).astype(np.int64)
311
- writes = np.random.choice(range(1, self.n_states+1), size=T)
312
  x = writes * nonzero_pos
313
  return x, self.f(x)
314
 
315
 
316
 
 
317
  class PermutationSampler(AutomatonSampler):
318
  """
 
319
  Subclasses: SymmetricSampler, AlternatingSampler
320
  """
321
  def __init__(self, data_config):
@@ -344,8 +356,8 @@ class PermutationSampler(AutomatonSampler):
344
  def f(self, x):
345
  curr_state = np.arange(self.n)
346
  labels = []
347
- for action in x:
348
- curr_state = self.actions[action].dot(curr_state)
349
 
350
  if self.label_type == 'state':
351
  labels += self.get_state_label(curr_state),
@@ -356,7 +368,7 @@ class PermutationSampler(AutomatonSampler):
356
 
357
  def sample(self):
358
  T = self.sample_length()
359
- x = np.random.choice(range(self.n_actions), replace=True, size=T)
360
 
361
  return x, self.f(x)
362
 
@@ -462,13 +474,55 @@ class AlternatingSampler(PermutationSampler):
462
 
463
 
464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
 
466
  dataset_map = {
467
  'abab': ABABSampler,
468
  'alternating': AlternatingSampler,
469
- 'gridworld': GridworldSampler,
470
  'flipflop': FlipFlopSampler,
 
471
  'parity': ParitySampler,
472
  'symmetric': SymmetricSampler,
473
  # TODO: more datasets
474
  }
 
 
103
 
104
 
105
  class AutomatonSampler:
106
+ """
107
+ This is a parent class that must be inherited.
108
+ """
109
+ def __init__(self, data_config):
110
+ self.data_config = data_config
111
 
112
+ if 'seed' in self.data_config:
113
+ self.np_rng = np.random.default_rng(self.data_config['seed'])
114
+ else:
115
+ self.np_rng = np.random.default_rng()
116
 
117
+ if 'length' not in data_config: # sequence length
118
+ data_config['length'] = 20
119
+ self.T = self.data_config['length']
120
 
121
+ if 'random_length' not in data_config:
122
+ data_config['random_length'] = 0
123
+ self.random_length = data_config['random_length']
124
 
125
+ self.__info__ = " - T (int): sequence length.\n" \
126
+ + " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
127
 
128
+ def f(self, x):
129
+ """
130
+ Get output sequence given an input seq
131
+ """
132
+ raise NotImplementedError()
133
 
134
+ def sample(self):
135
+ raise NotImplementedError()
136
 
137
+ def sample_length(self):
138
+ if self.random_length:
139
+ return self.np_rng.choice(range(1, self.T+1))
140
+ return self.T
141
 
142
+ def help(self):
143
  print(self.__info__)
144
 
145
 
146
+
147
+
148
  class BinaryInputSampler(AutomatonSampler):
149
+ """
150
+ This is a parent class that must be inherited.
151
+ Subclasses: ParitySampler, GridworldSampler, ABABSampler
152
+ """
153
+ def __init__(self, data_config):
154
+ super().__init__(data_config)
155
 
156
+ if 'prob1' not in data_config:
157
+ data_config['prob1'] = 0.5
158
+ self.prob1 = data_config['prob1']
159
+ self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
160
+ + self.__info__
161
 
162
+ def f(self, x):
163
+ raise NotImplementedError()
164
 
165
+ def sample(self):
166
+ T = self.sample_length()
167
+ x = self.np_rng.binomial(1, self.prob1, size=T)
168
+ return x, self.f(x)
169
 
170
  class ParitySampler(BinaryInputSampler):
171
  def __init__(self, data_config):
 
229
  states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
230
  return np.array(states).astype(np.int64)
231
 
232
+
233
  class ABABSampler(BinaryInputSampler):
234
  def __init__(self, data_config):
235
  super().__init__(data_config)
 
275
  return labels
276
 
277
  def sample(self):
278
+ pos_sample = self.np_rng.random() < self.prob_abab_pos_sample
279
  if pos_sample:
280
  T = self.sample_length()
281
  x = [0,1,0,1] * (T//4)
 
309
 
310
  def f(self, x):
311
  state, states = 0, []
312
+ for action_id in x:
313
+ state = self.transition[state, action_id]
314
  states += state,
315
  return np.array(states)
316
 
317
  def sample(self):
318
  T = self.sample_length()
319
+ rand = self.np_rng.uniform(size=T)
320
  nonzero_pos = (rand < 0.5).astype(np.int64)
321
+ writes = self.np_rng.choice(range(1, self.n_states+1), size=T)
322
  x = writes * nonzero_pos
323
  return x, self.f(x)
324
 
325
 
326
 
327
+
328
  class PermutationSampler(AutomatonSampler):
329
  """
330
+ This is a parent class that must be inherited.
331
  Subclasses: SymmetricSampler, AlternatingSampler
332
  """
333
  def __init__(self, data_config):
 
356
  def f(self, x):
357
  curr_state = np.arange(self.n)
358
  labels = []
359
+ for action_id in x:
360
+ curr_state = self.actions[action_id].dot(curr_state)
361
 
362
  if self.label_type == 'state':
363
  labels += self.get_state_label(curr_state),
 
368
 
369
  def sample(self):
370
  T = self.sample_length()
371
+ x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
372
 
373
  return x, self.f(x)
374
 
 
474
 
475
 
476
 
477
+ class CyclicSampler(AutomatonSampler):
478
+ def __init__(self, data_config):
479
+ super().__init__(data_config)
480
+
481
+ if 'n' not in data_config:
482
+ data_config['n'] = 5
483
+ self.n = data_config['n']
484
+
485
+ """
486
+ Get actions: shift by i positions, for i = 0 to n_actions-1
487
+ """
488
+ if 'n_actions' not in data_config:
489
+ data_config['n_actions'] = 2
490
+ self.n_actions = data_config['n_actions']
491
+ shift_idx = list(range(1, self.n)) + [0]
492
+ self.actions = {}
493
+ for i in range(self.n_actions):
494
+ shift_idx = list(range(i, self.n)) + list(range(0, i))
495
+ self.actions[i] = np.eye(self.n)[shift_idx]
496
+
497
+
498
+ def f(self, x):
499
+ if OLD_PY_VERSION:
500
+ # NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
501
+ x_padded = np.concatenate([np.array([0]), x]).astype(np.int64)
502
+ states = list(itertools.accumulate(x_padded, lambda a,b: (a+b)%self.n ))
503
+ states = states[1:]
504
+ else:
505
+ states = list(itertools.accumulate(x, lambda a,b: (a+b)%self.n, initial=0))
506
+ states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
507
+
508
+ return np.array(states).astype(np.int64)
509
+
510
+ def sample(self):
511
+ T = self.sample_length()
512
+ x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
513
+
514
+ return x, self.f(x)
515
+
516
+
517
 
518
  dataset_map = {
519
  'abab': ABABSampler,
520
  'alternating': AlternatingSampler,
521
+ 'cyclic': CyclicSampler,
522
  'flipflop': FlipFlopSampler,
523
+ 'gridworld': GridworldSampler,
524
  'parity': ParitySampler,
525
  'symmetric': SymmetricSampler,
526
  # TODO: more datasets
527
  }
528
+