ClaraBing commited on
Commit
6a23009
·
1 Parent(s): 3555211

add ABAB; add __info__ string & help()

Browse files
Files changed (1) hide show
  1. automata.py +130 -23
automata.py CHANGED
@@ -59,8 +59,8 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
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
 
@@ -113,8 +113,12 @@ class AutomatonSampler:
113
  else:
114
  self.np_rng = np.random.default_rng()
115
 
 
 
116
  self.T = self.data_config['length']
117
 
 
 
118
  def f(self, x):
119
  """
120
  Get output sequence given an input seq
@@ -124,6 +128,9 @@ class AutomatonSampler:
124
  def sample(self):
125
  raise NotImplementedError()
126
 
 
 
 
127
 
128
  class BinaryInputSampler(AutomatonSampler):
129
  def __init__(self, data_config):
@@ -132,6 +139,8 @@ class BinaryInputSampler(AutomatonSampler):
132
  if 'prob1' not in data_config:
133
  data_config['prob1'] = 0.5
134
  self.prob1 = data_config['prob1']
 
 
135
 
136
  def f(self, x):
137
  raise NotImplementedError()
@@ -145,6 +154,12 @@ class ParitySampler(BinaryInputSampler):
145
  super().__init__(data_config)
146
  self.name = 'parity'
147
 
 
 
 
 
 
 
148
  def f(self, x):
149
  return np.cumsum(x) % 2
150
 
@@ -164,8 +179,25 @@ class GridworldSampler(BinaryInputSampler):
164
  self.n = data_config['n']
165
  self.S = self.n - 1
166
 
 
 
 
 
 
167
  self.name = f'Grid{self.n}'
168
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  def f(self, x):
170
  x = copy(x)
171
  x[x == 0] = -1
@@ -179,33 +211,95 @@ class GridworldSampler(BinaryInputSampler):
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
191
-
192
- self.n_states = data_config['n']
193
- self.n_actions = self.n_states + 1
194
- self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
195
 
196
- def f(self, x):
197
- state, states = 0, []
198
- for action in x:
199
- state = self.transition[state, action]
200
- states += state,
201
- return np.array(states)
202
 
203
- def sample(self):
204
- rand = np.random.uniform(size=self.T)
205
- nonzero_pos = (rand < 0.5).astype(np.int64)
206
- writes = np.random.choice(range(1, self.n_states+1), size=self.T)
207
- x = writes * nonzero_pos
208
- return x, self.f(x)
 
 
 
 
 
 
 
209
 
210
 
211
  class SymmetricSampler(AutomatonSampler):
@@ -265,6 +359,18 @@ class SymmetricSampler(AutomatonSampler):
265
  cnt += 1
266
  if cnt == self.n_actions: break
267
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  def get_state_label(self, state):
269
  enc = self.state_encode(state)
270
  return self.state_label_map[enc]
@@ -289,6 +395,7 @@ class SymmetricSampler(AutomatonSampler):
289
 
290
 
291
  dataset_map = {
 
292
  'gridworld': GridworldSampler,
293
  'flipflop': FlipFlopSampler,
294
  'parity': ParitySampler,
 
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
 
 
113
  else:
114
  self.np_rng = np.random.default_rng()
115
 
116
+ if 'length' not in data_config: # sequence length
117
+ data_config['length'] = 20
118
  self.T = self.data_config['length']
119
 
120
+ self.__info__ = " - T (int): sequence length"
121
+
122
  def f(self, x):
123
  """
124
  Get output sequence given an input seq
 
128
  def sample(self):
129
  raise NotImplementedError()
130
 
131
+ def help(self):
132
+ print(self.__info__)
133
+
134
 
135
  class BinaryInputSampler(AutomatonSampler):
136
  def __init__(self, data_config):
 
139
  if 'prob1' not in data_config:
140
  data_config['prob1'] = 0.5
141
  self.prob1 = data_config['prob1']
142
+ self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
143
+ + self.__info__
144
 
145
  def f(self, x):
146
  raise NotImplementedError()
 
154
  super().__init__(data_config)
155
  self.name = 'parity'
156
 
157
+ self.__info__ = "Parity machine with 2 states: \n" \
158
+ + "- Inputs: binary strings\n" \
159
+ + "- Labels: binary strings of the partial parity\n" \
160
+ + "- Config: \n" \
161
+ + self.__info__
162
+
163
  def f(self, x):
164
  return np.cumsum(x) % 2
165
 
 
179
  self.n = data_config['n']
180
  self.S = self.n - 1
181
 
182
+ if 'label_type' not in data_config:
183
+ # Options: state, parity, boundary
184
+ data_config['label_type'] = 'state'
185
+ self.label_type = data_config['label_type']
186
+
187
  self.name = f'Grid{self.n}'
188
 
189
+ self.__info__ = f"1d Gridworld of n={self.n} states:\n" \
190
+ + "- Inputs: binary strings, i.e. move left(0) or right(1)\n" \
191
+ + "- Labels: depending on 'label_type'. \n" \
192
+ + "- Config: \n" \
193
+ + " - n (int): number of states; i.e. the states are 0,1,2,...,n-1.\n" \
194
+ + " - label_type (str): choosing from the following options:\n" \
195
+ + " - 'state' (default): the state id, i.e. 0 to n-1.\n" \
196
+ + " - 'parity': the state id mod 2.\n" \
197
+ + " - 'boundary': whether the current state is in {0, n-1} or not.\n" \
198
+ + self.__info__
199
+
200
+
201
  def f(self, x):
