I found two similar questions on this topic (#197422, #199360), none of which has an answer. Neural networks in Mathematica are implemented via external library, called MXNet. Using Trace[trained[{1,2,3}]]
, you can see that the input is numerically transferred to MXNet, which evaluates the function.
For simple networks, you can get the symbolic function by extracting the relevant information from NetChain
. The following code handles only LinearLayer
and ElementwiseLayer
but can probably be adapted for other types of layers.
netApply[input_, layer_] := Switch[NetExtract[layer, "Type"],
ElementwiseLayer, NetExtract[layer, "Function"]@input,
LinearLayer, Normal[NetExtract[layer, "Weights"]] . input +
Normal[NetExtract[layer, "Biases"]]
];
symbolicNet[net_NetChain] :=
Function[Evaluate@
FunctionExpand@
Fold[netApply, Slot /@ Range@NetExtract[trained, "Input"],
NetExtract[net, All]]];
f = symbolicNet[trained]
{-1.07891 - 1.28238/(1 + E^(-0.945874 - 0.183235/( 1 + E^(0.14805 - 0.903133 #1 - 2.16383 #2 + 0.280905 #3)) - 1.79141/( 1 + E^(-0.0879924 + 1.29072 #1 - 0.38487 #2 + 1.21005 #3)))) - 0.672629/(1 + E^(-0.735348 + 0.915794/( 1 + E^(0.14805 - 0.903133 #1 - 2.16383 #2 + 0.280905 #3)) - 0.477314/( 1 + E^(-0.0879924 + 1.29072 #1 - 0.38487 #2 + 1.21005 #3)))), -0.309332 - 0.897976/(1 + E^(-0.945874 - 0.183235/( 1 + E^(0.14805 - 0.903133 #1 - 2.16383 #2 + 0.280905 #3)) - 1.79141/( 1 + E^(-0.0879924 + 1.29072 #1 - 0.38487 #2 + 1.21005 #3)))) - 1.08833/(1 + E^(-0.735348 + 0.915794/( 1 + E^(0.14805 - 0.903133 #1 - 2.16383 #2 + 0.280905 #3)) - 0.477314/( 1 + E^(-0.0879924 + 1.29072 #1 - 0.38487 #2 + 1.21005 #3))))} &
Validating the function:
f[1, 2, 3] - trained[{1, 2, 3}]
{-1.33618*10^-7, 8.15666*10^-8}
f
"? $\endgroup$f[{x_,y_,z_}] = {2x + 3z^2, 5.2y^3}
or more realisticallyf[{x_,y_,z_}] = {{1,2,3},{4,5,6},{7,8,9}}.LogisticSigmoid[{x,y,z}] + ...
Like a closed-form expression. $\endgroup$Piecewise
s allowed?The results oftrained[{5, -4000, 30}]
andtrained[{10, 10^7, 100}]
demonstrate that the dependance onx,y,z
is very weak. $\endgroup$Piecewise
s would appear with ReLU activation functions, for instance. Not sure where there would come from here. As per the weak dependence inx,y,z
, it is only the case in the simplistic toy example (with arbitrary values). $\endgroup$