Skip to main content

Chainable optics for JAX PyTrees

Project description

Refrax

Refrax is a tiny library implementing the optics functional pattern for JAX PyTrees.

Installation

Refrax can be installed using pip:

pip install refrax

Quick example

This example demonstrates the Mixin approach on an Equinox module, which adds .at to your class and simply returns refrax.Lens(self).

We define a dummy model with the mixin:

from refrax import OpticsMixin

import equinox as eqx
import jax

class Model(eqx.Module, OpticsMixin):
    core: eqx.nn.Linear
    head: eqx.nn.Linear
    dropout: float

key1, key2 = jax.random.split(jax.random.key(0))
model = Model(
    core=eqx.nn.Linear(in_features=5, out_features=5, key=key1),
    head=eqx.nn.Linear(in_features=5, out_features=2, key=key2),
    dropout=0.5
)

Then we can do updates using .at:

new_model = model.at.dropout.set(0.1)
new_model = model.at.select("core", "head").bias.apply(lambda b: b + 1.0)

new_model.core.bias
# [1. 1. 1. 1. 1.]

new_model.head.bias
# [1. 1.]

Documentation

Documentation is available here.

Related

The library uses Equinox (specifically eqx.tree_at) under the hood.

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

refrax-0.0.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

refrax-0.0.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file refrax-0.0.1.tar.gz.

File metadata

  • Download URL: refrax-0.0.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for refrax-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f4d66dc0c5a9f7b6614a610f3756ce39106c3018bbc9b981dc1c5314a26246cd
MD5 9a6073dd90e78e61c6c3c86ff2994e2f
BLAKE2b-256 452566a2c15646ffb300c610872111f5d637a6266a710a4b6dcc6e4a1b7fde93

See more details on using hashes here.

Provenance

The following attestation bundles were made for refrax-0.0.1.tar.gz:

Publisher: publish.yml on gvcallen/refrax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file refrax-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: refrax-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for refrax-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 80bcd3afe550526db997d4af6d2e6033cdcb8d827d8e3a2164cb0c41c514d7f2
MD5 dd82b698bd890abe276dcf96270fd3ab
BLAKE2b-256 ca99a48fae0dd075c34686a5a4ceec7d5b95c39dab88896373ed1bfbd33056dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for refrax-0.0.1-py3-none-any.whl:

Publisher: publish.yml on gvcallen/refrax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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