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. This simply adds .at to your class, which 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:

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

model.dropout
# 0.1

model.core.bias
# [0.646, 0.860, 0.670 , 1.277 , 0.727]

model.head.bias
# [1.634, 1.877]

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.4.tar.gz (20.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.4-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refrax-0.0.4.tar.gz
  • Upload date:
  • Size: 20.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.4.tar.gz
Algorithm Hash digest
SHA256 0c528db697f4208a25964b4784350921c8608535c09ec1f9a17a34c8560832ca
MD5 8946362369ac0cff1b0743e71e3ebacf
BLAKE2b-256 d19293ef4ea271705fd42ecd605a5f2d1b4e70c483bb75bc419411603cd0cf15

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: refrax-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc70cf9c61cf8cfe1d26ebfa84e48ea5ebb07f8676299b0dcaa212ae8464a236
MD5 19fa88473cf57db7d748228a82c8bed3
BLAKE2b-256 43bc7127278873cb5eb8a6d9dedf35923c10b6a24acd8e45c707991cd9809b0d

See more details on using hashes here.

Provenance

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