Skip to main content

Python library for RAD Embeddings, provably correct latent DFA representations.

Project description

Rad Embeddings Logo

This repo contains a JAX implementation of RAD embeddings, see project webpage for more information.

Installation

Install using pip.

pip install rad-embeddings # CPU-only
pip install rad-embeddings[cuda] # With CUDA

Usage

Instantiate a pretrained encoder.

from rad_embeddings import Encoder

encoder = Encoder() # Loads a pretrained DFA encoder with default parameters: handles at most 10-state DFAs with 10-token alphabets

Use DFAx to sample DFAs.

import jax
from dfax.samplers import ReachSampler, ReachAvoidSampler, RADSampler

sampler = RADSampler()
key = jax.random.PRNGKey(42)

key, subkey = jax.random.split(key)
dfax = sampler.sample(subkey)

Pass the DFA to the encoder to get its embedding — both DFAx and DFA objects are supported.

from dfax import dfax2dfa

rad_embed_from_dfax = encoder(dfax)
rad_embed_from_dfa = encoder(dfax2dfa(dfax)) # Returns the same embedding

Compute bisimulation distance between two DFA embeddings.

key, subkey = jax.random.split(key)
dfax_l = sampler.sample(subkey)
rad_l = encoder(dfax_l)

key, subkey = jax.random.split(key)
dfax_r = sampler.sample(subkey)
rad_r = encoder(dfax_r)

distance = encoder.distance(rad_l, rad_r)

Solve a one-step bisimulation problem.

from dfax import DFAx
import jax.numpy as jnp

# Reach token 1 and then token 2 while avoding token 9
dfa_l = DFAx.create(
	start = 0,
	transitions = jnp.array([
		[0, 1, 0, 0, 0, 0, 0, 0, 0, 3],
		[1, 1, 2, 1, 1, 1, 1, 1, 1, 3],
		[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
		[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
		[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
	]),
	labels = jnp.array([False, False, True, False, False])
)

# Reach token 9
dfa_r = DFAx.create(
	start = 0,
	transitions = jnp.array([
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
		[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
		[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
		[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
		[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
	]),
	labels = jnp.array([False, True, False, False, False])
)

distinguishing_action = encoder.solve(dfa_l, dfa_r) # Returns 1 as token 1 is the one-step distinguishing action

Train a new encoder — see encoder.py for the default arguments of the EncoderModule.train class method.

from rad_embeddings import EncoderModule

EncoderModule.train(max_size=5, n_tokens=5, debug=True, save_dir="my_storage")

Load your trained parameters.

from rad_embeddings import Encoder

encoder = Encoder(max_size=5, n_tokens=5, storage_dir="my_storage")

Citation

Please cite the following papers if you use RAD Embeddings in your work.

@inproceedings{DBLP:conf/nips/YalcinkayaLVS24,
  author       = {Beyazit Yalcinkaya and
                  Niklas Lauffer and
                  Marcell Vazquez{-}Chanlatte and
                  Sanjit A. Seshia},
  title        = {Compositional Automata Embeddings for Goal-Conditioned Reinforcement
                  Learning},
  booktitle    = {NeurIPS},
  year         = {2024}
}
@inproceedings{DBLP:conf/neus/YalcinkayaLVS25,
  author       = {Beyazit Yalcinkaya and
                  Niklas Lauffer and
                  Marcell Vazquez{-}Chanlatte and
                  Sanjit A. Seshia},
  title        = {Provably Correct Automata Embeddings for Optimal Automata-Conditioned
                  Reinforcement Learning},
  booktitle    = {NeuS},
  series       = {Proceedings of Machine Learning Research},
  volume       = {288},
  pages        = {661--675},
  publisher    = {{PMLR}},
  year         = {2025}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rad_embeddings-0.2.7.tar.gz (13.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rad_embeddings-0.2.7-py3-none-any.whl (13.1 MB view details)

Uploaded Python 3

File details

Details for the file rad_embeddings-0.2.7.tar.gz.

File metadata

  • Download URL: rad_embeddings-0.2.7.tar.gz
  • Upload date:
  • Size: 13.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for rad_embeddings-0.2.7.tar.gz
Algorithm Hash digest
SHA256 fe76e7f602cb2a4f002dc52df739753591bf5182014479d3f96a60dbf229f12d
MD5 b49ef936ae397b8d8e6425c9dd4d5724
BLAKE2b-256 280c292a925c6723fe693efd745bfbacb249cc1de8b7d616406ece5820c5d6b0

See more details on using hashes here.

File details

Details for the file rad_embeddings-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for rad_embeddings-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6c4f410ca56e20ca2da5cbd00ccfdf3dc664cdd44d4de3a10432d4f4afe84722
MD5 28ec8677cf3a952bf95449b2dcda3ae6
BLAKE2b-256 d700d7846c77b49740b62de8c96949454034dbd68e9198e63dddef07273f21ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page