Computation Graphs
A computation graph is a sequence of computations drawn as a directed graph, so each intermediate value has a clear source and destination.
A Prediction Is a Chain of Small Steps
Most of the time, a model's prediction feels like a single action. You give it an input and it returns an answer. Underneath, though, the answer is produced by a chain of small operations, and each operation consumes the results of the one before it. The practical question this article asks is how to write that chain down so that every intermediate value has a place, a name, and a clear route to the final result.
The tool that answers this question is the computation graph. A computation graph is a sequence of computations drawn as a directed graph, so each intermediate value has a clear source and destination. Every step becomes a node, and every arrow points in the direction that data travels. Once a calculation is drawn this way, you can point at any value and see what produced it and what it will feed into next.
What do you think happens?
Before reading on: if the input is 0.5, the weight is 2, and the bias is -1, what is the net input (the weight times the input, plus the bias)?
Reveal answer
0
Multiply the input by the weight to get 1.0, then add the bias of -1 to get 0. The rest of this article uses these same three numbers, so you will be able to check each intermediate value as it appears.
Parts of a Logistic Regression Classifier
A logistic regression classifier is a single-layer neural network. Its forward pass multiplies the input by a weight, adds a bias, and applies a sigmoid to return a score between 0 and 1. The classifier has a small set of parts, and each one has a specific job in producing that score.
| Part | Where it comes from | Role in producing the score |
|---|---|---|
| Input feature | The data: the raw number describing the example | The starting value that the weight scales |
| Weight | A trainable parameter the model adjusts over time | Scales the input before the bias is applied |
| Bias | A trainable parameter the model adjusts over time | Shifts the result after scaling |
| Net input | Computed from the input, weight, and bias | The intermediate value: the weighted input plus the bias |
| Sigmoid | A function applied to the net input | Squashes the net input into an output between 0 and 1 |
| Label | The true class, either 0 or 1 | Not used to make the prediction; used only when measuring how wrong it was |
The parts of a logistic regression classifier and the role each one plays
The weight and the bias are the parameters the model adjusts during training, while the input comes from the data. The label is the fourth quantity, but it does not take part in the prediction. It enters only at the comparison that follows the prediction, and that comparison is what produces the loss, the quantity the learning process tries to reduce.
Tracing the Forward Pass Step by Step
The forward pass is the prediction step of the classifier. To trace it, read the computation from top to bottom and track dependencies rather than syntax. The net input cannot be computed until the feature, weight, and bias exist. The output cannot be computed until the net input exists. The loss cannot be computed until both the output and the label exist. Each step depends only on values defined before it.
One forward pass with input 0.5, weight 2, and bias -1
Trace the forward pass for a classifier with input x = 0.5, weight w = 2, and bias b = -1. Find the net input z and the output a.
- 1Start with the input and parameters
The input is 0.5, the weight is 2, and the bias is -1. These are the starting values, so they have no dependencies of their own.
- 2Compute the net input
Multiply the input by the weight to get 0.5 times 2, which is 1.0. Then add the bias: 1.0 plus -1 gives 0. The net input z is 0.
- 3Compute the output
The sigmoid takes the net input z = 0 and returns its output a. A sigmoid of 0 sits at the midpoint of its range, so a is 0.5. This output is bounded between 0 and 1, as the classifier requires.
- 4Hold the label until the comparison
If the true label is y = 1, it is not used in steps 2 or 3. It is only compared with a when the loss is computed.
The net input is 0, the output is 0.5, and the label enters only at the loss.
0.0 0.5- Each line depends only on names defined above it, which is the dependency pattern the graph makes visible
- The label is deliberately absent from the prediction lines
Sigmoid Squashing the Net Input
The sigmoid is the function that keeps the output between 0 and 1. It takes the net input, which can be any number, and maps it into a bounded range. This is what lets the classifier's output be read as a score and kept in a form that can be compared directly with a label that is either 0 or 1.
In the worked example, the net input is 0 and the sigmoid returns 0.5. In the graph, the sigmoid sits after the net input and before the loss. Its output is the last score value that the comparison with the label receives, so every upstream value exists to feed the score that the sigmoid produces.
Redrawing the Code as a Graph
To turn the computation into a graph, each input and parameter becomes a starting node. Each operation becomes a node that takes its inputs from earlier nodes. Arrows point from the values that are used toward the results that use them. The result is a directed graph in which the inputs flow toward the loss at the far end.
The graph makes three distinctions visible that the code only hints at. The input features and parameters sit at the top because they are the starting values. The intermediate results, such as the net input, sit in the middle because they exist only to pass information along. The output and the loss sit at the bottom because they are the end products. The label enters from the side, because it never feeds the prediction, only the comparison that follows it.
Reading the Chain of Intermediate Values
A graph is also useful as a pipeline. Each intermediate value appears once, has one source, and is passed to the next step. Following the arrows from left to right shows the order in which values must be ready, and it shows that the score is the end of a chain rather than a single lookup.
Graph Versus the Code That Builds It
The code and the graph describe the same computation, but they are not the same object. The code is a sequence of lines that create values and operations. The graph is the structure those lines produce: nodes for values and operations, and arrows for the dependencies between them. The structure is what represents the computation, because it shows which values must be ready before each step can run.
| Question | The code | The computation graph |
|---|---|---|
| What does it show? | The order of lines and the names assigned | Which values depend on which, and the direction data travels |
| How do you trace a value? | Scan upward for where a name was defined | Follow the arrow that points into the value |
| What is the role of the label? | A name that appears only in the loss line | A node entering from the side, feeding only the loss |
| Can it be read as a dependency rule? | Only with careful reading of each line | Yes, one-way arrows state the rule directly |
Comparing the code that builds a computation with the graph it represents
Common mistake Treating the graph as the code that builds it
What it looks likeLooking for the exact lines of Python inside the drawing and concluding that a missing line means a missing step.
Why it failsThe graph is the structure of the computation. Many different code listings could produce the same graph, so the drawing is not a transcription of syntax.
Do this insteadRead the graph for dependencies: which nodes feed which, and which values must exist before each step.
Common mistake Reading the arrows in both directions
What it looks likeThinking the loss feeds back into the input, because the loss is at the end of the picture.
Why it failsThe arrows are one-way dependencies. They show which values must be ready before each step can run, not a loop of influence.
Do this insteadFollow each arrow from the value that is used toward the result that uses it.
Common mistake Letting the label enter the prediction
What it looks likeAssuming the true class y is multiplied by the weight to produce the net input.
Why it failsThe label is not part of the prediction. It enters only at the loss, where the output is compared with it.
Do this insteadPlace the label on the side of the graph, connected to the loss node only.
Common mistake Confusing intermediate nodes with parameters, so the net input is treated as something the model learns directly
Checking Your Reading of the Graph
Keep the same weight and bias (weight 2, bias -1) but change the input to 1.5. Compute the net input z, then state whether the label y changes the value of z at any point.
Hints
- Multiply the new input by the weight first, then add the bias.
- Ask which arrows point into the net input node, and whether the label has an arrow into it.
What do you think happens?
In the graph, which node is the first place where the label y has any effect on the values computed so far?
Reveal answer
The loss
The label is not part of the prediction. It enters the graph only at the loss, where the output is compared with it.
Key Takeaways
- A computation graph is a sequence of computations drawn as a directed graph, so each intermediate value has a clear source and destination.
- A logistic regression classifier's forward pass multiplies the input by a weight, adds a bias to form the net input, and applies a sigmoid to return a score between 0 and 1.
- The true label is not part of the prediction; it enters the graph only at the loss, where the output is compared with it.
- Arrows in the graph are one-way dependencies that show which values must be ready before each step can run.
- The code that builds a graph is not the graph itself; the graph's nodes and arrows are what represent the computation's structure.