Skip to content

Commit

Permalink
Merge pull request #18 from shenxiangzhuang/doc/improve
Browse files Browse the repository at this point in the history
Docs:improve
  • Loading branch information
shenxiangzhuang authored Sep 12, 2023
2 parents 6dbd72f + 38db7f6 commit 1a3a880
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.9"]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- name: Checkout
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
if: success() && matrix.os == 'ubuntu-latest' && matrix.python-version == '3.9'
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.8' && success()
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
Expand Down
8 changes: 7 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
There are some deep learning algorithms implemented from scratch!
Let's learn deep learning with toy code.

The implementation of toydl is highly inspired by [minitorch](https://github.com/minitorch/minitorch).


## Overview

- [ ] Mini-Torch
ToyDl, as an education project, is aimed to make the
concepts behind the deep learning as clear as possible.
I do believe the key to reach the target is **SIMPLE**, as simple as possible.
I'm trying to use little code to build this library, although it's not simple enough yet.
66 changes: 66 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
# Quick Start



## Binary Classification with MLP

??? MLPBinaryClassifyModel

```python
from toydl.core.optim import SGD
from toydl.core.scalar import Scalar
from toydl.network.mlp import MLPBinaryClassify, MLPConfig


class MLPBinaryClassifyModel:
def __init__(self, mlp_config: MLPConfig):
self.model = MLPBinaryClassify(mlp_config)

def train(self, data, learning_rate, max_epochs=500):
optim = SGD(self.model.parameters(), learning_rate)

losses = []
for epoch in range(1, max_epochs + 1):
total_loss = 0.0
correct = 0
optim.zero_grad()

# Forward
for i in range(data.n):
x_1, x_2 = data.X[i]
y = data.y[i]
x_1 = Scalar(x_1)
x_2 = Scalar(x_2)
out = self.model.forward((x_1, x_2))
if y == 1:
prob = out
correct += 1 if out.data > 0.5 else 0
else:
prob = -out + 1.0
correct += 1 if out.data < 0.5 else 0
loss = -prob.log()
(loss / data.n).backward()
total_loss += loss.data

losses.append(total_loss)

# Update
optim.step()

# Logging
if epoch % 10 == 0 or epoch == max_epochs:
print("Epoch ", epoch, " loss ", total_loss, "correct", correct)


if __name__ == "__main__":
from toydl.dataset.simulation import datasets

PTS = 50
RATE = 0.05
data = datasets["Simple"](PTS)

config = MLPConfig(in_size=2, out_size=1, hidden_layer_size=5, hidden_layer_num=2)
mlp_model = MLPBinaryClassifyModel(config)
# print(mlp_model.model.order_layer_names)
mlp_model.train(data, RATE, max_epochs=500)

```
1 change: 0 additions & 1 deletion docs/user-guide/1-read-first.md

This file was deleted.

10 changes: 6 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ nav:
- Home: index.md
- Getting started:
- Quick Start: quickstart.md
- User Guide:
- Read First: user-guide/1-read-first.md
- API References:
- Operator: api/operator.md
- Context: api/context.md
Expand All @@ -29,13 +27,17 @@ theme:
palette:
- media: "(prefers-color-scheme: light)"
scheme: default
primary: teal
accent: deep purple
toggle:
icon: material/lightbulb-outline
icon: material/weather-sunny
name: Switch to dark mode
- media: "(prefers-color-scheme: dark)"
scheme: slate
primary: cyan
accent: deep purple
toggle:
icon: material/lightbulb
icon: material/weather-night
name: Switch to light mode
features:
# - announce.dismiss
Expand Down

0 comments on commit 1a3a880

Please sign in to comment.