equinox icon indicating copy to clipboard operation
equinox copied to clipboard

Error in Ensembling

Open lockwo opened this issue 3 years ago • 4 comments

When I try out the ensembling code from: https://docs.kidger.site/equinox/tricks/ (specifically the code below) I get an error (below the code). I just tested with a fresh install (eqx version 0.10.1 and jax version 0.4.4). Am I doing something wrong or has there been a recent change to how filter vmap works?

Code:


import equinox as eqx
import jax.numpy as jnp

key = jax.random.PRNGKey(0)
keys = jax.random.split(key, 8)

# Create an ensemble of models
@eqx.filter_vmap
def make_ensemble(key):
    return eqx.nn.MLP(2, 2, 2, 2, key=key)

mlp_ensemble = make_ensemble(keys)

# Evaluate each member of the ensemble on the same data
@eqx.filter_vmap(in_axes=(0, None))
def evaluate_ensemble(model, x):
    return model(x)

evaluate_ensemble(mlp_ensemble, jax.random.normal(key, (2,)))

# Evaluate each member of the ensemble on different data
@eqx.filter_vmap
def evaluate_per_ensemble(model, x):
    return model(x)

evaluate_per_ensemble(mlp_ensemble, jax.random.normal(key, (8, 2)))

Error:

IndexError                                Traceback (most recent call last)
[/usr/local/lib/python3.8/dist-packages/jax/_src/api.py](https://localhost:8080/#) in _get_axis_size(name, shape, axis)
   1789     try:
-> 1790       return shape[axis]
   1791     except (IndexError, TypeError) as e:

IndexError: tuple index out of range

The above exception was the direct cause of the following exception:

UnfilteredStackTrace                      Traceback (most recent call last)
10 frames
UnfilteredStackTrace: ValueError: vmap was requested to map its argument along axis 0, which implies that its rank should be at least 1, but is only 0 (its shape is ())

The stack trace below excludes JAX-internal frames.
The preceding is the original exception that occurred, unmodified.

--------------------

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
[/usr/local/lib/python3.8/dist-packages/equinox/vmap_pmap.py](https://localhost:8080/#) in __call__(***failed resolving arguments***)
    181         in_axes = _resolve_axes(args, in_axes)
    182 
--> 183         vmapd, static = jax.vmap(
    184             _fun_wrapper,
    185             in_axes=(in_axes,),

ValueError: vmap was requested to map its argument along axis 0, which implies that its rank should be at least 1, but is only 0 (its shape is ())

lockwo avatar Mar 08 '23 06:03 lockwo

This needs to be updated, you can use filter_vmap like this

@eqx.filter_vmap(in_axes=dict(x=None)) # or @eqx.filter_vmap(in_axes=(eqx.if_array(0), None))
def evaluate_ensemble(model, x):
    return model(x)

uuirs avatar Mar 08 '23 08:03 uuirs

Ah, this is a documentation bug. @uuirs' solution is correct. I'll update the docs now.

patrick-kidger avatar Mar 08 '23 17:03 patrick-kidger

Fixed in #280.

patrick-kidger avatar Mar 08 '23 17:03 patrick-kidger

Awesome, thanks for the quick response!

lockwo avatar Mar 09 '23 18:03 lockwo