...

/

Computation Graphs

Computation Graphs

Learn about computation graphs: what they are and how we code them using PyTorch?

That automatic gradient calculation in the last section seems magical, but of course, it isn’t.

It is worth understanding just a little bit about how the automatic gradient calculation is done because this knowledge will help us later when we build larger networks.

Simple computation graph

Take a look at the following very simple network. It’s not a neural network, just a sequence of calculations.

In this illustration, we can see an input xx, which is used to calculate yy, which is then used to calculate zz, the output.

Imagine that yy and zz are calculated as follows:

y=x2 y = x^2

z=2y+3z = 2y + 3

If we want to know how the output zz varies as xx varies, we need to do some calculus to work out the gradient dydx\frac{dy}{dx}. Let’s do this step by step.

dzdx=dzdy.dydx\frac{dz}{dx} = \frac{dz}{dy}.\frac{dy}{dx}

dzdx=2.2x\frac{dz}{dx} = 2.2x

dzdx=4x\frac{dz}{dx} = 4x

📝 The first line is the chain rule of calculus, and it really helps us get started here. One of the appendices of Make Your Own Neural Network is an introduction to calculus and the chain rule if you need a refresher.

So we’ve just worked out that the output zz varies with xx as 4x4x. If x=3.5x = 3.5 ...