How do I can I selectively inspect and use learned parameters in an MLJ pipeline?

From a question on slack:

If I have a pipeline like this @pipeline ContinuousEncoder(drop_last=true) Standardizer() can I specify to inverse_transform the standadizer part so that I won’t have to break up the pipeline?

1 Like

First, a note that @pipeline is now deprecated in favour of macro-free pipeline creation. See these docs. That said, I think the following example works for the old synax as well.

using MLJ

X = (
    x1 = [2.3, 4.5, 5.6],
    x2 = coerce(["a", "b", "a"], Multiclass),
);

pipe = ContinuousEncoder() |> Standardizer()
mach = machine(pipe, X) |> fit!
julia> Xout = transform(mach, X);
(x1 = [-1.0910894511799618, 0.21821789023599267, 0.8728715609439697],
 x2__a = [0.5773502691896257, -1.1547005383792512, 0.5773502691896257],
 x2__b = [-0.5773502691896256, 1.1547005383792515, -0.5773502691896256],)

# learned parameters of pipeline:
theta = fitted_params(mach);
julia> keys(theta)
(:standardizer, :continuous_encoder, :machines, :fitted_params_given_machine)

# to just *inspect* the learned standardization parameters:
julia> theta.standardizer
Dict{Symbol, Tuple{Float64, Float64}} with 3 entries:
  :x2__a => (0.666667, 0.57735)
  :x2__b => (0.333333, 0.57735)
  :x1    => (4.13333, 1.68028)

# to `inverse_transform` some data using those learned parameters:
sub_machines = theta.machines
standardizer_machine = sub_machines[2]
julia> inverse_transform(standardizer_machine, Xout)
(x1 = [2.3, 4.5, 5.6],
 x2__a = [1.0, 0.0, 1.0],
 x2__b = [0.0, 1.0, 0.0],)
4 Likes