diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..9b388533 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/README.md b/README.md index 62e4d6ba..79d83efa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-2e0aaae1b6195c2367325f4f02e2d04e9abb55f0b24a779b69b11b9e10269abc.svg)](https://classroom.github.com/online_ide?assignment_repo_id=17425364&assignment_repo_type=AssignmentRepo) # MiniTorch Module 0 diff --git a/minitorch/datasets.py b/minitorch/datasets.py index b3bd9faa..699cad04 100644 --- a/minitorch/datasets.py +++ b/minitorch/datasets.py @@ -67,19 +67,29 @@ def circle(N): def spiral(N): - def x(t): return t * math.cos(t) / 20.0 def y(t): return t * math.sin(t) / 20.0 - X = [(x(10.0 * (float(i) / (N // 2))) + 0.5, y(10.0 * (float(i) / (N // - 2))) + 0.5) for i in range(5 + 0, 5 + N // 2)] - X = X + [(y(-10.0 * (float(i) / (N // 2))) + 0.5, x(-10.0 * (float(i) / - (N // 2))) + 0.5) for i in range(5 + 0, 5 + N // 2)] + + X = [ + (x(10.0 * (float(i) / (N // 2))) + 0.5, y(10.0 * (float(i) / (N // 2))) + 0.5) + for i in range(5 + 0, 5 + N // 2) + ] + X = X + [ + (y(-10.0 * (float(i) / (N // 2))) + 0.5, x(-10.0 * (float(i) / (N // 2))) + 0.5) + for i in range(5 + 0, 5 + N // 2) + ] y2 = [0] * (N // 2) + [1] * (N // 2) return Graph(N, X, y2) -datasets = {'Simple': simple, 'Diag': diag, 'Split': split, 'Xor': xor, - 'Circle': circle, 'Spiral': spiral} +datasets = { + "Simple": simple, + "Diag": diag, + "Split": split, + "Xor": xor, + "Circle": circle, + "Spiral": spiral, +} diff --git a/minitorch/operators.py b/minitorch/operators.py index 37cc7c09..750d843e 100644 --- a/minitorch/operators.py +++ b/minitorch/operators.py @@ -3,28 +3,98 @@ import math # ## Task 0.1 -from typing import Callable, Iterable # # Implementation of a prelude of elementary functions. + # Mathematical functions: # - mul +def mul(x: float, y: float) -> float: + return x * y + + # - id +def id(x: float) -> float: + return x + + # - add +def add(x: float, y: float) -> float: + return x + y + + # - neg +def neg(x: float) -> float: + return -x + + # - lt +def lt(x: float, y: float) -> bool: + return x < y + + # - eq +def eq(x: float, y: float) -> bool: + return x == y + + # - max +def max(x: float, y: float) -> float: + return y if lt(x, y) else x + + # - is_close +def is_close(x: float, y: float) -> bool: + return abs(x - y) <= 0.01 + + # - sigmoid +def sigmoid(x: float) -> float: + if x >= 0: + return 1.0 / (1.0 + math.e ** (-x)) + else: + return math.e**x / (1.0 + math.e**x) + + # - relu +def relu(x: float) -> float: + if x >= 0: + return x + else: + return 0 + + # - log +def log(x: float) -> float: + return math.log(x) + + # - exp +def exp(x: float) -> float: + return math.e**x + + # - log_back +def log_back(x: float, d: float) -> float: + return d * (1 / x) + + # - inv +def inv(x: float) -> float: + return 1 / x + + # - inv_back +def inv_back(x: float, d: float) -> float: + return -d / (x**2) + + # - relu_back +def relu_back(x: float, d: float) -> float: + return d if x > 0 else 0 + + # # For sigmoid calculate as: # $f(x) = \frac{1.0}{(1.0 + e^{-x})}$ if x >=0 else $\frac{e^x}{(1.0 + e^{x})}$ @@ -44,11 +114,27 @@ # - zipWith # - reduce # + + # Use these to implement # - negList : negate a list +def negList(a: list) -> list: + return a + + # - addLists : add two lists together +def addLists(a: list, b: list) -> list: + return a + b + + # - sum: sum lists +def sum(a: list) -> float: + return a[0] + + # - prod: take the product of lists +def prod(a: list) -> float: + return a[0] # TODO: Implement for Task 0.3. diff --git a/requirements.extra.txt b/requirements.extra.txt index 070fa1d0..81db5232 100644 --- a/requirements.extra.txt +++ b/requirements.extra.txt @@ -1,5 +1,7 @@ +altair==4.2.2 datasets==2.4.0 embeddings==0.0.8 +networkx==3.3 plotly==4.14.3 pydot==1.4.1 python-mnist @@ -7,5 +9,3 @@ streamlit==1.12.0 streamlit-ace torch watchdog==1.0.2 -altair==4.2.2 -networkx==3.3 diff --git a/tests/test_operators.py b/tests/test_operators.py index f6e555af..17d16ed7 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -22,7 +22,6 @@ prod, relu, relu_back, - sigmoid, ) from .strategies import assert_close, small_floats