Module 7 · after
The same 6 questions you saw before the module. No running the code: predict, then compare with your first score.
x_grad = 0 for rate in [2, 2, 1]: x_grad = rate print(x_grad)
class Value: def __init__(self, data, children=(), local_grads=()): self.data = data self._local_grads = local_grads def __pow__(self, n): return Value(self.data ** n, (self,), (n * self.data ** (n - 1),)) y = Value(4.0) ** -1 print(y.data, y._local_grads)
class V: def __init__(self, data, kids=(), rates=()): self.data, self.grad, self.kids, self.rates = data, 0, kids, rates def __add__(self, o): return V(self.data + o.data, (self, o), (1, 1)) def __mul__(self, o): return V(self.data * o.data, (self, o), (o.data, self.data)) a, b = V(3.0), V(4.0) c = a * b L = c + a L.grad = 1 for v in [L, c]: # reverse topological order for kid, rate in zip(v.kids, v.rates): kid.grad += rate * v.grad print(a.grad, b.grad)
def z(x): y = 2 * x + 1 return y * y h = 1e-6 print(round((z(1 + h) - z(1 - h)) / (2 * h), 3))
Your answers are saved with a random id, not your name, so we can see which lessons work. Privacy