add option 'random_length'
Browse files- automata.py +21 -7
automata.py
CHANGED
|
@@ -117,7 +117,12 @@ class AutomatonSampler:
|
|
| 117 |
data_config['length'] = 20
|
| 118 |
self.T = self.data_config['length']
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
def f(self, x):
|
| 123 |
"""
|
|
@@ -128,6 +133,11 @@ class AutomatonSampler:
|
|
| 128 |
def sample(self):
|
| 129 |
raise NotImplementedError()
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def help(self):
|
| 132 |
print(self.__info__)
|
| 133 |
|
|
@@ -146,7 +156,8 @@ class BinaryInputSampler(AutomatonSampler):
|
|
| 146 |
raise NotImplementedError()
|
| 147 |
|
| 148 |
def sample(self):
|
| 149 |
-
|
|
|
|
| 150 |
return x, self.f(x)
|
| 151 |
|
| 152 |
class ParitySampler(BinaryInputSampler):
|
|
@@ -258,8 +269,9 @@ class ABABSampler(BinaryInputSampler):
|
|
| 258 |
def sample(self):
|
| 259 |
pos_sample = np.random.random() < self.prob_abab_pos_sample
|
| 260 |
if pos_sample:
|
| 261 |
-
|
| 262 |
-
x
|
|
|
|
| 263 |
x = np.array(x)
|
| 264 |
return x, self.f(x)
|
| 265 |
else:
|
|
@@ -295,9 +307,10 @@ class FlipFlopSampler(AutomatonSampler):
|
|
| 295 |
return np.array(states)
|
| 296 |
|
| 297 |
def sample(self):
|
| 298 |
-
|
|
|
|
| 299 |
nonzero_pos = (rand < 0.5).astype(np.int64)
|
| 300 |
-
writes = np.random.choice(range(1, self.n_states+1), size=
|
| 301 |
x = writes * nonzero_pos
|
| 302 |
return x, self.f(x)
|
| 303 |
|
|
@@ -389,7 +402,8 @@ class SymmetricSampler(AutomatonSampler):
|
|
| 389 |
return np.array(labels)
|
| 390 |
|
| 391 |
def sample(self):
|
| 392 |
-
|
|
|
|
| 393 |
|
| 394 |
return x, self.f(x)
|
| 395 |
|
|
|
|
| 117 |
data_config['length'] = 20
|
| 118 |
self.T = self.data_config['length']
|
| 119 |
|
| 120 |
+
if 'random_length' not in data_config:
|
| 121 |
+
data_config['random_length'] = 0
|
| 122 |
+
self.random_length = data_config['random_length']
|
| 123 |
+
|
| 124 |
+
self.__info__ = " - T (int): sequence length.\n" \
|
| 125 |
+
+ " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
|
| 126 |
|
| 127 |
def f(self, x):
|
| 128 |
"""
|
|
|
|
| 133 |
def sample(self):
|
| 134 |
raise NotImplementedError()
|
| 135 |
|
| 136 |
+
def sample_length(self):
|
| 137 |
+
if self.random_length:
|
| 138 |
+
return self.np_rng.choice(range(1, self.T+1))
|
| 139 |
+
return self.T
|
| 140 |
+
|
| 141 |
def help(self):
|
| 142 |
print(self.__info__)
|
| 143 |
|
|
|
|
| 156 |
raise NotImplementedError()
|
| 157 |
|
| 158 |
def sample(self):
|
| 159 |
+
T = self.sample_length()
|
| 160 |
+
x = self.np_rng.binomial(1, self.prob1, size=T)
|
| 161 |
return x, self.f(x)
|
| 162 |
|
| 163 |
class ParitySampler(BinaryInputSampler):
|
|
|
|
| 269 |
def sample(self):
|
| 270 |
pos_sample = np.random.random() < self.prob_abab_pos_sample
|
| 271 |
if pos_sample:
|
| 272 |
+
T = self.sample_length()
|
| 273 |
+
x = [0,1,0,1] * (T//4)
|
| 274 |
+
x += [0,1,0,1][:(T%4)]
|
| 275 |
x = np.array(x)
|
| 276 |
return x, self.f(x)
|
| 277 |
else:
|
|
|
|
| 307 |
return np.array(states)
|
| 308 |
|
| 309 |
def sample(self):
|
| 310 |
+
T = self.sample_length()
|
| 311 |
+
rand = np.random.uniform(size=T)
|
| 312 |
nonzero_pos = (rand < 0.5).astype(np.int64)
|
| 313 |
+
writes = np.random.choice(range(1, self.n_states+1), size=T)
|
| 314 |
x = writes * nonzero_pos
|
| 315 |
return x, self.f(x)
|
| 316 |
|
|
|
|
| 402 |
return np.array(labels)
|
| 403 |
|
| 404 |
def sample(self):
|
| 405 |
+
T = self.sample_length()
|
| 406 |
+
x = np.random.choice(range(self.n_actions), replace=True, size=T)
|
| 407 |
|
| 408 |
return x, self.f(x)
|
| 409 |
|