API

Auto

ChainPlots.NODE_PROPERTIES — Constant
NODE_PROPERTIES

Names of the properties stored for each neuron (node) of a ChainGraph.

  • :layer_number: Int with the layer the neuron belongs to, with 0 for the input layer, 1, …, n-1 for the hidden layers and n for the output layer of a chain with n layers;
  • :layer_name: String with the name of the layer, e.g. "Dense(2 => 5, σ)", or "input layer";
  • :layer_type: Symbol with the type of the layer, e.g. :Dense, :LSTM, :GRUCell, :Conv, or :input_layer;
  • :index_in_layer: Tuple with the position of the neuron within its layer, e.g. (i,) for the output of Dense(n => m) or RNN(n => m), or (i, j, c) for a two-dimensional convolutional layer, and so on;
  • :layer_center: Float64 with the vertical mid-point of the layer;
  • :loc_x, :loc_y: Float64 with the coordinates of the neuron in the plot;
  • :neuron_color: Symbol with the default color of the neuron, see NEURON_COLORS.
source
ChainPlots.RECURRENT_CELLS — Constant
RECURRENT_CELLS

Recurrent cells that process a single time step, returning (y, state).

Since a cell returns a tuple, a Flux.Chain cannot continue after it, so a cell is only accepted on its own or as the last layer of a chain.

source
ChainPlots.SEQUENCE_LAYERS — Constant
SEQUENCE_LAYERS

Recurrent layers that process a whole sequence at once (Flux ≥ 0.15).

ChainPlots draws them folded, as a single time step: a vector input of length in is fed to them as a sequence of length one, and their output is taken as a vector of length out.

source
ChainPlots.ChainGraph — Type
ChainGraph <: Graphs.AbstractGraph{Int}

Graph of the neurons of a neural network, as built by chaingraph, with one vertex per neuron and one edge per connection between neurons of consecutive layers.

A ChainGraph implements the Graphs.jl interface, so functions such as nv, ne, vertices, edges and neighbors work on it, and so do packages built on that interface, such as GraphPlot.jl. It is plotted with plot(cg), once Plots.jl or Makie.jl is loaded; see plotchain for the attributes of the plot recipes.

Use nodeprop and nodeprops to read the properties of a neuron (see NODE_PROPERTIES), and layer_dimensions and layer_names for the properties of each layer.

The fields of a ChainGraph are internal: the graph backend may change in a future release without notice.

source
ChainPlots.ModelLayers — Type
ModelLayers

Internal description of a model as a sequence of layers, which is what chaingraph works with, so that it supports models from different packages.

For each layer, it holds a function applying the layer to an array of Float32, used to infer the dimensions of each layer, a function applying it to an array of NeuralNumbers, used to infer the connections between neurons, and its name and type, such as :Dense.

source
ChainPlots.UnitVector — Type
UnitVector{T}

Structure for unit vectors in a linear space

Used for generating a basis to infer the layer connection

source
ChainPlots.applylayer — Method
applylayer(l, x)

Apply layer l to x the way ChainPlots models it.

Sequence layers given a vector are run on a sequence of length one and their output is returned as a vector, and recurrent cells return only their output y, dropping the state.

source
ChainPlots.chaingraph — Function
chaingraph(m::Flux.Chain) -> ChainGraph

Return a ChainGraph with the graph structure of the neural network m.

In this case, the first layer must be a layer with fixed input dimension.

See chaingraph for the properties of each node of the graph.

source
ChainPlots.chaingraph — Function
chaingraph(l::Union{FIXED_INPUT_DIM_LAYERS...}) -> ChainGraph

Return a ChainGraph with the graph structure of a neural network composed of the single layer l.

This covers Flux.Dense, the sequence layers Flux.RNN, Flux.LSTM, Flux.GRU and Flux.GRUv3, and their cells.

source
ChainPlots.chaingraph — Method
chaingraph(l, input::Union{Array,Tuple}) -> ChainGraph

Return a ChainGraph with the graph structure of a neural network composed of the single layer l, such as a Flux.Conv, applied to input, or to an input of shape input.

source
ChainPlots.chaingraph — Method
chaingraph(ml::ModelLayers, input_data::Array) -> ChainGraph

Build the ChainGraph of the model described by ml, when applied to input_data.

This is the method all the others end up calling, after describing their model as a ModelLayers.

source
ChainPlots.chaingraph — Method
chaingraph(m::Flux.Chain, input_data::Array) -> ChainGraph

Return a ChainGraph with the graph structure of the neural network m when applied to input_data.

Each vertex is a neuron of the network, and each edge connects a neuron to a neuron of the next layer that it affects. The properties of each neuron, listed in NODE_PROPERTIES, are read with nodeprop and nodeprops.

The recurrent layers RNN, LSTM, GRU and GRUv3 are drawn folded, as a single time step, when the input is a vector. If the input is a sequence of size (in, len), the graph has a neuron for each time step instead, with each time step connected to the same and the later time steps of the next layer; the plot recipe does not lay the time steps out separately, though. The recurrent cells (RNNCell, …) return a tuple (y, state), so they can only be the last layer of a chain.

