-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add metadata and getter functions to check what type of NN parameter the package generates #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d96f68
init
TorkelE 963c5d2
add documentation
TorkelE 1f71672
Update src/nn_par_metadata.jl
TorkelE 843eed5
Update src/nn_par_metadata.jl
TorkelE deb40c9
add has... functions
TorkelE 36b24f8
add get_nn_chain
TorkelE eb3c95d
remove hasneuralnetowkr and hasneuralnetworkps to pass tests
TorkelE 479bcd4
remove functions from doc page as well
TorkelE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| ### Defines Metadata Type ### | ||
| struct NeuralNetworkParameter end | ||
| struct NeuralNetworkParametrisation end | ||
| Symbolics.option_to_metadata_type(::Val{:neuralnetwork}) = NeuralNetworkParameter | ||
| Symbolics.option_to_metadata_type(::Val{:neuralnetworkps}) = NeuralNetworkParametrisation | ||
|
|
||
| ### Defines Metadata Getters ### | ||
| """ | ||
| ModelingToolkitNeuralNets.isneuralnetwork(p) | ||
|
|
||
| Returns `true` if the parameter corresponds to the neural network chain that is saved as a MTK parameter. This function is primarily intended for internal use within dependent packages. | ||
|
|
||
| Example: | ||
| ```julia | ||
| @parameters d | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
| ModelingToolkitNeuralNets.isneuralnetwork(d) # false | ||
| ModelingToolkitNeuralNets.isneuralnetwork(NN) # true | ||
| ModelingToolkitNeuralNets.isneuralnetwork(θ) # false | ||
| ```` | ||
| """ | ||
| isneuralnetwork(p::Union{Symbolics.Num, Symbolics.Arr, Symbolics.CallAndWrap}) = isneuralnetwork(Symbolics.unwrap(p)) | ||
| function isneuralnetwork(p::Symbolics.SymbolicT) | ||
| getmetadata(p, NeuralNetworkParameter, false) | ||
| end | ||
|
|
||
| """ | ||
| ModelingToolkitNeuralNets.isneuralnetworkps(p) | ||
|
|
||
| Returns `true` if the parameter corresponds to the a neural network parametrisation. This function is primarily intended for internal use within dependent packages. | ||
|
|
||
| Example: | ||
| ```julia | ||
| @parameters d | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
| ModelingToolkitNeuralNets.isneuralnetworkps(d) # false | ||
| ModelingToolkitNeuralNets.isneuralnetworkps(NN) # false | ||
| ModelingToolkitNeuralNets.isneuralnetworkps(θ) # true | ||
| ```` | ||
| """ | ||
| isneuralnetworkps(p::Union{Symbolics.Num, Symbolics.Arr, Symbolics.CallAndWrap}) = isneuralnetworkps(Symbolics.unwrap(p)) | ||
| function isneuralnetworkps(p::Symbolics.SymbolicT) | ||
| getmetadata(p, NeuralNetworkParametrisation, false) | ||
| end | ||
|
|
||
|
|
||
| ### Defines Other Accessors ### | ||
|
|
||
| """ | ||
| ModelingToolkitNeuralNets.get_nn_chain(p) | ||
|
|
||
| For a neural network parameter `p` (i.e. such that `isneuralnetwork(p) == true`), return the associated neural network chain. This function is primarily intended for internal use within dependent packages. | ||
|
|
||
| Example: | ||
| ```julia | ||
| chain = Lux.Chain( | ||
| Lux.Dense(1 => 3, Lux.softplus; use_bias = false), | ||
| Lux.Dense(3 => 1, Lux.softplus; use_bias = false), | ||
| ) | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
|
|
||
| ModelingToolkitNeuralNets.get_nn_chain(NN) # Returns `chain`. | ||
| ModelingToolkitNeuralNets.get_nn_chain(θ) # Throws an error. | ||
| ```` | ||
| """ | ||
| get_nn_chain(p::Union{Symbolics.Num, Symbolics.Arr, Symbolics.CallAndWrap}) = get_nn_chain(Symbolics.unwrap(p)) | ||
| function get_nn_chain(p::Symbolics.SymbolicT) | ||
| isneuralnetwork(p) || error("Parameter $p does not have a neural network chain associated with it.") | ||
| return getmetadata(p, Symbolics.VariableDefaultValue).lux_model | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # Fetch packages. | ||
| using ModelingToolkitBase, ModelingToolkitNeuralNets, Lux, Random | ||
| using ModelingToolkitBase: t_nounits as t, D_nounits as D | ||
|
|
||
| # Check that `isneuralnetwork` and `isneuralnetworkps` give correct input on various inputs. | ||
| let | ||
| # Tests on normally declared parameters. | ||
| @variables X(t) Y(t)[1:2] | ||
| @parameters p q[1:3] | ||
| for s in [X, Y, Y[1], p, q, q[1]] | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(s) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(s) | ||
| end | ||
|
|
||
| # Tests on MTKNeuralNets parameters | ||
| chain = Lux.Chain( | ||
| Lux.Dense(1 => 3, Lux.softplus; use_bias = false), | ||
| Lux.Dense(3 => 1, Lux.softplus; use_bias = false), | ||
| ) | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
| U, p = SymbolicNeuralNetwork(; chain, n_input = 1, n_output = 1, nn_name = :U, nn_p_name = :p) | ||
| @test ModelingToolkitNeuralNets.isneuralnetwork(NN) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(θ) | ||
| @test ModelingToolkitNeuralNets.isneuralnetwork(U) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(p) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(NN) | ||
| @test ModelingToolkitNeuralNets.isneuralnetworkps(θ) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(U) | ||
| end | ||
|
|
||
| # Check that `isneuralnetwork` and `isneuralnetworkps` give correct input on parameters stored in a model created using symbolic approach. | ||
| let | ||
| # Model created via symbolic neural network representation. | ||
| chain = Lux.Chain( | ||
| Lux.Dense(1 => 3, Lux.softplus; use_bias = false), | ||
| Lux.Dense(3 => 1, Lux.softplus; use_bias = false), | ||
| ) | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
| @variables X(t) Y(t) | ||
| @parameters d | ||
| eqs = [ | ||
| D(X) ~ NN([X], θ)[1] - d*X | ||
| D(Y) ~ X - d*Y | ||
| ] | ||
| @mtkcompile sys = System(eqs, t) | ||
|
|
||
| # Check that content have the correct metadata tags. | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys.X) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys.d) | ||
| @test ModelingToolkitNeuralNets.isneuralnetwork(sys.NN) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys.θ) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys.X) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys.d) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys.NN) | ||
| @test ModelingToolkitNeuralNets.isneuralnetworkps(sys.θ) | ||
| end | ||
|
|
||
| # Check that `isneuralnetwork` and `isneuralnetworkps` give correct input on parameters stored in a model created using NNBlock approach. | ||
| let | ||
| # Model created via NeuralNetwork block. | ||
| chain = Lux.Chain( | ||
| Lux.Dense(2 => 3, Lux.softplus; use_bias = false), | ||
| Lux.Dense(3 => 2, Lux.softplus; use_bias = false), | ||
| ) | ||
| @variables x(t) = 3.1 y(t) = 1.5 | ||
| @parameters α = 1.3 [tunable = false] δ = 1.8 [tunable = false] | ||
| @named nn = NeuralNetworkBlock(2, 2; chain) | ||
| eqs = [ | ||
| D(x) ~ α * x + nn.outputs[1], | ||
| D(y) ~ -δ * y + nn.outputs[2], | ||
| nn.inputs[1] ~ x, | ||
| nn.inputs[2] ~ y, | ||
| ] | ||
| @mtkcompile sys_nnblock = System(eqs, t, systems = [nn]) | ||
|
|
||
| # Check that content have the correct metadata tags. | ||
| @test ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.nn.lux_apply) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.nn.lux_model) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.nn.p) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.nn.T) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.α) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.δ) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.x) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetwork(sys_nnblock.y) | ||
|
|
||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.nn.lux_apply) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.nn.lux_model) | ||
| @test ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.nn.p) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.nn.T) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.α) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.δ) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.x) | ||
| @test !ModelingToolkitNeuralNets.isneuralnetworkps(sys_nnblock.y) | ||
| end | ||
|
|
||
| # Checks the `get_nn_chain` accessor function. | ||
| let | ||
| # Model created via symbolic neural network representation. | ||
| chain = Lux.Chain( | ||
| Lux.Dense(1 => 3, Lux.softplus; use_bias = false), | ||
| Lux.Dense(3 => 1, Lux.softplus; use_bias = false), | ||
| ) | ||
| @SymbolicNeuralNetwork NN, θ = chain | ||
| @variables X(t) Y(t) | ||
| @parameters d | ||
| eqs = [ | ||
| D(X) ~ NN([X], θ)[1] - d*X | ||
| D(Y) ~ X - d*Y | ||
| ] | ||
| @mtkcompile sys = System(eqs, t) | ||
|
|
||
| # Checks accessor function. | ||
| @test ModelingToolkitNeuralNets.get_nn_chain(NN) == chain | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(θ) | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(X) | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(d) | ||
| @test ModelingToolkitNeuralNets.get_nn_chain(sys.NN) == chain | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(sys.θ) | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(sys.X) | ||
| @test_throws ErrorException ModelingToolkitNeuralNets.get_nn_chain(sys.d) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.