add cyclic groups; fix random seeds (should use self.np_rng, otherwise the seed is not fixed)
Browse files- automata.py +102 -48
automata.py
CHANGED
|
@@ -103,60 +103,69 @@ class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
|
|
| 103 |
|
| 104 |
|
| 105 |
class AutomatonSampler:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
print(self.__info__)
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
| 143 |
class BinaryInputSampler(AutomatonSampler):
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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 =
|
| 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
|
| 303 |
-
state = self.transition[state,
|
| 304 |
states += state,
|
| 305 |
return np.array(states)
|
| 306 |
|
| 307 |
def sample(self):
|
| 308 |
T = self.sample_length()
|
| 309 |
-
rand =
|
| 310 |
nonzero_pos = (rand < 0.5).astype(np.int64)
|
| 311 |
-
writes =
|
| 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
|
| 348 |
-
curr_state = self.actions[
|
| 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 =
|
| 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 |
-
'
|
| 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 |
+
|