202
  x = copy(x)
203
  x[x == 0] = -1
 
211
  states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
212
  return np.array(states).astype(np.int64)
213
 
214
+ class ABABSampler(BinaryInputSampler):
215
+ def __init__(self, data_config):
216
+ super().__init__(data_config)
217
+ self.name = 'abab'
218
+
219
+ if 'prob_abab_pos_sample' not in data_config:
220
+ # The probability of having a positive sequence, i.e. 010101010101...
221
+ data_config['prob_abab_pos_sample'] = 0.25
222
+ if 'label_type' not in data_config:
223
+ # Options: 'state', 'boundary'
224
+ data_config['label_type'] = 'state'
225
+
226
+ self.prob_abab_pos_sample = data_config['prob_abab_pos_sample']
227
+ self.label_type = data_config['label_type']
228
+
229
+ self.transition = np.array(
230
+ [[4, 1], # state 0
231
+ [2, 4], # state 1
232
+ [4, 3], # state 2
233
+ [0, 4], # state 3
234
+ [4, 4], # state 4
235
+ ])
236
+
237
+ self.__info__ = "abab: an automaton with 4 states + 1 absorbing state:\n" \
238
+ + "- Inputs: binary strings\n" \
239
+ + "- Labels: depending on 'label_type'.\n" \
240
+ + "- Config:\n" \
241
+ + " - prob_abab_pos_sample (float in [0,1]): probability of having a 'positive' sequence, i.e. 01010101010...\n" \
242
+ + " - label_type (str): choosing from the following options:\n" \
243
+ + " - 'state' (default): the state id.\n" \
244
+ + " - 'boundary': whether the state is in state 3 (the states are 0,1,2,3).\n" \
245
+ + self.__info__
246
+
247
+ def f(self, x):
248
+ labels = []
249
+ curr_state = 3
250
+ for each in x:
251
+ curr_state = self.transition[curr_state, each]
252
+ labels += curr_state,
253
+ labels = np.array(labels).astype(np.int64)
254
+ if self.label_type == 'boundary':
255
+ labels = (labels == 3).astype(np.int64)
256
+ return labels
257
+
258
+ def sample(self):
259
+ pos_sample = np.random.random() < self.prob_abab_pos_sample
260
+ if pos_sample:
261
+ x = [0,1,0,1] * (self.T//4)
262
+ x += [0,1,0,1][:(self.T%4)]
263
+ x = np.array(x)
264
+ return x, self.f(x)
265
+ else:
266
+ return super().sample()
267
+
268
+
269
 
270
 
271
  class FlipFlopSampler(AutomatonSampler):
272
+ def __init__(self, data_config):
273
+ super().__init__(data_config)
274
+ self.name = 'flipflop'
275
 
276
+ if 'n' not in data_config:
277
+ data_config['n'] = 2
278
+
279
+ self.n_states = data_config['n']
280
+ self.n_actions = self.n_states + 1
281
+ self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
282
 
283
+ self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
284
+ +f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
285
+ + "- Labels: depending on 'label_type'.\n" \
286
+ + "- Config:\n" \
287
+ + " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
288
+ + self.__info__
289
 
290
+ def f(self, x):
291
+ state, states = 0, []
292
+ for action in x:
293
+ state = self.transition[state, action]
294
+ states += state,
295
+ return np.array(states)
296
+
297
+ def sample(self):
298
+ rand = np.random.uniform(size=self.T)
299
+ nonzero_pos = (rand < 0.5).astype(np.int64)
300
+ writes = np.random.choice(range(1, self.n_states+1), size=self.T)
301
+ x = writes * nonzero_pos
302
+ return x, self.f(x)
303
 
304
 
305
  class SymmetricSampler(AutomatonSampler):
 
359
  cnt += 1
360
  if cnt == self.n_actions: break
361
 
362
+ self.__info__ = f"Symmetric group on n={self.n} objects:\n" \
363
+ +f"- Inputs: tokens are either 0 (no-op), or 1:{self.n_actions} (corresponding to {self.n_actions} permutations).\n" \
364
+ + "- Labels: depending on 'label_type'.\n" \
365
+ + "- Config:\n" \
366
+ + " - n (int): number of objects, i.e. there are n! states.\n" \
367
+ + " - label_type (str): choosing from the following options:\n" \
368
+ + " - 'state' (default): the state id.\n" \
369
+ + " - 'first_chair': the element in the first position of the permutation.\n" \
370
+ + " e.g. if the current permutation is [2,3,1,4], then 'first_chair' is 2.\n" \
371
+ + self.__info__
372
+
373
+
374
  def get_state_label(self, state):
375
  enc = self.state_encode(state)
376
  return self.state_label_map[enc]
 
395
 
396
 
397
  dataset_map = {
398
+ 'abab': ABABSampler,
399
  'gridworld': GridworldSampler,
400
  'flipflop': FlipFlopSampler,
401
  'parity': ParitySampler,