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.2.tar.gz (19.6 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.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refrax-0.0.2.tar.gz
  • Upload date:
  • Size: 19.6 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.2.tar.gz
Algorithm Hash digest
SHA256 f536903463ce8f1b18917e0432c444c218f538f3910455f6a15ce558fa781564
MD5 6e844cd910ecd8288f872e1f198dcb83
BLAKE2b-256 7239f9ede87eaf639b1d4e2a1b8c23dc085e3eeedca9ac1129f729effe650252

See more details on using hashes here.

Provenance

The following attestation bundles were made for refrax-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: refrax-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1c46d4305ad8e73299155d18cde087f79ade679a14ef61ab5748f5282ff3ede
MD5 235b0686d27c01a40ca1c084c844404e
BLAKE2b-256 acdb7c091c95218bcaaf63595eaa2e2b7185591b52d918b607452be3ea463b13

See more details on using hashes here.

Provenance

The following attestation bundles were made for refrax-0.0.2-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