add AdderSampler
Browse files- automata.py +68 -2
automata.py
CHANGED
|
@@ -150,6 +150,8 @@ class BinaryInputSampler(AutomatonSampler):
|
|
| 150 |
"""
|
| 151 |
This is a parent class that must be inherited.
|
| 152 |
Subclasses: ParitySampler, GridworldSampler, ABABSampler
|
|
|
|
|
|
|
| 153 |
"""
|
| 154 |
def __init__(self, data_config):
|
| 155 |
super().__init__(data_config)
|
|
@@ -287,6 +289,65 @@ class ABABSampler(BinaryInputSampler):
|
|
| 287 |
return super().sample()
|
| 288 |
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
|
| 292 |
class FlipFlopSampler(AutomatonSampler):
|
|
@@ -303,7 +364,7 @@ class FlipFlopSampler(AutomatonSampler):
|
|
| 303 |
|
| 304 |
self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
|
| 305 |
+f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
|
| 306 |
-
+ "- Labels:
|
| 307 |
+ "- Config:\n" \
|
| 308 |
+ " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
|
| 309 |
+ self.__info__
|
|
@@ -638,8 +699,13 @@ class QuaternionSampler(AutomatonSampler):
|
|
| 638 |
return x, self.f(x)
|
| 639 |
|
| 640 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
dataset_map = {
|
| 642 |
'abab': ABABSampler,
|
|
|
|
| 643 |
'alternating': AlternatingSampler,
|
| 644 |
'cyclic': CyclicSampler,
|
| 645 |
'dihedral': DihedralSampler,
|
|
@@ -648,6 +714,6 @@ dataset_map = {
|
|
| 648 |
'parity': ParitySampler,
|
| 649 |
'quaternion': QuaternionSampler,
|
| 650 |
'symmetric': SymmetricSampler,
|
| 651 |
-
# TODO:
|
| 652 |
}
|
| 653 |
|
|
|
|
| 150 |
"""
|
| 151 |
This is a parent class that must be inherited.
|
| 152 |
Subclasses: ParitySampler, GridworldSampler, ABABSampler
|
| 153 |
+
|
| 154 |
+
TODO: sample sequences with a given number of 1s
|
| 155 |
"""
|
| 156 |
def __init__(self, data_config):
|
| 157 |
super().__init__(data_config)
|
|
|
|
| 289 |
return super().sample()
|
| 290 |
|
| 291 |
|
| 292 |
+
class AdderSampler(BinaryInputSampler):
|
| 293 |
+
def __init__(self, data_config):
|
| 294 |
+
super().__init__(data_config)
|
| 295 |
+
self.name = 'addition'
|
| 296 |
+
|
| 297 |
+
if 'n_addends' not in data_config:
|
| 298 |
+
data_config['n_addends'] = 2
|
| 299 |
+
self.n_addends = data_config['n_addends']
|
| 300 |
+
self.addend_scales = np.array([2**i for i in range(self.n_addends)]).reshape(-1, 1)
|
| 301 |
+
|
| 302 |
+
if 'label_type' not in data_config:
|
| 303 |
+
data_config['label_type'] = 'state'
|
| 304 |
+
self.label_type = data_config['label_type']
|
| 305 |
+
|
| 306 |
+
self.__info__ = f'Adder of n={self.n_addends} binary numbers:\n' \
|
| 307 |
+
+f"- Inputs: {self.n_addends} binary numbers, encoded as the int for the {self.n_addends}-bit binary number.\n" \
|
| 308 |
+
+ "- Labels: depending on the label_type.\n" \
|
| 309 |
+
+ "- Config:\n" \
|
| 310 |
+
+ " - n_addends (int): number of binary numbers to be added; default as 2.\n" \
|
| 311 |
+
+ " - label_type (str): choosing from the following options: \n" \
|
| 312 |
+
+f" - 'state': the state id, i.e. the int for the base-{self.n_addends} int corresponding to the number (carry, digit). \n" \
|
| 313 |
+
+f" - 'digit': the current output base-{self.n_addends} digit, without the carry. \n" \
|
| 314 |
+
+ " - 'position': the current carry bit.\n" \
|
| 315 |
+
+ self.__info__
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def f(self, x):
|
| 319 |
+
outputs, carries = [], []
|
| 320 |
+
carry = 0
|
| 321 |
+
T = x.shape[-1]
|
| 322 |
+
for i in range(T):
|
| 323 |
+
curr_sum = x[:, i].sum() + carry
|
| 324 |
+
# NOTE: 'mod n_addends' makes sure the carry is binary
|
| 325 |
+
output, carry = curr_sum % self.n_addends, curr_sum // self.n_addends
|
| 326 |
+
outputs += output,
|
| 327 |
+
carries += carry,
|
| 328 |
+
outputs = np.array(outputs).astype(np.int64)
|
| 329 |
+
carries = np.array(carries).astype(np.int64)
|
| 330 |
+
|
| 331 |
+
if self.label_type == 'state':
|
| 332 |
+
return outputs + self.n_addends*carries
|
| 333 |
+
elif self.label_type == 'digit':
|
| 334 |
+
return outputs
|
| 335 |
+
elif self.label_type == 'carry':
|
| 336 |
+
return carries
|
| 337 |
+
|
| 338 |
+
def sample_addend(self, T):
|
| 339 |
+
a = self.np_rng.binomial(1, self.prob1, size=T)
|
| 340 |
+
return a
|
| 341 |
+
|
| 342 |
+
def sample(self):
|
| 343 |
+
T = self.sample_length()
|
| 344 |
+
x = np.stack([self.sample_addend(T) for _ in range(self.n_addends)])
|
| 345 |
+
# Pad the most significant bit (rightmost position, i.e. we're reversing the number) with 0 to handle the potential carry
|
| 346 |
+
pad = np.zeros((self.n_addends, 1))
|
| 347 |
+
x = np.concatenate([x, pad], 1)
|
| 348 |
+
|
| 349 |
+
x_encode = (self.addend_scales * x).sum(0)
|
| 350 |
+
return x_encode, self.f(x)
|
| 351 |
|
| 352 |
|
| 353 |
class FlipFlopSampler(AutomatonSampler):
|
|
|
|
| 364 |
|
| 365 |
self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
|
| 366 |
+f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
|
| 367 |
+
+ "- Labels: the state id.\n" \
|
| 368 |
+ "- Config:\n" \
|
| 369 |
+ " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
|
| 370 |
+ self.__info__
|
|
|
|
| 699 |
return x, self.f(x)
|
| 700 |
|
| 701 |
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
|
| 705 |
+
|
| 706 |
dataset_map = {
|
| 707 |
'abab': ABABSampler,
|
| 708 |
+
'add': AdderSampler,
|
| 709 |
'alternating': AlternatingSampler,
|
| 710 |
'cyclic': CyclicSampler,
|
| 711 |
'dihedral': DihedralSampler,
|
|
|
|
| 714 |
'parity': ParitySampler,
|
| 715 |
'quaternion': QuaternionSampler,
|
| 716 |
'symmetric': SymmetricSampler,
|
| 717 |
+
# TODO: add Dyck
|
| 718 |
}
|
| 719 |
|