# Built-in way to draw LinearLayer graph

Newer versions of Mathematica come with built-in tooling for training and simulating neural networks.

For a simple LinearLayer, e.g. LinearLayer[2, "Weights" -> {{0, 0}, {0, 0}}, "Biases" -> {0, 0}] I want to produce a graph showing:

• The input and output nodes in the network
• The biases
• The connections, and respective weights

For example, something like the diagram below (though obviously without the hidden layer for a simple LinearLayer)

(source: lol768.com)

Is this possible?

MATLAB has the view function, but it's fairly limited too.

• Where the weights and biases are represented in the showed diagram? Commented May 25, 2019 at 16:50
• Related discussion: "Neural network illustrations". Commented May 25, 2019 at 17:05
• "Where the weights and biases are represented in the showed diagram?" - they're not, but I normally draw the weights as edge labels, and the biases/thresholds inside the nodes. Commented May 26, 2019 at 11:29

Using CompleteGraph with a list of layer sizes as the first argument and deleting undesired edges:

ClearAll[nwG]
nwG[layers : {__}, opts : OptionsPattern[Graph]] :=  Module[{nf = First@layers,
nl = Last@layers, cg = CompleteGraph[Flatten[{First@layers, layers, Last@layers}],
DirectedEdges -> True]},
cg = EdgeDelete[cg, {DirectedEdge[a_, b_] /;
(Subtract @@ (PropertyValue[{cg, #}, VertexCoordinates][[1]] & /@ {b, a}) > 1),
DirectedEdge[v1_, v2_] /; Or[And[v1 <= nf, v2 != nf + v1],
And[v2 >= 1 + VertexCount[cg] - nl, v1 != v2 - nl]]}];
SetProperty[cg, {PerformanceGoal -> "Quality",
VertexShapeFunction ->
{Alternatives @@ Join[Take[VertexList[cg], nf], Take[VertexList[cg], -nl]] :> None},
VertexSize -> .5, VertexStyle -> White,
EdgeStyle -> Black, EdgeLabelStyle -> 16,
VertexCoordinates -> (ReflectionTransform[{0, -1}]@ GraphEmbedding[cg]),
EdgeLabels -> {DirectedEdge[v : Alternatives @@ Range[nf], _] :>
Placed[Subscript["I", v], 1/2],
## & @@ MapIndexed[DirectedEdge[_, #] -> Placed[Subscript["O", #2[[1]]], 1/2] &,
Take[VertexList[cg], -nl]]}, opts}]]


Examples:

g0 = nwG[{5, 3, 1}, ImageSize -> Large]


SetProperty[g0, {VertexSize -> .9, VertexStyle -> {11 -> Yellow},
VertexCoordinates -> (ScalingTransform[{2, 1}]@GraphEmbedding[g0])}]


layers = {5, 2, 4, 3, 2, 1, 2, 3, 4};
nwG[layers, VertexStyle -> {23 -> Red}, ImageSize -> Large, ImagePadding -> Scaled[.03],
Epilog -> {Text["First\nLayer", {1, 4}], Text["Third\nLayer", {3, 4}],
Text["Seventh\nLayer", {7, 4}]}]


• Please add this answer to the related discussion: "Neural network illustrations". Commented May 25, 2019 at 18:01
• @AntonAntonov, posted a modified version of nwG in the linked q/a.
– kglr
Commented May 25, 2019 at 21:25
• So I'm guessing in terms of a "built-in", there isn't anything available? What would it take to get the connection weights added as edge labels, and the biases in the nodes with your solution? Could it take a LinearLayer/NetChain as an argument? Commented May 26, 2019 at 11:34
• @AdamWilliams I am working on a repository function for all of this for my current research (will present at WTC, if interested) it is a combination of weight/bias visualizations ala a custom style (I will add options for these labels it seems), a layer/connection organizer for adjacency graphs (the input to the research-based function), and some automated network constructions. The neuron behavior is outside of the normal neural networks functionality (time-based behavior of activations aka spiking networks) so everything is being custom made from the ground up. I’ll be sure to tag you! Commented Jul 27, 2019 at 15:58
• This sounds awesome @CATrevillian - keep me updated for sure :) Commented Aug 1, 2019 at 20:12