The plot recipe for ChainGraph draws it, as in plot(chaingraph(m, input_data)); see also plotchain.

source
ChainPlots.chaingraph — Method
chaingraph(m::Flux.Chain, ldim::Tuple) -> ChainGraph

Return a ChainGraph with the graph structure of the neural network m with an input of shape ldim.

See chaingraph for the properties of each node of the graph.

source
ChainPlots.check_chain — Method
check_chain(m::Flux.Chain)

Throw an ArgumentError if a recurrent cell appears before the last layer of m.

source
ChainPlots.get_dimensions — Method
get_dimensions(m, input = nothing)

Return the dimensions of the input and of the output data of each layer of the model m, given as a Flux.Chain or a ModelLayers.

If input is not given, the first layer is required to be a layer with fixed input dimensions, such as Flux.Dense or Flux.RNN, otherwise the given data, or the shape given as a Tuple, is used to infer the dimensions of each layer.

source
ChainPlots.input_array — Method
input_array(m, input::Union{Nothing,Array,Tuple})

Return an input for the model m: input itself if it is an Array, a random array of shape input if it is a Tuple, and, if it is nothing, a random input of the size expected by the first layer, when it has a fixed input size.

source
ChainPlots.layer_dimensions — Method
layer_dimensions(cg::ChainGraph) -> Vector{Tuple}

Return the dimensions of each layer of cg, starting with the input layer.

source
ChainPlots.layer_names — Method
layer_names(cg::ChainGraph) -> Vector{String}

Return the name of each layer of cg, starting with "input layer".

source
ChainPlots.neuron_color — Method
neuron_color(s; neuron_colors = NEURON_COLORS)

Grab the color for each specific type of neuron.

The color depends on the type of layer the neuron belongs to and the colorset given neuron_colors, which defaults to NEURON_COLORS.

source
ChainPlots.neuron_connections — Method
neuron_connections(m, input = nothing) -> Vector{Dict{Tuple, Vector{Tuple}}}

Return all the connections from every neuron in each layer to the corresponding neurons in the next layer of the model m, given as a Flux.Chain or a ModelLayers.

source
ChainPlots.nlayers — Method
nlayers(cg::ChainGraph)

Return the number of layers of the network, not counting the input layer.

source
ChainPlots.nodeprop — Method
nodeprop(cg::ChainGraph, v::Integer, prop::Symbol)

Return the property prop of the neuron v of cg.

See NODE_PROPERTIES for the available properties. To get one property of all the neurons, broadcast over the vertices, e.g. nodeprop.(Ref(cg), vertices(cg), :loc_x).

source
ChainPlots.plotbackend — Method
plotbackend() -> AbstractPlotBackend

Return the backend for the plotting package that is loaded, Plots.jl or Makie.jl.

Throw an ArgumentError if neither is loaded, or if both are, in which case pass the backend to plotchain explicitly.

source
ChainPlots.plotchain — Function
plotchain([backend,] m, input = nothing; kwargs...)

Plot the topology of the neural network m, which is plot(chaingraph(m, input); kwargs...).

It works with Plots.jl and with Makie.jl, whichever is loaded, as in using Plots or using CairoMakie. If both are loaded, choose one by passing ChainPlots.PlotsBackend() or ChainPlots.MakieBackend() as the first argument.

m can be a Flux.Chain, a Lux.Chain, or a single layer. If the first layer accepts an input with arbitrary dimensions, such as a convolutional or a functional layer, an input must be given, either as an Array or as a Tuple with the dimensions of the input.

Any other keyword argument is passed on to plot, such as title with Plots.jl, or axis = (; title = "...") with Makie.jl, as well as the attributes of the plot recipe for ChainGraph:

  • connection_color: color of the connections;
  • neuron_colors = NEURON_COLORS: either a Dict from layer types to colors, as in NEURON_COLORS, or a color for all neurons;
  • neuron_shape = :auto: :auto for a marker depending on the type of the layer, or a marker shape for all neurons.

With Plots.jl, it returns a Plots.Plot; with Makie.jl, a Makie.FigureAxisPlot.

source
ChainPlots.plotchain! — Function
plotchain!([backend,] [target,] m, input = nothing; kwargs...)

Add the topology of the neural network m to the current plot, or to target, which is a Plots.Plot with Plots.jl, or an axis, such as a Makie.Axis, with Makie.jl.

See plotchain.

source
ChainPlots.projection — Method
projection(z, center, max_widths, dimensions)

Transform the indexing of a neuron into its x and y coordinates for plotting.

source
ChainPlots.NeuralNumbers.NeuralNumber — Type
NeuralNumber <: Real

NeuralNumber encodes the "state" of a neuron as an Int8.

The possible states are: * state = Int8(0) for a "cold", or "off", state, meaning it can be triggered by a signal but it has not yet been triggered. It works as a neutral element in any diadic operation. * state = Int8(1) for a "hot", or "on", state, meaning it has been triggered by a signal.

The aliases are * cold = NeuralNumber(Int8(0)) * hot = NeuralNumber(Int8(1))

source