You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The main export of this package is the ````ComponentArray```` type. "Components" of ````ComponentArray````s
22
-
are really just array blocks that can be accessed through a named index. This will create a new ```ComponentArray``` whose data is a view into the original,
9
+
The main export of this package is the ``ComponentArray`` type. "Components" of ``ComponentArray``s
10
+
are really just array blocks that can be accessed through a named index. This will create a new `ComponentArray` whose data is a view into the original,
23
11
allowing for standalone models to be composed together by simple function composition. In
24
-
essence, ```ComponentArray```s allow you to do the things you would usually need a modeling
12
+
essence, `ComponentArray`s allow you to do the things you would usually need a modeling
25
13
language for, but without actually needing a modeling language. The main targets are for use
26
14
in [DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl) and
27
15
[Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl), but anything that requires
28
16
flat vectors is fair game.
29
17
30
18
Check out the [NEWS](https://github.com/SciML/ComponentArrays.jl/blob/master/NEWS.md) for new features by minor release version.
31
19
32
-
33
20
## General use
34
-
The easiest way to construct 1-dimensional ```ComponentArray```s (aliased as `ComponentVector`) is as if they were ```NamedTuple```s. In fact, a good way to think about them is as arbitrarily nested, mutable ```NamedTuple```s that can be passed through a solver.
21
+
22
+
The easiest way to construct 1-dimensional `ComponentArray`s (aliased as `ComponentVector`) is as if they were `NamedTuple`s. In fact, a good way to think about them is as arbitrarily nested, mutable `NamedTuple`s that can be passed through a solver.
23
+
35
24
```julia
36
-
julia> c = (a=2, b=[1, 2]);
25
+
julia> c = (a=2, b=[1, 2]);
37
26
38
-
julia> x =ComponentArray(a=5, b=[(a=20., b=0), (a=33., b=0), (a=44., b=3)], c=c)
27
+
julia> x =ComponentArray(a=5, b=[(a=20.0, b=0), (a=33.0, b=0), (a=44.0, b=3)], c=c)
39
28
ComponentVector{Float64}(a =5.0, b = [(a =20.0, b =0.0), (a =33.0, b =0.0), (a =44.0, b =3.0)], c = (a =2.0, b = [1.0, 2.0]))
40
29
41
-
julia> x.c.a =400; x
30
+
julia> x.c.a =400;
31
+
x
42
32
ComponentVector{Float64}(a =5.0, b = [(a =20.0, b =0.0), (a =33.0, b =0.0), (a =44.0, b =3.0)], c = (a =400.0, b = [1.0, 2.0]))
julia>typeof(similar(x, Int32)) ===typeof(ComponentVector{Int32}(a =5, b = [
51
+
(a =20.0, b =0), (a =33.0, b =0), (a =44.0, b =3)], c = c))
61
52
true
62
53
```
54
+
63
55
`ComponentArray`s can be constructed from existing
64
56
`ComponentArray`s (currently nested fields cannot be changed this way):
57
+
65
58
```julia
66
-
julia> x =ComponentVector(a=1, b=2, c=3);
59
+
julia> x =ComponentVector(a=1, b=2, c=3);
67
60
68
-
julia>ComponentVector(x; a=11, new=42)
61
+
julia>ComponentVector(x; a=11, new=42)
69
62
ComponentVector{Int64}(a =11, b =2, c =3, new =42)
70
63
```
71
64
72
-
Higher dimensional ```ComponentArray```s can be created too, but it's a little messy at the moment. The nice thing for modeling is that dimension expansion through broadcasted operations can create higher-dimensional ```ComponentArray```s automatically, so Jacobian cache arrays that are created internally with ```false .* x .* x'``` will be two-dimensional ```ComponentArray```s (aliased as `ComponentMatrix`) with proper axes. Check out the [ODE with Jacobian](https://github.com/jonniedie/ComponentArrays.jl/blob/master/examples/ODE_jac_example.jl) example in the examples folder to see how this looks in practice.
65
+
Higher dimensional `ComponentArray`s can be created too, but it's a little messy at the moment. The nice thing for modeling is that dimension expansion through broadcasted operations can create higher-dimensional `ComponentArray`s automatically, so Jacobian cache arrays that are created internally with `false .* x .* x'` will be two-dimensional `ComponentArray`s (aliased as `ComponentMatrix`) with proper axes. Check out the [ODE with Jacobian](https://github.com/jonniedie/ComponentArrays.jl/blob/master/examples/ODE_jac_example.jl) example in the examples folder to see how this looks in practice.
66
+
73
67
```julia
74
-
julia> x =ComponentArray(a=1, b=[2, 1, 4.0], c=c)
68
+
julia> x =ComponentArray(a=1, b=[2, 1, 4.0], c=c)
75
69
ComponentVector{Float64}(a =1.0, b = [2.0, 1.0, 4.0], c = (a =2.0, b = [1.0, 2.0]))
76
70
77
71
julia> x2 = x .* x'
@@ -84,42 +78,42 @@ julia> x2 = x .* x'
84
78
1.02.01.04.02.01.02.0
85
79
2.04.02.08.04.02.04.0
86
80
87
-
julia> x2[:c,:c]
81
+
julia> x2[:c,:c]
88
82
3×3 ComponentMatrix{Float64} with axes Axis(a =1, b =2:3) ×Axis(a =1, b =2:3)
89
83
4.02.04.0
90
84
2.01.02.0
91
85
4.02.04.0
92
86
93
-
julia> x2[:a,:a]
87
+
julia> x2[:a,:a]
94
88
1.0
95
89
96
-
julia>@view x2[:a,:c]
90
+
julia>@view x2[:a,:c]
97
91
ComponentVector{Float64,SubArray...}(a =2.0, b = [1.0, 2.0])
98
92
99
-
julia> x2[:b,:c]
93
+
julia> x2[:b,:c]
100
94
3×3 ComponentMatrix{Float64} with axes FlatAxis() ×Axis(a =1, b =2:3)
101
95
4.02.04.0
102
96
2.01.02.0
103
97
8.04.08.0
104
98
```
105
99
106
-
107
100
## Examples
101
+
108
102
### Differential equation example
109
-
This example uses ```@unpack``` from [Parameters.jl](https://github.com/mauro3/Parameters.jl)
103
+
104
+
This example uses `@unpack` from [Parameters.jl](https://github.com/mauro3/Parameters.jl)
Notice how cleanly the ```composed!``` function can pass variables from one function to another with no array index juggling in sight. This is especially useful for large models as it becomes harder to keep track top-level model array position when adding new or deleting old components from the model. We could go further and compose ```composed!``` with other components ad (practically) infinitum with no mental bookkeeping.
166
+
Notice how cleanly the `composed!` function can pass variables from one function to another with no array index juggling in sight. This is especially useful for large models as it becomes harder to keep track top-level model array position when adding new or deleting old components from the model. We could go further and compose `composed!` with other components ad (practically) infinitum with no mental bookkeeping.
176
167
177
-
The main benefit, however, is now our differential equations are unit testable. Both ```lorenz``` and ```lotka``` can be run as their own ```ODEProblem``` with ```f``` set to zero to see the unforced response.
168
+
The main benefit, however, is now our differential equations are unit testable. Both `lorenz` and `lotka` can be run as their own `ODEProblem` with `f` set to zero to see the unforced response.
Next we'll make a function that creates dense neural layer components. It is similar to `Flux.Dense`, except it doesn't handle the activation function. We'll do that separately.
We now have convenient struct-like access to the weights and biases of the layers for our neural ODE function while giving our optimizer something that acts like a flat array.
0 commit comments