In a backward pass, no box ever looks at the whole graph. Each box receives one number from above — the gradient flowing back, — and multiplies it by its own local derivatives to hand one number back to each of its inputs. That is the chain rule, done one box at a time.
Task: write node_backward(op, inputs, upstream), which plays the part of a single box.
op names the box.inputs lists the numbers that flowed into the box during the forward pass.upstream is the gradient arriving at the box's output from above.Return a list holding one gradient for each input value, in the same order as inputs, each rounded to 4 decimal places.
op | inputs | what the box computes |
|---|---|---|
"add" | [x, y] | |
"mul" | [x, y] | |
"pow" | [x, k] | |
"exp" | [x] | , available as math.exp |
"tanh" | [x] | , available as math.tanh |
For "pow", the exponent k is a fixed number written into the box — it may be negative or a fraction — not a value flowing through the graph. It gets no gradient, so a "pow" box returns a one-element list. Whenever k is a fraction, x is positive.
is the one box this course has not differentiated, so take its local derivative as given: if , then