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))

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()

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())

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}
}

Download files

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

Source Distribution

mean_conc_beta-0.1.4.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

mean_conc_beta-0.1.4-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file mean_conc_beta-0.1.4.tar.gz.

File metadata

  • Download URL: mean_conc_beta-0.1.4.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for mean_conc_beta-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0cedb5b6c1c295c2bfb5f03d35fb906adf465c5d0b909797d808208e85863d36
MD5 812b48e4345b7e3c06b08e6844ffeeeb
BLAKE2b-256 7977d5034e7be037c852b48999751052c1bed2de1647ceb1ffd19f52b3e62fbd

See more details on using hashes here.

File details

Details for the file mean_conc_beta-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for mean_conc_beta-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ce03b81e128980163013154d13d6b558b54c3a7b684e96e8f20b17a4fa9c3f3d
MD5 05d867f2a22c1bf8cd9d95545a17c46e
BLAKE2b-256 56b7e4308512a790942dbe99aa2400a17384567a91678c09302d502034aab8f8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.12

2 files

0.0.11

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

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