ClaraBing commited on
Commit
47341b7
·
1 Parent(s): d3fea2b

add quaternion group

Browse files
Files changed (1) hide show
  1. automata.py +37 -1
automata.py CHANGED
@@ -122,7 +122,8 @@ class AutomatonSampler:
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):
@@ -602,6 +603,40 @@ class DihedralSampler(AutomatonSampler):
602
 
603
 
604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605
 
606
  dataset_map = {
607
  'abab': ABABSampler,
@@ -611,6 +646,7 @@ dataset_map = {
611
  'flipflop': FlipFlopSampler,
612
  'gridworld': GridworldSampler,
613
  'parity': ParitySampler,
 
614
  'symmetric': SymmetricSampler,
615
  # TODO: more datasets
616
  }
 
122
  data_config['random_length'] = 0
123
  self.random_length = data_config['random_length']
124
 
125
+ self.__info__ = \
126
+ " - T (int): sequence length.\n" \
127
  + " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
128
 
129
  def f(self, x):
 
603
 
604
 
605
 
606
+ class QuaternionSampler(AutomatonSampler):
607
+ def __init__(self, data_config):
608
+ super().__init__(data_config)
609
+
610
+ self.vocab_size = 8 # {-1, 1} x {1, i, j, k}
611
+ self.n_actions = 4 # {1, i, j, k}
612
+ self.transition_pos = [
613
+ 0, 1, 2, 3,
614
+ 1, 4, 3, 6,
615
+ 2, 7, 4, 1,
616
+ 3, 2, 5, 4,]
617
+ self.transition_neg = [(each+4)%8 for each in self.transition_pos]
618
+ self.transition = np.array(self.transition_pos + self.transition_neg)
619
+ self.transition = self.transition.reshape(-1, 4)
620
+
621
+ self.__info__ = "Quaternion group:\n" \
622
+ + "- Inputs: tokens in {0,1,2,3}, corresponding to 1,i,j,k.\n" \
623
+ + "- Labels: the state id; 8 states in total: 2 signs ({-1,1}) x 4 values ({1,i,j,k}).\n" \
624
+ + "- Config:\n" \
625
+ + self.__info__
626
+
627
+ def f(self, x):
628
+ curr_state = 0
629
+ states = []
630
+ for action_id in x:
631
+ curr_state = self.transition[curr_state, action_id]
632
+ states += curr_state,
633
+ return np.array(states).astype(np.int64)
634
+
635
+ def sample(self):
636
+ T = self.sample_length()
637
+ x = self.np_rng.choice(range(self.n_actions), size=T)
638
+ return x, self.f(x)
639
+
640
 
641
  dataset_map = {
642
  'abab': ABABSampler,
 
646
  'flipflop': FlipFlopSampler,
647
  'gridworld': GridworldSampler,
648
  'parity': ParitySampler,
649
+ 'quaternion': QuaternionSampler,
650
  'symmetric': SymmetricSampler,
651
  # TODO: more datasets
652
  }