Put the neural network in the Engee Function block¶
In this example, we will place the trained neural network on the Engee canvas as a block Engee Function
- generate code on Julia from it.
Task Description¶
Earlier we trained a fully connected neural network to predict the value of $y$ based on two input variables: $x_1$ and $x_2$. We already have a file model.jld2
, where the trained neural network lies. Now we will translate it into Julia code and create a block that can be used in any model or make our own library of such blocks.
⚠️ Only global addressing can be used inside the
Engee Function
block. So if you have an example located in a folder other than/user/start/data_analysis/neural_regression_to_engee_function
, you need to edit the code inside the blockEngee Function
, writing the full path to the fileneural_net_code.jl
.
Creating block mask¶
It would be convenient to place some drawing on the front side of the block, which may simplify the perception of the model. As a mask we will use the topology of our neural network.
We'll need the library ChainPlots
, which creates a neural network visualisation "recipe" for the function plot()
, after which we can simply pass the neural network as an argument to the graph output function.
Pkg.add(["JLD2", "Flux", "ChainPlots", "Symbolics"])
using Flux, JLD2, ChainPlots
model = load_object( "$(@__DIR__)/model.jld2" ) # Загрузим нейросеть из файла
gr()
p = plot( model, titlefontsize=10, size=(300,300),
xticks=:none, series_annotations="", markersize=8,
markercolor="white", markerstrokewidth=4, linewidth=1 )
savefig( p, "$(@__DIR__)/neural_net_block_mask.png"); # Сохраним график в PNG
☝️ The link to the image is already prescribed in the block mask. But the mask will not be updated automatically. To have the illustration loaded on the block face, open its properties (in mask view) and click "Update mask".
Creating Julia code for neural network¶
One feature of the Julia language will help us to create the code.
Since the library Flux
is entirely written in Julia, each layer of the neural network is described by Julia code. What happens if the input is not numbers but mathematical symbols from the library Symbolics
?
using Symbolics
@variables x1 x2
s = model( [x1, x2] );
# Первые 200 символов этого кода
print( string(s[1])[1:200], "..." )
We see a lot of ifelse
... Indeed, a ReLU-activated neural network can be rewritten as a large piecewise linear function.
Let's save the code to a .jl file¶
If we put the code in a text file, it can be included in the block Engee Function
via the command include()
, but only at the absolute address. But you will always need to keep this code file in the model folder. There is another way - put the code and mask inside the Engee Function
block and make the component self-sufficient.
open("$(@__DIR__)/neural_net_code.jl", "w") do f
println(f, "function nn(x1, x2)\n $(s[1])\nend")
end
# Сразу же запустим этот код и получим прогноз нейросети:
include("$(@__DIR__)/neural_net_code.jl")
nn(1,2)
Testing: launching a neural network from the Engee Function block¶
Let's run the model and visualise the results.
# Загрузим (если модель еще не открыта) и выполним модель
if "neural_regression_to_engee_function" ∉ getfield.(engee.get_all_models(), :name)
engee.load( "$(@__DIR__)/neural_regression_to_engee_function.engee");
end
model_data = engee.run( "neural_regression_to_engee_function" );
# Подготовим выходные переменные
model_x1 = model_data["X1"].value;
model_x2 = model_data["X2"].value;
model_y = vec( hcat( model_data["Y"].value... ));
# Построим график
scatter!( model_x1, model_x2, model_y, ms=2.5, msw=.5, leg=false, zcolor=model_y, c=:viridis,
xlimits=(-3,3), ylimits=(-3,3), title="Прогноз от блока Engee Function", titlefont=font(10) )
We see a familiar function, but each output point is "predicted" by a neural network that has been trained to model the dependence we need $y = f(x_1, x_2)$.
Conclusion¶
Using the library Symbolics
it is not difficult to generate code from the Julia function, even if this function is a neural network from the library Flux
. It remains to choose a convenient way to place the code on the canvas, and you can use the neural network as a component in a model-oriented project.