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.3.tar.gz (19.1 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.3-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: refrax-0.0.3.tar.gz
  • Upload date:
  • Size: 19.1 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.3.tar.gz
Algorithm Hash digest
SHA256 a597997e89012226876cf63c6fc44157a5b54c721df14b687e045abc955bfeb1
MD5 1bee6e6d00581b2bac3082961b67e061
BLAKE2b-256 6af687debc78d680764e15d5dd22f7d1482c2da6377725619a379f9e9af627bd

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: refrax-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ef655d53b8267419c004519aba204138477570a8bb50578df5ca5aea7f087d51
MD5 6a8640d8173b649b20c25eb4c071f5b7
BLAKE2b-256 03613ecf38e7ffc24695822eb31c03c4587d11c0100e93c2b109206612d7981c

See more details on using hashes here.

Provenance

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