Skip to main content

Chainable optics for JAX PyTrees

Project description

Refrax

Refrax is a small, pragmatic library implementing the optics pattern for JAX PyTrees.

It focuses on elegant syntax and composable conditional chains to provide easy yet powerful PyTree manipulation.

Installation

Refrax can be installed using pip:

pip install refrax

Quick example

import equinox as eqx
import jax

class Model(eqx.Module):
    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 focus:

from refrax import focus

model = focus(model).dropout.set(0.1)
model = focus(model).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]

One of the useful optics methods is simply traversing over a PyTree's leaves:

len(focus(model).leaves().get())
# 5

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.6.tar.gz (21.3 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.6-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refrax-0.0.6.tar.gz
  • Upload date:
  • Size: 21.3 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.6.tar.gz
Algorithm Hash digest
SHA256 f5090adda4c37d619005854c4254c27bad9fcbbd926fb13861f4dc8c64c87b01
MD5 9781fe224213bad853011fb4d31ecbdf
BLAKE2b-256 322711b6e22ca97c0901647c20a8f1c351dedd4638d8bb1a52e078347212f073

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: refrax-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 94f20b96d0bc35b1ca52c3eabb858df84b55d9397e77dbef68325deba1b8cf36
MD5 ccaa6b788a147ea1e974a4daa1290ebf
BLAKE2b-256 8b13e5099e033d55b3c9614165c5414f78e6d213adbc9bb42a67413b3e77a64d

See more details on using hashes here.

Provenance

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