Networks as Graphs
Draw every neuron as a node and every weight as an arrow, and a neural network becomes a weighted directed graph with no cycles. That's why a forward pass works at all: there's always an order, a topological order, in which every node's inputs are ready before the node itself.
What the symbols mean
- Adjacency entry
Row i lists where node i sends its output; column j lists where node j's inputs come from.
What the symbols mean
- k-th power
Adds up, over every path of exactly k edges from i to j, the product of the weights along it.
Explanation
A network as a graph is simply the picture you've been drawing all along, taken seriously: each neuron is a node, each weight is an arrow (a directed edge) from the neuron that sends a number to the one that receives it, and the weight's value is written on the arrow. Once it's a graph, graph theory hands us tools for free. The words to know are few: a node's fan-in is how many arrows come in (how many numbers it adds up), its fan-out is how many arrows go out (how many neurons use its output), and acyclic means no path of arrows ever loops back to where it started. It sounds abstract, but it's just a precise way of saying which numbers have to be ready before which; bear with it.
Why no cycles? A forward pass computes each node from the nodes pointing into it. If there were a cycle, say , then would need , which needs : there'd be no place to start. So a feedforward network is a DAG, a directed acyclic graph. (Recurrent networks do have loops, but they're run by unrolling the loop over time into one long DAG, a copy of the network per time step.)
Layers, fan-in and fan-out. In a dense layer every node of layer points to every node of layer , so each node's fan-in is and the edges between the two layers number , which is the weight count from Layers as Matrix Multiplication. A skip connection, an arrow that jumps over a layer, is fine too: it still points forward. Fan-in matters for the numbers as well. A neuron adding up random-sized terms gets a sum about times bigger than one with a single input, which is why good weight initialization scales by the fan-in; that comes back in Vanishing and Exploding Gradients.
The adjacency matrix. Number the nodes to and let be the weight on the edge from to ( if there isn't one). Row holds node 's outgoing weights, so its nonzero entries count the fan-out; column holds node 's incoming weights, so its nonzero entries count the fan-in. For a layered network is made of blocks, and the block from layer to layer is : transposed, because was indexed (to, from) while is indexed (from, to). If you number the nodes in a topological order, every edge goes from a smaller number to a bigger one, so is strictly upper triangular.
Powers of follow paths. Look at one entry of :
Each term is a two-step path , with the two weights multiplied; terms with a missing edge are . The same goes for , and so on. With every weight equal to , simply counts the paths of edges. With real weights and the activations removed, the total effect of input on output is the sum over all paths of the product of the weights along each path, which is . Keep that phrase in mind, "multiply along a path, add over paths": it's the chain rule, and it's exactly how backpropagation works. In a DAG with nodes no path can have more than edges, so (the matrix is nilpotent) and the sum stops on its own.
Topological order is the forward-pass order. A topological order lists the nodes so that every edge points from earlier to later. Every DAG has one. In the language of partial orders, the edges define "must come before", and a topological order lines the nodes up in a single row that respects it. Kahn's algorithm finds one: repeatedly pick a node with no incoming edges left, write it down, and delete its outgoing edges. If you ever get stuck with nodes remaining, every one of them is waiting on another, which means there's a cycle. For a layered network, layer by layer is a topological order. But nodes in the same layer can go in any order, so it's rarely unique, and those interchangeable nodes are exactly the ones a graphics chip computes in parallel. There's an animation further down that runs a small network in this order.
Not a tree. A tree is a connected graph with no cycles even when you ignore the arrow directions. A dense network has many routes between the same two nodes ( and ), and those form a cycle once directions are ignored, so it isn't a tree. In fact, by the product rule, a dense network has million input-to-output paths.
A common mistake. It's tempting to think a skip connection creates a cycle, because it "goes around" a layer. It doesn't. A cycle needs a path that follows the arrows back to its own start, and a skip connection still points forward. The undirected loop it makes is no problem for a forward pass. The other common slip is thinking the topological order is unique: any order that respects the arrows works.
See it

Worked examples
The network in the animation has nodes and edges , , , , , and the skip edge . Use Kahn's algorithm to find a topological order, and count how many orders there are.
- 1
Write down each node's fan-in (how many arrows come in): : , : , : , : , : (from , and ).
Give the same network the weights from the animation: : , : , : , : , : , : , : . Ignoring the activations, use the adjacency matrix to find how much changes per unit of .
- 1
Number the nodes in the topological order we found: , , , , . Row lists node 's outgoing weights:
Notice everything is above the diagonal, because the numbering is a topological order.
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many edges (weights, not counting biases) does a fully connected network have?
A network's adjacency matrix is (with for an edge ). Give the fan-in of node and the fan-out of node .
A network has nodes and edges , , , , , and a skip edge . Which is a valid forward-pass order?
How many different topological orders does a fully connected network have?
A network has the weighted edges : , : , : , : , : , : , and a skip edge : . Ignore the activations. By how much does change per unit of ?
A network has edges , , , , , and a skip edge . Adding which one edge would stop it from being a DAG?
A speech model has input features, two hidden layers of , and outputs, all fully connected layer to layer. It also has skip connections from every node of the first hidden layer straight to every output. How many weighted edges does it have?
How many different input-to-output paths does a fully connected network have?
The hardest one here. Get it, and you've got this lesson.
A computation graph has the adjacency matrix . Find the number of paths from node to node with exactly edges, and the total number of paths from node to node of any length.
Where it's used
Every deep learning framework stores a model as exactly this kind of graph. Before running it, the software sorts the nodes topologically, finds the ones that don't depend on each other and runs them at the same time, and checks for cycles that would make the model impossible to evaluate. Compilers for neural network chips go further, fusing chains of nodes into one operation and planning when each intermediate result can be thrown away, which all comes down to reasoning about paths in a DAG.