cyrilzhang commited on
Commit
0c45ea8
1 Parent(s): 2a180d8

add PermutationResetSampler

Browse files
Files changed (1) hide show
  1. automata.py +49 -0
automata.py CHANGED
@@ -699,7 +699,55 @@ class QuaternionSampler(AutomatonSampler):
699
  return x, self.f(x)
700
 
701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703
 
704
 
705
 
@@ -714,6 +762,7 @@ dataset_map = {
714
  'parity': ParitySampler,
715
  'quaternion': QuaternionSampler,
716
  'symmetric': SymmetricSampler,
 
717
  # TODO: add Dyck
718
  }
719
 
 
699
  return x, self.f(x)
700
 
701
 
702
+ class PermutationResetSampler(AutomatonSampler):
703
+ def __init__(self, data_config):
704
+ super().__init__(data_config)
705
+
706
+ self.n = data_config['n']
707
+ self.generators = data_config['generators']
708
+ self.perm_probs = data_config['perm_probs']
709
+ if type(self.generators[0]) is str:
710
+ self.generators = [ np.array(list(map(int, list(g)))) for g in self.generators ]
711
+
712
+ self.vocab_size = math.factorial(self.n) # states = permutations; maybe rename
713
+ self.n_generators = len(self.generators) # actions = generators
714
+ self.n_actions = self.vocab_size + self.n_generators # 1 reset symbol per state, 1 apply symbol per generator
715
 
716
+ self.init_state = np.arange(self.n) # identity permutation
717
+
718
+ # lookup tables
719
+ self.int2perm = list(map(np.array, itertools.permutations(range(self.n))))
720
+ self.perm2int = {tuple(p):i for i,p in enumerate(self.int2perm)}
721
+
722
+ # interval lengths
723
+ T = self.sample_length()
724
+ self.lags = [1]
725
+ while self.lags[-1]*2 < T:
726
+ self.lags.append(self.lags[-1]*2)
727
+
728
+ def f(self, x):
729
+ curr_state = self.init_state
730
+ states = []
731
+ for action_id in x:
732
+ if action_id >= self.vocab_size:
733
+ curr_state = self.generators[action_id - self.vocab_size][curr_state]
734
+ else:
735
+ curr_state = self.int2perm[action_id]
736
+ states.append(self.perm2int[tuple(curr_state)])
737
+ return np.array(states, dtype=np.int64)
738
+
739
+ def sample(self):
740
+ T = self.sample_length()
741
+ x = self.np_rng.choice(range(self.n_generators), p=self.perm_probs, size=T) + self.vocab_size
742
+
743
+ i = 0
744
+ reset_in = 0 # always start with reset
745
+ while i < T:
746
+ if reset_in == 0:
747
+ x[i] = self.np_rng.choice(range(self.vocab_size))
748
+ i += self.np_rng.choice(self.lags)
749
+
750
+ return x, self.f(x)
751
 
752
 
753
 
 
762
  'parity': ParitySampler,
763
  'quaternion': QuaternionSampler,
764
  'symmetric': SymmetricSampler,
765
+ 'permutation_reset': PermutationResetSampler
766
  # TODO: add Dyck
767
  }
768