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
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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=15734874&assignment_repo_type=AssignmentRepo)
# MiniTorch Module 0

<img src="https://minitorch.github.io/minitorch.svg" width="50%">
Expand Down
24 changes: 17 additions & 7 deletions minitorch/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
146 changes: 125 additions & 21 deletions minitorch/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
import math

# ## Task 0.1
from typing import Callable, Iterable

#
# Implementation of a prelude of elementary functions.

# Mathematical functions:
# - mul
# - id
# - add
# - neg
# - lt
# - eq
# - max
# - is_close
# - sigmoid
# - relu
# - log
# - exp
# - log_back
# - inv
# - inv_back
# - relu_back

# mul - Multiplies two numbers
# id - Returns the input unchanged
# add - Adds two numbers
# neg - Negates a number
# lt - Checks if one number is less than another
# eq - Checks if two numbers are equal
# max - Returns the larger of two numbers
# is_close - Checks if two numbers are close in value
# sigmoid - Calculates the sigmoid function
# relu - Applies the ReLU activation function
# log - Calculates the natural logarithm
# exp - Calculates the exponential function
# inv - Calculates the reciprocal
# log_back - Computes the derivative of log times a second arg
# inv_back - Computes the derivative of reciprocal times a second arg
# relu_back - Computes the derivative of ReLU times a second arg
#
# For sigmoid calculate as:
# $f(x) = \frac{1.0}{(1.0 + e^{-x})}$ if x >=0 else $\frac{e^x}{(1.0 + e^{x})}$
Expand All @@ -35,15 +35,86 @@
# TODO: Implement for Task 0.1.


def mul(a, b):
return a * b


def id(a):
return a


def add(a, b):
return a + b


def neg(a):
return -1 * a


def lt(a, b):
return a < b


def eq(a, b):
return a == b


def max(a, b):
return a if (a > b) else b


def is_close(a, b):
return abs(a - b) < 1e-2


def sigmoid(a):
if a >= 0:
return 1 / (1 + math.exp(-a))
else:
return math.exp(a) / (1 + math.exp(a))


def relu(a):
return max(0, a)


def log(a):
return math.log(a)


def exp(a):
return math.exp(a)


def inv(a):
return 1 / a


def log_back(a, b):
return inv(a) * b


def inv_back(a, b):
return (-1) / (a**2) * b


def relu_back(a, b):
if a <= 0:
return 0
else:
return b


# ## Task 0.3

# Small practice library of elementary higher-order functions.

# Implement the following core functions
# - map
# - zipWith
# - reduce
#

# map - Higher-order function that applies a given function to each element of an iterable
# zipWith - Higher-order function that combines elements from two iterables using a given function
# reduce - Higher-order function that reduces an iterable to a single value using a given function

# Use these to implement
# - negList : negate a list
# - addLists : add two lists together
Expand All @@ -52,3 +123,36 @@


# TODO: Implement for Task 0.3.


def map(fn, iter):
return [fn(item) for item in iter]


def zipWith(fn, iter1, iter2):
return [fn(x, y) for x, y in zip(iter1, iter2)]


def reduce(fn, iter):
if len(iter) == 0:
return 0
result = iter[0]
for item in iter[1:]:
result = fn(result, item)
return result


def negList(ls):
return map(neg, ls)


def addLists(ls1, ls2):
return zipWith(add, ls1, ls2)


def sum(ls):
return reduce(add, ls)


def prod(ls):
return reduce(mul, ls)
4 changes: 2 additions & 2 deletions requirements.extra.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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
streamlit==1.12.0
streamlit-ace
torch
watchdog==1.0.2
altair==4.2.2
networkx==3.3
Loading