Skip to main content

mean-conc-beta

Beta distribution parameterized by mean and concentration for bounded continuous action spaces in reinforcement learning.

Install

$ pip install mean-conc-beta

Usage

import torch
from mean_conc_beta import Beta

# defaults to (-1., 1.), most continuous action spaces are symmetric centered on 0, sometimes off by a scale
# but you can pass in custom bounds, e.g. Beta((-0.4, 0.4))

# the entropy bonus only regularizes concentration by default, without moving the mean

beta = Beta()

# network output: (batch, num_actions, 2) for raw mean and concentration

params = torch.randn(16, 4, 2, requires_grad = True)

# distribution on the action bounds

dist = beta(params)

# sample actions

actions = dist.sample()
actions_reparam = dist.rsample()

# joint log prob and entropy over the action dimensions

log_prob = dist.log_prob(actions).sum(dim = -1)
entropy = dist.entropy().sum(dim = -1)

# auto-rescale actions directly into env.step, with optional clipping

env_step = beta.rescale_env_step(env.step, target_range = (-2.1, 2.1), clip = (-2., 2.))
next_obs, reward, term, trunc, info = env_step(actions)

# behavior cloning with mse loss on mean

expert_actions = torch.rand(16, 4) * 2. - 1.

pred_mean = beta.mean(params)

bc_loss = (pred_mean - expert_actions).pow(2).mean()
bc_loss.backward()

Greedy actions and uniform prior

# temperature = 0. gives a point mass at the mean, useful for evaluation

action = beta(params, temperature = 0.).mean

# kl against the uniform on the bounds, for prior regularization

prior_loss = beta.kl_uniform(params).mean()

Per-action bounds

Bounds must have shape (2,) for a single (low, high) pair, or (num_actions, 2) for a stack of per-action pairs.

beta = Beta(bounds = [(-1., 1.), (0., 1.), (-2., 2.)])

params = torch.randn(16, 3, 2)
actions = beta(params).sample()

Mean Squashing

The raw mean is squashed onto the bounds with LeakyTanh by default - the exact tanh forward, with the backward gradient floored at leak so a policy saturated at a bound can still be pulled back. It can be swapped for plain tanh or any other squash:

from mean_conc_beta import Beta, LeakyTanh

beta = Beta(squash_fn = 'tanh')
beta = Beta(squash_fn = LeakyTanh(leak = 0.1))
beta = Beta(squash_fn = 'softsign')
beta = Beta(squash_fn = lambda x: x / (1. + x ** 2).sqrt())

Mean perturbation

For update-time mean perturbation, as in Robust Policy Optimization, the concentration is held fixed and the mean is shifted by uniform noise in action space:

dist = beta(params, perturb_mean_scale = 0.5)
log_prob = dist.log_prob(actions)

The explicit beta.perturb_mean(params, scale = 0.5) path also supports apply_unimodal and a generator.

Citations

@article{Ferrari2004BetaRF,
    title   = {Beta Regression for Modelling Rates and Proportions},
    author  = {Silvia L. P. Ferrari and Francisco Cribari-Neto},
    journal = {Journal of Applied Statistics},
    year    = {2004},
    volume  = {31},
    pages   = {799 - 815}
}
@inproceedings{Chou2017TheBP,
    title   = {The Beta Policy for Continuous Reinforcement Learning},
    author  = {Po-Wei Chou and Daniel Maturana and Sebastian Scherer},
    booktitle = {International Conference on Machine Learning},
    year    = {2017}
}
@article{rahman2022robust,
    title   = {Robust Policy Optimization in Deep Reinforcement Learning},
    author  = {Rahman, Md Masudur and Xue, Yexiang},
    journal = {arXiv preprint arXiv:2212.07536},
    year    = {2022}
}

Metadata

Release files for mean-conc-beta 0.2.14

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

Source distribution (sdist)

Source distribution for mean-conc-beta 0.2.14
File Size Uploaded Provenance Actions
mean_conc_beta-0.2.14.tar.gz 12.8 kB Without provenance Details for mean_conc_beta-0.2.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mean-conc-beta 0.2.14
File Interpreter ABI Platform Actions
mean_conc_beta-0.2.14-py3-none-any.whl Python 3 none any Details for mean_conc_beta-0.2.14-py3-none-any.whl

Total release size: 25.0 kB

Release files / mean_conc_beta-0.2.14.tar.gz

Download URL mean_conc_beta-0.2.14.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
428322dc7ae5638ee5014dc3eac23f7ba9f51bc5bd92cc81341440460defa3b1
BLAKE2b-256 checksum
How to use checksums
f3447749966c32c5943f1625b51a23ea27e92f18b00b5c3fb2e61e3e6be29277
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / mean_conc_beta-0.2.14-py3-none-any.whl

Download URL mean_conc_beta-0.2.14-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f282d38ab00a3031db8968bb3fe0f41d2bd4446e88898b9f0defda1e2d6d134c
BLAKE2b-256 checksum
How to use checksums
4237cc4d40d3c6020e72e8dc43b096a80a3ae4c2856cf4860ac33db5da2804d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Security for mean-conc-beta 0.2.14

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.

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