Skip to main content

UniqueRandomizer

Overview

UniqueRandomizer is a data structure for sampling outputs of a randomized program, such as a neural sequence model, incrementally and without replacement.

  • Incremental sampling: Instead of sampling a large batch of outputs all at once, as with beam search, UniqueRandomizer provides samples one at a time. This enables flexibility in stopping criteria, such as stopping the sampling process as soon as a satisfactory output is found.
  • Sampling without replacement: In many applications, a neural model is used to produce candidate solutions to some search or optimization problem. In such applications it is usually desirable to consider unique candidate solutions, since duplicates are typically not useful.

For more details, refer to our paper, Incremental Sampling Without Replacement for Sequence Models, published at ICML 2020.

BibTeX entry:

@article{shi2020uniquerandomizer,
  title = {Incremental Sampling Without Replacement for Sequence Models},
  author = {Kensen Shi and David Bieber and Charles Sutton},
  booktitle = {Proceedings of the 37th International Conference on Machine Learning},
  year = {2020}
}

Installation

python3 -m pip install --user unique-randomizer

This package requires Python 3. The above command automatically installs the following dependencies as well:

  • absl-py >= 0.6.1
  • numpy >= 1.15.4
  • scipy >= 1.1.0

Usage

To use UniqueRandomizer, first identify the program or function that you wish to draw unique samples from, such as the draw_sample function in the following example:

def draw_sample(sequence_model, state):
  """Draws a sample (a sequence of token indices) from the sequence model."""
  tokens = []
  token = BOS
  for i in range(MAX_LEN):
    probs, state = sequence_model(token, state)
    token = np.random.choice(np.arange(len(probs)), p=probs)
    if token == EOS:
      break
    tokens.append(token)
  return tokens

Note that draw_sample can take inputs and can use control flow such as loops, conditionals, and recursion. There are only two constraints on the draw_sample function:

  1. It must be deterministic given the inputs, except for random choices provided by np.random.choice (or some other method of selecting a random index given a discrete probability distribution).
  2. Two different sequences of random choices must lead to draw_sample returning different outputs.

Next, add a UniqueRandomizer object as an input to draw_sample, and use its sample_distribution function to replace np.random.choice:

- def draw_sample(sequence_model, state):
+ def draw_sample(sequence_model, state, randomizer):
    """Draws a sample (a sequence of token indices) from the sequence model."""
    tokens = []
    token = BOS
    for i in range(MAX_LEN):
      probs, state = sequence_model(token, state)
-     token = np.random.choice(np.arange(len(probs)), p=probs)
+     token = randomizer.sample_distribution(probs)
      if token == EOS:
        break
      tokens.append(token)
    return tokens

Finally, a simple loop around draw_sample can collect unique samples, as follows:

def draw_unique_samples(model, state, num_samples):
  """Draws multiple unique samples from the sequence model."""
  samples = []
  randomizer = unique_randomizer.UniqueRandomizer()
  for _ in range(num_samples):
    samples.append(draw_sample(model, state, randomizer))
    randomizer.mark_sequence_complete()
  return samples

Code Samples

We include a few code samples that demonstrate how to use UniqueRandomizer:

  • examples/weighted_coin_flips.py: This provides a very simple example of using UniqueRandomizer. The function flip_two_weighted_coins simulates flipping a pair of weighted coins. The sample_flips_without_replacement function then uses UniqueRandomizer to efficiently sample outputs of flip_two_weighted_coins without replacement.

  • examples/expand_grammar.py: This defines a Probabilistic Context-Free Grammar (PCFG), as well as methods to sample elements of the grammar without replacement by using UniqueRandomizer, rejection sampling, and Stochastic Beam Search (SBS). The script examples/expand_grammar_main.py enables easy comparison between the different sampling methods under different scenarios.

  • examples/sequence_example.py: This implements sampling without replacement from a sequence model, using UniqueRandomizer, Batched UniqueRandomizer, rejection sampling, and SBS. The script examples/sequence_example_main.py enables easy comparison between the different sampling methods under different scenarios.

Disclaimer

This is not an officially supported Google product.

Metadata

Release files for unique-randomizer 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unique-randomizer 0.0.1
File Size Uploaded Provenance Actions
unique-randomizer-0.0.1.tar.gz 23.1 kB Without provenance Details for unique-randomizer-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unique-randomizer 0.0.1
File Interpreter ABI Platform Actions
unique_randomizer-0.0.1-py3-none-any.whl Python 3 none any Details for unique_randomizer-0.0.1-py3-none-any.whl

Total release size: 56.9 kB

Release files / unique-randomizer-0.0.1.tar.gz

Download URL unique-randomizer-0.0.1.tar.gz
Size 23.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8d0e737d05fdb530eb1f751d1f7786e32ff6af53e2c1792d1613119d735cdc44
BLAKE2b-256 checksum
How to use checksums
61722f14fdb43f05b70e8c383160849a9ed4a9b09c08bc65db79d9b8cd4ba13b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

Release files / unique_randomizer-0.0.1-py3-none-any.whl

Download URL unique_randomizer-0.0.1-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58734804fd0a277f614692a6c9a751cca68c5d93a1e4b6c2113e94c945b2c163
BLAKE2b-256 checksum
How to use checksums
5d75b0a9a977acee04db73fe89411f5a0b8bc98339ac60a0305439c82ac9c72a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

Security for unique-randomizer 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page