Converting SymbolicNeuralNetwork to numeric function

Hi all,

I have a symbolic neural network, parameterised by theta=theta_vals which I wish to evaluate at some point x. I construct the neural network as:

using Lux, ModelingToolkit, ModelingToolkitNeuralNets

nn_arch = Lux.Chain(
    Lux.Dense(1 => 3, Lux.softplus, use_bias = true),
    Lux.Dense(3 => 1, Lux.softplus, use_bias = true)
)

nn, theta = SymbolicNeuralNetwork(; 
    nn_p_name = :theta,
    chain = nn_arch,
    n_input = 1,
    n_output = 1
    )
    
theta_vals = ModelingToolkit.getdefault(theta)

I tried to convert nn to a function I could evaluate numerically by running

@variables x[1]
nn_num = ModelingToolkit.build_function(nn.f, theta, x; expression = Val{false})
nn_num(theta_vals, [1.0])

But this threw

ERROR: UndefVarError: `NN` not defined in `Symbolics`

I’m not sure where I’m going wrong here - how can I construct the parameterised neural network function that I can evaluate numerically?

Thanks!

Is there a reason you used nn.f instead of nn?

Not particularly - I did try using nn instead, but got the same error.

When I run typeof(nn) I get:

Symbolics.CallWithMetadata{SymbolicUtils.FnType{Tuple, Vector{Real}, ModelingToolkitNeuralNets.StatelessApplyWrapper{Chain{@NamedTuple{layer_1::Dense{typeof(softplus), Int64, Int64, Nothing, Nothing, Static.True}, layer_2::Dense{typeof(softplus), Int64, Int64, Nothing, Nothing, Static.True}}, Nothing}}}, Base.ImmutableDict{DataType, Any}}

and I think I want to ‘get at’ the ModelingToolkitNeuralNets...Static.True}}, Nothing}} bit, which I would hope to pass parameters and inputs to.