Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .github/.keep
Empty file.
67 changes: 67 additions & 0 deletions .github/workflows/classroom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Autograding Tests
'on':
- workflow_dispatch
- repository_dispatch
permissions:
checks: write
actions: read
contents: read
jobs:
run-autograding-tests:
runs-on: ubuntu-latest
if: github.actor != 'github-classroom[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup
id: setup
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Setup
setup-command: sudo -H pip3 install -qr requirements.txt; sudo -H pip3 install
flake8==5.0.4 mypy
command: flake8 --ignore "N801, E203, E266, E501, W503, F812, E741, N803,
N802, N806" minitorch/ tests/ project/; mypy minitorch/
timeout: 10
- name: Task 0.1
id: task-0-1
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Task 0.1
setup-command: sudo -H pip3 install -qr requirements.txt
command: pytest -m task0_1
timeout: 10
- name: Task 0.2
id: task-0-2
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Task 0.2
setup-command: sudo -H pip3 install -qr requirements.txt
command: pytest -m task0_2
timeout: 10
- name: Task 0.3
id: task-0-3
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Task 0.3
setup-command: sudo -H pip3 install -qr requirements.txt
command: pytest -m task0_3
timeout: 10
- name: Task 0.4
id: task-0-4
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Task 0.4
setup-command: sudo -H pip3 install -qr requirements.txt
command: pytest -m task0_4
timeout: 10
- name: Autograding Reporter
uses: classroom-resources/autograding-grading-reporter@v1
env:
SETUP_RESULTS: "${{steps.setup.outputs.result}}"
TASK-0-1_RESULTS: "${{steps.task-0-1.outputs.result}}"
TASK-0-2_RESULTS: "${{steps.task-0-2.outputs.result}}"
TASK-0-3_RESULTS: "${{steps.task-0-3.outputs.result}}"
TASK-0-4_RESULTS: "${{steps.task-0-4.outputs.result}}"
with:
runners: setup,task-0-1,task-0-2,task-0-3,task-0-4
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ dmypy.json
.pyre/
*.\#*
data/
pyodide
pyodide

# VS code configure
.vscode/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-2e0aaae1b6195c2367325f4f02e2d04e9abb55f0b24a779b69b11b9e10269abc.svg)](https://classroom.github.com/online_ide?assignment_repo_id=23504068&assignment_repo_type=AssignmentRepo)
# MiniTorch Module 0

<img src="https://minitorch.github.io/minitorch.svg" width="50%">
![Result](images/result.png)

* Docs: https://minitorch.github.io/

Expand Down
Binary file added images/result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 33 additions & 15 deletions minitorch/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from typing import List, Tuple


def make_pts(N):
def make_pts(N: int) -> list:
"""Generate N random points."""
X = []
for i in range(N):
x_1 = random.random()
Expand All @@ -20,7 +21,8 @@ class Graph:
y: List[int]


def simple(N):
def simple(N: int) -> Graph:
"""Generate a simple dataset."""
X = make_pts(N)
y = []
for x_1, x_2 in X:
Expand All @@ -29,7 +31,8 @@ def simple(N):
return Graph(N, X, y)


def diag(N):
def diag(N: int) -> Graph:
"""Generate a diagonal dataset."""
X = make_pts(N)
y = []
for x_1, x_2 in X:
Expand All @@ -38,7 +41,8 @@ def diag(N):
return Graph(N, X, y)


def split(N):
def split(N: int) -> Graph:
"""Generate a split dataset."""
X = make_pts(N)
y = []
for x_1, x_2 in X:
Expand All @@ -47,7 +51,8 @@ def split(N):
return Graph(N, X, y)


def xor(N):
def xor(N: int) -> Graph:
"""Generate an XOR dataset."""
X = make_pts(N)
y = []
for x_1, x_2 in X:
Expand All @@ -56,7 +61,8 @@ def xor(N):
return Graph(N, X, y)


def circle(N):
def circle(N: int) -> Graph:
"""Generate a circular dataset."""
X = make_pts(N)
y = []
for x_1, x_2 in X:
Expand All @@ -66,20 +72,32 @@ def circle(N):
return Graph(N, X, y)


def spiral(N):
def spiral(N: int) -> Graph:
"""Generate a spiral dataset."""

def x(t):
def x(t: float) -> float:
return t * math.cos(t) / 20.0

def y(t):
def y(t: float) -> float:
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,
}
25 changes: 17 additions & 8 deletions minitorch/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def modules(self) -> Sequence[Module]:

def train(self) -> None:
"""Set the mode of this module and all descendent modules to `train`."""
# TODO: Implement for Task 0.4.
raise NotImplementedError("Need to implement for Task 0.4")
self.training = True
for m in self._modules.values():
m.train()

def eval(self) -> None:
"""Set the mode of this module and all descendent modules to `eval`."""
# TODO: Implement for Task 0.4.
raise NotImplementedError("Need to implement for Task 0.4")
self.training = False
for m in self._modules.values():
m.eval()

def named_parameters(self) -> Sequence[Tuple[str, Parameter]]:
"""Collect all the parameters of this module and its descendents.
Expand All @@ -47,13 +49,19 @@ def named_parameters(self) -> Sequence[Tuple[str, Parameter]]:
The name and `Parameter` of each ancestor parameter.

"""
# TODO: Implement for Task 0.4.
raise NotImplementedError("Need to implement for Task 0.4")
result = []
for k, v in self._parameters.items():
result.append((k, v))

for mod_name, mod in self._modules.items():
for sub_name, param in mod.named_parameters():
result.append((f"{mod_name}.{sub_name}", param))

return result

def parameters(self) -> Sequence[Parameter]:
"""Enumerate over all the parameters of this module and its descendents."""
# TODO: Implement for Task 0.4.
raise NotImplementedError("Need to implement for Task 0.4")
return [p for _, p in self.named_parameters()]

def add_parameter(self, k: str, v: Any) -> Parameter:
"""Manually add a parameter. Useful helper for scalar parameters.
Expand Down Expand Up @@ -89,6 +97,7 @@ def __getattr__(self, key: str) -> Any:
return None

def __call__(self, *args: Any, **kwargs: Any) -> Any:
"""Call the forward method."""
return self.forward(*args, **kwargs)

def __repr__(self) -> str:
Expand Down
Loading