From 72f04ff2a5b94c7e0793d2acf82e0bf5a7ceaa98 Mon Sep 17 00:00:00 2001 From: daniel <1534513+dantp-ai@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:27:31 +0100 Subject: [PATCH] Remove todos --- minitorch/operators.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/minitorch/operators.py b/minitorch/operators.py index 895408d..0032b8f 100644 --- a/minitorch/operators.py +++ b/minitorch/operators.py @@ -12,49 +12,48 @@ def mul(x: float, y: float) -> float: "$f(x, y) = x * y$" - # TODO: Implement for Task 0.1. return x * y def id(x: float) -> float: "$f(x) = x$" - # TODO: Implement for Task 0.1. + return x def add(x: float, y: float) -> float: "$f(x, y) = x + y$" - # TODO: Implement for Task 0.1. + return x + y def neg(x: float) -> float: "$f(x) = -x$" - # TODO: Implement for Task 0.1. + return -x def lt(x: float, y: float) -> float: "$f(x) =$ 1.0 if x is less than y else 0.0" - # TODO: Implement for Task 0.1. + return float(x < y) def eq(x: float, y: float) -> float: "$f(x) =$ 1.0 if x is equal to y else 0.0" - # TODO: Implement for Task 0.1. + return float(x == y) def max(x: float, y: float) -> float: "$f(x) =$ x if x is greater than y else y" - # TODO: Implement for Task 0.1. + return x if x >= y else y def is_close(x: float, y: float) -> float: "$f(x) = |x - y| < 1e-2$" - # TODO: Implement for Task 0.1. + if x >= y: return x - y <= 1e-2 else: @@ -73,7 +72,7 @@ def sigmoid(x: float) -> float: for stability. """ - # TODO: Implement for Task 0.1. + return (1.0 / (1.0 + math.exp(-x))) if x >= 0 else math.exp(x) / (1 + math.exp(x)) @@ -83,7 +82,7 @@ def relu(x: float) -> float: (See https://en.wikipedia.org/wiki/Rectifier_(neural_networks) .) """ - # TODO: Implement for Task 0.1. + return x if x > 0.0 else 0 @@ -102,26 +101,26 @@ def exp(x: float) -> float: def log_back(x: float, d: float) -> float: r"If $f = log$ as above, compute $d \times f'(x)$" - # TODO: Implement for Task 0.1. + return d / x def inv(x: float) -> float: "$f(x) = 1/x$" - # TODO: Implement for Task 0.1. + if x != 0.0: return 1 / x def inv_back(x: float, d: float) -> float: r"If $f(x) = 1/x$ compute $d \times f'(x)$" - # TODO: Implement for Task 0.1. + return -(x ** (-2)) * d def relu_back(x: float, d: float) -> float: r"If $f = relu$ compute $d \times f'(x)$" - # TODO: Implement for Task 0.1. + return d * (x > 0.0)