From 42174698fa4097a3fe57ed6e7e542a260320f12e Mon Sep 17 00:00:00 2001 From: Maxxhim <maxc2507@gmail.com> Date: Thu, 2 Apr 2020 23:56:23 +0200 Subject: [PATCH] add Substraction --- functions.py | 10 +++++----- variable.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/functions.py b/functions.py index fefbe9e..e50c421 100644 --- a/functions.py +++ b/functions.py @@ -52,21 +52,21 @@ class Sub(_Function): def __init__(self, x, y): super().__init__("Sub", x, y) ####################################################################### - # TODO: Implement the forward pass and put the result in self.result. + # Implement the forward pass and put the result in self.result. # The notbook provide you the formulas for this operation. ####################################################################### - self.result = None + self.result = x.data - y.data ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### def _backward(self, grad): ####################################################################### - # TODO: Implement the derivative dx for this opetation and add the + # Implement the derivative dx for this opetation and add the # result of the chain rule on self.dx. ####################################################################### - self.dx = None - self.dy = None + self.dx = grad + self.dy = -grad ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### diff --git a/variable.py b/variable.py index 1e49396..e7426a4 100644 --- a/variable.py +++ b/variable.py @@ -169,9 +169,9 @@ class Variable: children = [] if not len(children): ####################################################################### - # TODO: Call the backward of the operation that has build this Variable + # Call the backward of the operation that has build this Variable ####################################################################### - pass + self.grad_fn.backward(self.grad, retain_graph) ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### -- GitLab