Advanced Graph Functions
Some more advanced functions for quering a graph.
Imported to namespace by
using NaiveNASlib.Advanced
NaiveNASlib.findterminating
— Functionfindterminating(v::AbstractVertex, direction::Function, other::Function= v -> [], visited = [])
Return an array of all vertices which terminate size changes (i.e does not propagate them) seen through the given direction (typically inputs
or outputs
). A vertex will be present once for each unique path through which its seen.
The other
direction may be specified and will be traversed if a SizeInvariant
vertex is encountered.
Will return the given vertex if it is terminating.
Examples
julia> using NaiveNASlib, NaiveNASlib.Advanced
julia> v1 = inputvertex("v1", 3);
julia> v2 = inputvertex("v2", 3);
julia> v3 = conc(v1,v2,v1,dims=1);
julia> name.(findterminating(v1, outputs, inputs))
1-element Vector{String}:
"v1"
julia> name.(findterminating(v3, outputs, inputs))
Any[]
julia> name.(findterminating(v3, inputs, outputs))
3-element Vector{String}:
"v1"
"v2"
"v1"
julia> v5 = v3 + inputvertex("v4", 9);
julia> name.(findterminating(v3, outputs, inputs))
1-element Vector{String}:
"v4"
NaiveNASlib.all_in_graph
— Functionall_in_graph(v::AbstractVertex)
Return an array of vertices in the same graph (or connected component) as v
NaiveNASlib.output!
— Functionoutput!(memo::AbstractDict{K, V}, v::AbstractVertex) where {K,V}
Return the output from v given any input in memo by traversing the graph. Intermediate results from all visited vertices will be stored in memo after function exits.
Examples
julia> using NaiveNASlib, NaiveNASlib.Advanced, NaiveNASlib.Extend
julia> ivs = InputVertex.(1:2);
julia> v1 = CompVertex(*, ivs...);
julia> v2 = CompVertex(-, v1, ivs[1]);
julia> results = Dict{AbstractVertex, Any}(zip(ivs, [2,3]));
julia> output!(results, v2)
4
julia> Pair{AbstractVertex, Int}[v=>results[v] for v in ancestors(v2)]
4-element Vector{Pair{AbstractVertex, Int64}}:
InputVertex(1) => 2
InputVertex(2) => 3
CompVertex(*, inputs=[InputVertex(1), InputVertex(2)]) => 6
CompVertex(-, inputs=[CompVertex(*), InputVertex(1)]) => 4
NaiveNASlib.ancestors
— Functionancestors(v::AbstractVertex)
Return an array of all ancestors of v
, including v
itself.
Examples
julia> using NaiveNASlib, NaiveNASlib.Advanced, NaiveNASlib.Extend
julia> ancestors(invariantvertex(+, inputvertex("in", 1)))
2-element Vector{AbstractVertex}:
InputSizeVertex(InputVertex(in, outputs=[CompVertex(+)]), 1)
MutationVertex(CompVertex(+, inputs=[in], outputs=[]), SizeInvariant())
NaiveNASlib.descendants
— Functiondescendants(v::AbstractVertex)
Return an array of all descendants of v
, including v
itself.
Examples
julia> using NaiveNASlib, NaiveNASlib.Advanced, NaiveNASlib.Extend
julia> descendants(invariantvertex(+, inputvertex("in", 1)) |> inputs |> first)
2-element Vector{AbstractVertex}:
MutationVertex(CompVertex(+, inputs=[in], outputs=[]), SizeInvariant())
InputSizeVertex(InputVertex(in, outputs=[CompVertex(+)]), 1)
NaiveNASlib.defaultutility
— Functiondefaultutility(v::AbstractVertex)
Default function used to calculate utility of output neurons.
Implement either defaultutility(f)
or defaultutility(t, f)
where f
is the computation performed by CompVertex
and t
is trait(v)
to set the default for f
.
Examples
julia> using NaiveNASlib, Statistics
julia> struct Affine{T}
W::Matrix{T}
end;
julia> NaiveNASlib.defaultutility(l::Affine) = mean(abs, l.W; dims=2);
julia> NaiveNASlib.defaultutility(Affine(ones(2,3)))
2×1 Matrix{Float64}:
1.0
1.0