Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Inference ModelOp demo

Open In Colab

This notebook shows how to wrap a trained sklearn estimator and a small JAX model as xrtoolz Layer-1 operators with ModelOp, then compose them in a Graph together with an RMSE metric.

Key points (per design decision D4):

  • xrtoolz.inference.ModelOp is framework-agnostic — it never imports sklearn, jax, torch, or equinox at module load time.

  • SklearnModelOp and JaxModelOp are thin subclasses; backend imports happen lazily on first use.

  • Inputs are xr.DataArray / xr.Dataset with a feature dim; outputs come back as xr.DataArray carrying the input’s non-feature coordinates.

1. Synthetic data

Build a small regression dataset as an xr.DataArray with (sample, feature) dims and a matching target.

Loading...

2. Wrap a fitted scikit-learn model with SklearnModelOp

SklearnModelOp(model='<model>', method='predict', feature_dim='feature', output_name='prediction', output_dim='output')
Loading...

Notice that the output is an xr.DataArray on the same sample axis as the input. The model itself was never serialized; get_config() captures only the wrapper config:

{'model': '<model>', 'method': 'predict', 'feature_dim': 'feature', 'output_name': 'prediction', 'output_dim': 'output'}

3. A tiny JAX model wrapped with JaxModelOp

We define a minimal MLP as a plain pytree-callable. JaxModelOp JIT-compiles it on first use; jax is imported lazily.

JaxModelOp(model='<model>', method=None, feature_dim='feature', output_name='prediction', output_dim='output', jit=True)
Loading...

4. Compose both wrappers in a single Graph with RMSE

Both ModelOp instances are full Layer-1 operators, so they slot into the functional Graph API alongside any other operator — here the RMSE metric from xrtoolz.metrics.

Graph (2 inputs, 4 outputs):
  Inputs: ['X', 'Y']
  [2] AsDataset(name='y') ← ['Y']
  [3] AsDataset(name='y') ← ['Y']
  [4] JaxModelOp(model='<model>', method=None, feature_dim='feature', output_name='prediction', output_dim='output', jit=True) ← ['X']
  [5] SklearnModelOp(model='<model>', method='predict', feature_dim='feature', output_name='prediction', output_dim='output') ← ['X']
  [6] AsDataset(name='y') ← ['<node 125852674600912>']
  [7] AsDataset(name='y') ← ['<node 125849727031952>']
  [8] RMSE(variable='y', dims='sample') ← ['<node 125849730559312>', '<node 125849727648096>']
  [9] RMSE(variable='y', dims='sample') ← ['<node 125849726853008>', '<node 125849726933920>']
  Outputs: ['lr_pred', 'mlp_pred', 'lr_rmse', 'mlp_rmse']
{'lr_rmse': 0.10134100923089427, 'mlp_rmse': 0.1696163804879099}

5. The wrapper is fully introspectable

Both wrappers expose JSON-serializable configs (the model itself is referenced as the literal string "<model>"):

[
  {
    "model": "<model>",
    "method": "predict",
    "feature_dim": "feature",
    "output_name": "prediction",
    "output_dim": "output"
  },
  {
    "model": "<model>",
    "method": null,
    "feature_dim": "feature",
    "output_name": "prediction",
    "output_dim": "output",
    "jit": true
  }
]