File size: 16,398 Bytes
8453f96 03bcb43 8453f96 87d59b4 59a70ca 8453f96 d262e6f 8453f96 5377ed8 8453f96 be8b808 8453f96 be8b808 6a23009 d262e6f be8b808 5377ed8 8453f96 0a3368a 8453f96 87d59b4 e572ba6 87d59b4 8453f96 0a3368a 8453f96 470476a 8453f96 470476a 8453f96 470476a 8453f96 470476a c0d4681 470476a 6a23009 470476a 8453f96 470476a 8453f96 470476a c0d4681 470476a 6a23009 8453f96 470476a d262e6f 470476a d262e6f 470476a 8453f96 470476a 8453f96 470476a 8453f96 d262e6f 6a23009 d262e6f 6a23009 3555211 6a23009 d262e6f 470476a 6a23009 470476a 6a23009 c0d4681 6a23009 d262e6f 8453f96 c0124b2 6a23009 8453f96 6a23009 8453f96 6a23009 8453f96 6a23009 470476a 6a23009 c0d4681 470476a 6a23009 470476a 6a23009 8453f96 59a70ca 470476a 59a70ca aca0f6d 470476a 59a70ca aca0f6d 59a70ca aca0f6d d262e6f aca0f6d 59a70ca 470476a 59a70ca 470476a 59a70ca 3555211 aca0f6d 59a70ca aca0f6d 6a23009 59a70ca 6a23009 59a70ca aca0f6d 59a70ca aca0f6d 59a70ca aca0f6d 470476a e4c741c 470476a 2c440d1 470476a aca0f6d 8453f96 6a23009 59a70ca 470476a c0124b2 470476a d262e6f aca0f6d 8453f96 aca0f6d 470476a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# Copyright 2020 The HuggingFace Datasets Authors.
# Copyright 2023 Bingbin Liu, Jordan Ash, Surbhi Goel, Akshay Krishnamurthy, and Cyril Zhang.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import csv
import json
import os
import itertools
from sympy.combinatorics.permutations import Permutation
import datasets
import numpy as np
from copy import copy
# check python version
import sys
major, minor = sys.version_info[:2]
version = major + 0.1*minor
OLD_PY_VERSION = 1 if version < 3.8 else 0
_CITATION = """\
"""
_DESCRIPTION = """\
Online dataset mockup.
"""
_HOMEPAGE = ""
_LICENSE = ""
_URLS = {}
class SyntheticAutomataDataset(datasets.GeneratorBasedBuilder):
"""TODO: Short description of my dataset."""
VERSION = datasets.Version("0.0.0")
BUILDER_CONFIGS = []
def __init__(self, config={}, **kwargs):
super().__init__(**kwargs)
"""
Set default configs
"""
if 'name' not in config:
config['name'] = 'parity'
# if 'length' not in config: # sequence length
# config['length'] = 20
if 'size' not in config: # number of sequences
config['size'] = -1
self.data_config = config
self.sampler = dataset_map[config['name']](config)
def _info(self):
features = datasets.Features(
{
"input_ids": datasets.Sequence(datasets.Value("int32"), length=-1),
"label_ids": datasets.Sequence(datasets.Value("int32"), length=-1)
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"split": "train",
},
)
]
def _generate_examples(self, split):
for i in itertools.count(start=0):
if i == self.data_config['size']:
break
x, y = self.sampler.sample()
yield i, {
"input_ids": x,
"label_ids": y
}
class AutomatonSampler:
"""
This is a parent class that must be inherited.
"""
def __init__(self, data_config):
self.data_config = data_config
if 'seed' in self.data_config:
self.np_rng = np.random.default_rng(self.data_config['seed'])
else:
self.np_rng = np.random.default_rng()
if 'length' not in data_config: # sequence length
data_config['length'] = 20
self.T = self.data_config['length']
if 'random_length' not in data_config:
data_config['random_length'] = 0
self.random_length = data_config['random_length']
self.__info__ = " - T (int): sequence length.\n" \
+ " - random_length (int in {0, 1}): whether to randomly sample a length per sample.\n"
def f(self, x):
"""
Get output sequence given an input seq
"""
raise NotImplementedError()
def sample(self):
raise NotImplementedError()
def sample_length(self):
if self.random_length:
return self.np_rng.choice(range(1, self.T+1))
return self.T
def help(self):
print(self.__info__)
class BinaryInputSampler(AutomatonSampler):
"""
This is a parent class that must be inherited.
Subclasses: ParitySampler, GridworldSampler, ABABSampler
"""
def __init__(self, data_config):
super().__init__(data_config)
if 'prob1' not in data_config:
data_config['prob1'] = 0.5
self.prob1 = data_config['prob1']
self.__info__ = " - prob1 (float in [0,1]): probability of token 1\n" \
+ self.__info__
def f(self, x):
raise NotImplementedError()
def sample(self):
T = self.sample_length()
x = self.np_rng.binomial(1, self.prob1, size=T)
return x, self.f(x)
class ParitySampler(BinaryInputSampler):
def __init__(self, data_config):
super().__init__(data_config)
self.name = 'parity'
self.__info__ = "Parity machine with 2 states: \n" \
+ "- Inputs: binary strings\n" \
+ "- Labels: binary strings of the partial parity\n" \
+ "- Config: \n" \
+ self.__info__
def f(self, x):
return np.cumsum(x) % 2
class GridworldSampler(BinaryInputSampler):
"""
Note: gridworld currently doesn't include a no-op.
"""
def __init__(self, data_config):
super().__init__(data_config)
if 'n' not in data_config:
data_config['n'] = 9
"""
NOTE: n is the number of states, and S is the id (0-indexing) of the rightmost state.
i.e. the states are 0,1,2,...,S, where S=n-1.
"""
self.n = data_config['n']
self.S = self.n - 1
if 'label_type' not in data_config:
# Options: state, parity, boundary
data_config['label_type'] = 'state'
self.label_type = data_config['label_type']
self.name = f'Grid{self.n}'
self.__info__ = f"1d Gridworld of n={self.n} states:\n" \
+ "- Inputs: binary strings, i.e. move left(0) or right(1)\n" \
+ "- Labels: depending on 'label_type'. \n" \
+ "- Config: \n" \
+ " - n (int): number of states; i.e. the states are 0,1,2,...,n-1.\n" \
+ " - label_type (str): choosing from the following options:\n" \
+ " - 'state' (default): the state id, i.e. 0 to n-1.\n" \
+ " - 'parity': the state id mod 2.\n" \
+ " - 'boundary': whether the current state is in {0, n-1} or not.\n" \
+ self.__info__
def f(self, x):
x = copy(x)
x[x == 0] = -1
if OLD_PY_VERSION:
# NOTE: for Python 3.7 or below, accumulate doesn't have the 'initial' argument.
x = np.concatenate([np.array([0]), x]).astype(np.int64)
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0)))
states = states[1:]
else:
states = list(itertools.accumulate(x, lambda a,b: max(min(a+b, self.S), 0), initial=0))
states = states[1:] # remove the 1st entry with is the (meaningless) initial value 0
return np.array(states).astype(np.int64)
class ABABSampler(BinaryInputSampler):
def __init__(self, data_config):
super().__init__(data_config)
self.name = 'abab'
if 'prob_abab_pos_sample' not in data_config:
# The probability of having a positive sequence, i.e. 010101010101...
data_config['prob_abab_pos_sample'] = 0.25
if 'label_type' not in data_config:
# Options: 'state', 'boundary'
data_config['label_type'] = 'state'
self.prob_abab_pos_sample = data_config['prob_abab_pos_sample']
self.label_type = data_config['label_type']
self.transition = np.array(
[[4, 1], # state 0
[2, 4], # state 1
[4, 3], # state 2
[0, 4], # state 3
[4, 4], # state 4
])
self.__info__ = "abab: an automaton with 4 states + 1 absorbing state:\n" \
+ "- Inputs: binary strings\n" \
+ "- Labels: depending on 'label_type'.\n" \
+ "- Config:\n" \
+ " - prob_abab_pos_sample (float in [0,1]): probability of having a 'positive' sequence, i.e. 01010101010...\n" \
+ " - label_type (str): choosing from the following options:\n" \
+ " - 'state' (default): the state id.\n" \
+ " - 'boundary': whether the state is in state 3 (the states are 0,1,2,3).\n" \
+ self.__info__
def f(self, x):
labels = []
curr_state = 3
for each in x:
curr_state = self.transition[curr_state, each]
labels += curr_state,
labels = np.array(labels).astype(np.int64)
if self.label_type == 'boundary':
labels = (labels == 3).astype(np.int64)
return labels
def sample(self):
pos_sample = self.np_rng.random() < self.prob_abab_pos_sample
if pos_sample:
T = self.sample_length()
x = [0,1,0,1] * (T//4)
x += [0,1,0,1][:(T%4)]
x = np.array(x)
return x, self.f(x)
else:
return super().sample()
class FlipFlopSampler(AutomatonSampler):
def __init__(self, data_config):
super().__init__(data_config)
self.name = 'flipflop'
if 'n' not in data_config:
data_config['n'] = 2
self.n_states = data_config['n']
self.n_actions = self.n_states + 1
self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
self.__info__ = f"Flipflop with n={self.n_states} states:\n" \
+f"- Inputs: tokens are either 0 (read) or 1:{self.n} (write).\n" \
+ "- Labels: depending on 'label_type'.\n" \
+ "- Config:\n" \
+ " - n (int): number of write states; i.e. the states are 1,2,...,n, plus a default start state 0.\n" \
+ self.__info__
def f(self, x):
state, states = 0, []
for action_id in x:
state = self.transition[state, action_id]
states += state,
return np.array(states)
def sample(self):
T = self.sample_length()
rand = self.np_rng.uniform(size=T)
nonzero_pos = (rand < 0.5).astype(np.int64)
writes = self.np_rng.choice(range(1, self.n_states+1), size=T)
x = writes * nonzero_pos
return x, self.f(x)
class PermutationSampler(AutomatonSampler):
"""
This is a parent class that must be inherited.
Subclasses: SymmetricSampler, AlternatingSampler
"""
def __init__(self, data_config):
super().__init__(data_config)
if 'n' not in data_config:
data_config['n'] = 5
if 'label_type' not in data_config:
# Options: 'state', 'first_chair'
data_config['label_type'] = 'state'
self.n = data_config['n'] # the symmetric group Sn
self.label_type = data_config['label_type']
self.__info__ = \
" - label_type (str): choosing from the following options:\n" \
+ " - 'state' (default): the state id.\n" \
+ " - 'first_chair': the element in the first position of the permutation.\n" \
+ " e.g. if the current permutation is [2,1,4,3], then 'first_chair' is 2.\n" \
+ self.__info__
def get_state_label(self, state):
enc = self.state_encode(state)
return self.state_label_map[enc]
def f(self, x):
curr_state = np.arange(self.n)
labels = []
for action_id in x:
curr_state = self.actions[action_id].dot(curr_state)
if self.label_type == 'state':
labels += self.get_state_label(curr_state),
elif self.label_type == 'first_chair':
labels += curr_state[0],
return np.array(labels)
def sample(self):
T = self.sample_length()
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
return x, self.f(x)
class SymmetricSampler(PermutationSampler):
"""
TODO: add options for labels as functions of states
- parity (whether a state is even): this may need packages (e.g. Permutation from sympy)
- position / toggle: for S3 ~ D6, we can add labels for substructures as in Dihedral groups.
"""
def __init__(self, data_config):
super().__init__(data_config)
self.name = f'S{self.n}'
"""
Get states
"""
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
self.state_label_map = {}
for si, state in enumerate(itertools.permutations(range(self.n))):
enc = self.state_encode(state)
self.state_label_map[enc] = si
"""
Get actions (3 defaults: id, shift-by-1, swap-first-two)
"""
if 'n_actions' not in data_config:
data_config['n_actions'] = 3
self.n_actions = data_config['n_actions']
self.actions = {0: np.eye(self.n)}
# shift all elements to the right by 1
shift_idx = list(range(1, self.n)) + [0]
self.actions[1] = np.eye(self.n)[shift_idx]
# swap the first 2 elements
shift_idx = [1, 0] + list(range(2, self.n))
self.actions[2] = np.eye(self.n)[shift_idx]
if self.n_actions > 3:
# add permutations in the order given by itertools.permutations
self.all_permutations = list(itertools.permutations(range(self.n)))[1:]
cnt = 2
for each in self.all_permutations:
action = np.eye(self.n)[list(each)]
if np.linalg.norm(action - self.actions[0]) == 0:
continue
elif np.linalg.norm(action - self.actions[1]) == 0:
continue
self.actions[cnt] = action
cnt += 1
if cnt == self.n_actions: break
self.__info__ = f"Symmetric group on n={self.n} objects:\n" \
+f"- Inputs: tokens are either 0 (no-op), or 1:{self.n_actions} (corresponding to {self.n_actions} permutations).\n" \
+ "- Labels: depending on 'label_type'.\n" \
+ "- Config:\n" \
+ " - n (int): number of objects, i.e. there are n! states.\n" \
+ " - n_actions (int): number of permutations to include in the generator set;\n" \
+ " the ordering is given by itertools.permutations, and the first 'n_actions' permutations will be included.\n" \
+ self.__info__
class AlternatingSampler(PermutationSampler):
"""
TODO: other choices of generators (currently using (12x))?
"""
def __init__(self, data_config):
super().__init__(data_config)
self.name = f'A{self.n}'
"""
Get states
"""
self.state_label_map = {}
self.state_encode = lambda state: ''.join([str(int(each)) for each in state])
cnt = 0
for si, state in enumerate(itertools.permutations(range(self.n))):
if not Permutation(state).is_even:
continue
enc = self.state_encode(state)
self.state_label_map[enc] = cnt
cnt += 1
"""
Get actions: all 3 cycles of the form (12x)
"""
self.actions = {0: np.eye(self.n)}
for idx in range(2, self.n):
# (1, 2, idx)
shift_idx = list(range(self.n))
shift_idx[0],shift_idx[1], shift_idx[idx] = shift_idx[1], shift_idx[idx], shift_idx[0]
self.actions[idx-1] = np.eye(self.n)[shift_idx]
self.n_actions = len(self.actions)
self.__info__ = f"Alternating group on n={self.n} objects:\n" \
+f"- Inputs: tokens from 0 to n-3, corresponding to all 3-cycles of the form (12x).\n" \
+ "- Labels: depending on 'label_type'.\n" \
+ "- Config:\n" \
+ " - n (int): number of objects, i.e. there are n!/2 states.\n" \
+ self.__info__
class CyclicSampler(AutomatonSampler):
def __init__(self, data_config):
super().__init__(data_config)
if 'n' not in data_config:
data_config['n'] = 5
self.n = data_config['n']
"""
Get actions: shift by i positions, for i = 0 to n_actions-1
"""
if 'n_actions' not in data_config:
data_config['n_actions'] = 2
self.n_actions = data_config['n_actions']
shift_idx = list(range(1, self.n)) + [0]
self.actions = {}
for i in range(self.n_actions):
shift_idx = list(range(i, self.n)) + list(range(0, i))
self.actions[i] = np.eye(self.n)[shift_idx]
self.__info__ = 'Cyclic group of n={self.n} states:\n' \
+f"- Inputs: tokens from 0 to n_actions-1\n" \
+ "- Labels: the current state.\n" \
+ "- Config:\n" \
+ " - n (int): number of states.\n" \
+ " - n_actions (int): number of actions/generators, which are 0, 1, ..., n_actions-1.\n" \
+ self.__info__
def f(self, x):
return np.cumsum(x) % self.n
def sample(self):
T = self.sample_length()
x = self.np_rng.choice(range(self.n_actions), replace=True, size=T)
return x, self.f(x)
dataset_map = {
'abab': ABABSampler,
'alternating': AlternatingSampler,
'cyclic': CyclicSampler,
'flipflop': FlipFlopSampler,
'gridworld': GridworldSampler,
'parity': ParitySampler,
'symmetric': SymmetricSampler,
# TODO: more datasets
}
|