Discrete Continuous Embed Readout
Embedding and readout for simple categorical and gaussian distributions, from the language model to sophisticated robotic action spaces
Install
pip install discrete-continuous-embed-readout
Usage
Discrete
For standard autoregressive language modeling or discrete action spaces.
import torch
from discrete_continuous_embed_readout import EmbedAndReadout
# 1. Initialize
embed_readout = EmbedAndReadout(
dim = 512,
num_discrete = 20000 # vocabulary size
)
embed, readout = embed_readout
# 2. Embed
ids = torch.randint(0, 20000, (2, 1024))
embeds = embed(ids) # (2, 1024, 512)
# ... pass through your transformer / network ...
# 3. Readout
logits = readout(embeds) # (2, 1024, 20000)
# Calculate loss (automatically handles cross entropy)
labels = torch.randint(0, 20000, (2, 1024))
loss = readout(embeds, labels, return_loss = True)
loss.backward()
# Sampling and other utilities
sampled = readout.sample(logits) # (2, 1024)
log_probs = readout.log_prob(logits, sampled) # (2, 1024)
entropy = readout.entropy(logits) # (2, 1024)
Continuous
For continuous control or regression tasks.
import torch
from discrete_continuous_embed_readout import EmbedAndReadout
# 1. Initialize
embed_readout = EmbedAndReadout(
dim = 512,
num_continuous = 4, # 4 continuous dimensions
continuous_mean_std = torch.ones(4, 2) # optional mean and std for normalization
)
embed, readout = embed_readout
# 2. Embed
values = torch.randn(2, 1024, 4)
embeds = embed(values) # (2, 1024, 512)
# ... pass through network ...
# 3. Readout (returns distinct Gaussian parameters)
dist_params = readout(embeds) # (2, 1024, 4, 2) - mean and log var
# Loss (Gaussian NLL)
targets = torch.randn(2, 1024, 4)
loss = readout(embeds, targets, return_loss = True)
loss.backward()
# Sampling
sampled = readout.sample(dist_params) # (2, 1024, 4)
Mixed Discrete and Continuous
For complex environments with both discrete and continuous action spaces.
import torch
from discrete_continuous_embed_readout import EmbedAndReadout
# 1. Initialize
embed_readout = EmbedAndReadout(
dim = 512,
num_discrete = 100,
num_continuous = 4
)
embed, readout = embed_readout
# 2. Embed inputs (passed as tuple)
discrete_in = torch.randint(0, 100, (2, 32))
continuous_in = torch.randn(2, 32, 4)
embeds = embed((discrete_in, continuous_in)) # (2, 32, 512)
# ... network ...
# 3. Readout
output = readout(embeds)
# Access individual logits/params
print(output.discrete.shape) # (2, 32, 100)
print(output.continuous.shape) # (2, 32, 4, 2)
# Sampling returns tuple
sampled_discrete, sampled_continuous = readout.sample(output)
Multi-Discrete
For action spaces with multiple independent discrete actions.
import torch
from discrete_continuous_embed_readout import EmbedAndReadout
embed_readout = EmbedAndReadout(
dim = 512,
num_discrete = (10, 5, 8), # 3 independent discrete actions
use_parallel_multi_discrete = True # optimized parallel processing
)
embed, readout = embed_readout
# Input shape: (batch, seq, 3)
action_indices = torch.randint(0, 5, (2, 16, 3))
embeds = embed(action_indices)
# Readout returns list of logits if not using parallel optimization, or a special structure if so.
# However, the wrapper handles it seamlessly.
logits = readout(embeds)
sampled = readout.sample(logits) # (2, 16, 3)
Runtime Selectors
You can also define inputs dynamically at runtime if your architecture shares embeddings across different modalities.
import torch
from discrete_continuous_embed_readout import EmbedAndReadout
# 1. Initialize with the total capacity of the system
# e.g. 10 discrete embeddings total, 5 continuous dimensions total
embed_readout = EmbedAndReadout(
dim = 512,
num_discrete = 10,
num_continuous = 5,
continuous_mean_std = torch.ones(5, 2) # normalization for continuous
)
embed, readout = embed_readout
# 2. Define a Runtime Schema (Selector Config)
# This defines which specific embeddings this particular input uses.
# For example, this input uses discrete indices 0, 1, 2 and continuous indices 0, 1.
discrete_config = [[0, 1, 2]] # List of lists (for potentially multiple discrete groups)
continuous_config = [0, 1] # List of indices
selector_config = (discrete_config, continuous_config)
# 3. Create Inputs that match the schema
# Discrete: 3 values (ranges matching the config is handled by index looking up the config)
# Continuous: 2 values
# (Batch, Seq) - values must be valid for the local schema size (3)
discrete_input = torch.randint(0, 3, (2, 32))
# (Batch, Seq, 2)
continuous_input = torch.randn(2, 32, 2)
# 4. Embed with the specific selector config
embeds = embed(
(discrete_input, continuous_input),
selector_config = selector_config
)
# 5. Readout with the same selector config
logits = readout(
embeds,
selector_config = selector_config
)
# logits will be a NamedTuple with .discrete and .continuous matching the config
print(logits.discrete.shape) # (2, 32, 3) - matches discrete_config size
print(logits.continuous.shape) # (2, 32, 2, 2) - matches continuous_config size
Citations
@article{lakshminarayanan2017simple,
title = {Simple and scalable predictive uncertainty estimation using deep ensembles},
author = {Lakshminarayanan, Balaji and Pritzel, Alexander and Blundell, Charles},
journal = {Advances in neural information processing systems},
volume = {30},
year = {2017}
}
Metadata
Release files for discrete-continuous-embed-readout 0.2.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| discrete_continuous_embed_readout-0.2.12.tar.gz | 15.7 kB | Without provenance | Details for discrete_continuous_embed_readout-0.2.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| discrete_continuous_embed_readout-0.2.12-py3-none-any.whl | Python 3 | none | any | Details for discrete_continuous_embed_readout-0.2.12-py3-none-any.whl |
Total release size: 31.0 kB
Release files / discrete_continuous_embed_readout-0.2.12.tar.gz
| Download URL | discrete_continuous_embed_readout-0.2.12.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a712c40c84350ece1c82c81d81a6716862691fde04235d2ff1ae7e939649763b
|
|
BLAKE2b-256 checksum How to use checksums |
a7da9d6f9fc26aca21c950f5553f416db15e4163a5d60e7f329931ee376a891c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|
Release files / discrete_continuous_embed_readout-0.2.12-py3-none-any.whl
| Download URL | discrete_continuous_embed_readout-0.2.12-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa0c1f6e8a8b62ed3510b2c0f7077d9ce1571d36c050bf4c47067494a6f53f84
|
|
BLAKE2b-256 checksum How to use checksums |
0d9a1b22e9308cc78957c15226896055b8e4b5fa5dbb275c08cbb5f48179c1b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|
Security for discrete-continuous-embed-readout 0.2.12
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.