https://github.com/xrsrke/tinypytorch
A deep learning framework from scratch
https://github.com/xrsrke/tinypytorch
Last synced: 3 months ago
JSON representation
A deep learning framework from scratch
- Host: GitHub
- URL: https://github.com/xrsrke/tinypytorch
- Owner: xrsrke
- License: mit
- Created: 2022-08-25T01:03:45.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-13T02:05:04.000Z (over 2 years ago)
- Last Synced: 2025-01-21T15:26:28.050Z (5 months ago)
- Language: Jupyter Notebook
- Homepage:
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tinypytorch
================``` python
import torch
from tinypytorch.data import get_local_data
from tinypytorch.model import initialize_parameters, Model
from tinypytorch.metrics import accuracy
```Dependence libraries - nbdev - torch - matplotlib - pytest
This file will become your README and also the index of your
documentation.## Install
``` sh
pip install tinypytorch
```### Train a neural network
#### Data
``` python
x_train, y_train, x_valid, y_valid = get_local_data()
`````` python
x_train.shape, y_train.shape
```(torch.Size([50000, 784]), torch.Size([50000]))
``` python
y_train[1]
```tensor(0)
``` python
y_train[1:5]
```tensor([0, 4, 1, 9])
#### Initialize hyperparameters
``` python
n, m = x_train.shape # num rows and columns
`````` python
c = y_train.max() + 1
`````` python
n, m, c
```(50000, 784, tensor(10))
``` python
nh = 50 # num hidden
`````` python
w1, b1, w2, b2 = initialize_parameters(m, nh)
`````` python
w1.shape, b1.shape
```(torch.Size([784, 50]), torch.Size([50]))
``` python
w2.shape, b2.shape
```(torch.Size([50, 1]), torch.Size([1]))
- Training set’s shape: (50000, 784)
- Weight’s shape: (784, 50)
- Bias’s shape: (50)#### The first layer (Lin): (50000, 784) x (784, 50) + (50)
``` python
model = Model(w1, b1, w2, b2)
`````` python
loss = model(x_train, y_train)
```Model.__call__
l=
Lin.forward
inp=torch.Size([50000, 784])
w=torch.Size([784, 50])
b=torch.Size([50])
output.shape=torch.Size([50000, 50])
x.shape=torch.Size([50000, 50])
Model.__call__
l=
x.shape=torch.Size([50000, 50])
Model.__call__
l=
Lin.forward
inp=torch.Size([50000, 50])
w=torch.Size([50, 1])
b=torch.Size([1])
output.shape=torch.Size([50000, 1])
x.shape=torch.Size([50000, 1])``` python
loss
```tensor(26.1652)
``` python
model.backward()
```### Example 2
``` python
bs = 64
`````` python
xb = x_train[0:64]
```