Skip to content

Commit 2d57b7e

Browse files
committed
modify readme
1 parent 38477b4 commit 2d57b7e

2 files changed

Lines changed: 139 additions & 27 deletions

File tree

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# PyDyNet:DL Framework (MLP, CNN, RNN, Transformer, ...) implementation using Numpy
1+
# PyDyNet:NumPy-based Dynamic Deep Learning Framework
22

3-
前作:[PyNet: Use NumPy to build neuron network](https://github.com/Kaslanarian/PyNet)。在那里我们基于求导规则实现了全连接网络。在这里,我们向当今的深度学习框架看齐,实现属于自己的DL框架。
4-
5-
**PyDyNet已被多个技术公众号和社区分享**[居然用Numpy实现了一个深度学习框架](https://segmentfault.com/a/1190000042108301).
3+
Chinese README: [cnREADME.md](./cnREADME.md)
64

75
[![Downloads](https://pepy.tech/badge/pydynet)](https://pepy.tech/project/pydynet)
86
[![Downloads](https://static.pepy.tech/personalized-badge/pydynet?period=month&units=international_system&left_color=grey&right_color=orange&left_text=downloads/month)](https://pepy.tech/project/pydynet)
@@ -13,20 +11,23 @@
1311

1412
## Towards Large Language Model
1513

16-
**2025.8.12**: 实现了纯推理的llama3 (6-layer Transformer, vocab-size=32000). 参考了[这里](https://github.com/likejazz/llama3.np)的NumPy实现和数据集. 将数据集下载到`llama`文件夹即可运行:
14+
**In the summer of 2025, I restart the development of PyDyNet after two years.** PyDyNet implemented a pure inference version of Llama3 (6-layer Transformer, vocab-size=32000). The implementation is inspired by the NumPy version and dataset available [here](https://github.com/likejazz/llama3.np). To run it, download the dataset into the `llama` folder and execute:
1715

1816
```bash
1917
>>> python -m llama.infer
18+
2019
There was a boy named Timmy. He loved to play with hi toy and run around outside. One day, Timmy' mom asked him to help her with the laundry. Timmy didn't want to help because he wanted to play. But hi mom said, "Timmy, you need to help me. It' important to help out."
2120
Timmy didn't want to help, but he knew he had to. So, he put on hi shoe and went outside to help hi mom. A they were folding the clothe, Timmy saw a big pile of laundry on the floor. He wanted to help, so he started to pick it up. But then, he accidentally knocked over a pile of clothe and they fell on him. Timmy wa okay, but he felt bad.
2221
Hi mom saw what happened and said, "Timmy, you need to be more careful. You could have hurt yourself." Timmy felt bad and said sorry. Hi mom hugged him and said, "It' okay, accident happen. Let' clean up the laundry together." Timmy learned that it' important to be careful and help out when you need it.
2322

2423
Token count: 262, elapsed: 0.87s, 300 tokens/s
2524
```
2625

26+
This implementation is not optimized for speed, but it serves as a reference for implementing large language models in PyDyNet. **I plan to implement some modern model such as CLIP in the future, and I welcome contributions from anyone interested in this project.**
27+
2728
## Overview
2829

29-
PyDyNet也是纯NumPy(0.0.7版本后加入CuPy,其用法和NumPy一致)实现的神经网络,语法受PyTorch的启发,大致结构如下:
30+
PyDyNet is a neural network framework implemented entirely in NumPy (with CuPy support since version 0.0.7, using the same API). Its syntax is inspired by PyTorch, and its structure is as follows:
3031

3132
```mermaid
3233
graph LR
@@ -42,7 +43,7 @@ graph LR
4243
A --> GD(Optimizer:<br> SGD, Adam, etc) ---> LS(lr_scheduler: <br>StepLR, etc)---> Mission
4344
```
4445

45-
虚线表示用户可以通过`no_grad`来关闭自动微分功能.
46+
Dashed lines indicate that users can disable automatic differentiation using `no_grad`.
4647

4748
## Install
4849

@@ -52,60 +53,62 @@ cd PyDyNet
5253
python setup.py install
5354
```
5455

56+
We are actively working on a pip installation option.
57+
5558
## Example
5659

57-
[examples/pydynet](./examples/pydynet)中是一些例子,[examples/pytorch](./examples/pytorch)给出等价的pytorch实现. 运行`python examples.pydynet.xxx`即可:
60+
Examples can be found in the [examples/pydynet](./examples/pydynet) directory, with equivalent PyTorch implementations in [examples/pytorch](./examples/pytorch). To run an example, use:
5861

59-
### AutoDiff
62+
```bash
63+
python examples.pydynet.xxx
64+
```
65+
66+
### Automatic Differentiation
6067

61-
[autodiff1d.py](examples/pydynet/autodiff1d.py)利用自动微分,对一个一维凸函数进行梯度下降:
68+
The example [autodiff1d.py](examples/pydynet/autodiff1d.py) demonstrates automatic differentiation by performing gradient descent on a one-dimensional convex function:
6269

6370
<img src="imgs/ad1d.png" alt="ad1" style="zoom:67%;" />
6471

65-
以及一个多元凸函数的例子: [autodiff2d.py](examples/pydynet/autodiff2d.py)
72+
A multi-variable convex function example is provided in [autodiff2d.py](examples/pydynet/autodiff2d.py):
6673

6774
<img src="imgs/ad2d.png" alt="ad2" style="zoom:67%;" />
6875

6976
### MLP & LeNet
7077

71-
[mlp_cnn.py](examples/pydynet/mnist.py)使用MLP和LeNet对MNIST进行分类. 训练准确率和测试准确率:
78+
The example [mlp_cnn.py](examples/pydynet/mnist.py) uses MLP and LeNet to classify MNIST digits. The training and testing accuracies are shown below:
7279

7380
<img src="imgs/mlp_cnn.png" alt="dnn" style="zoom:67%;" />
7481

75-
### Dropout & BN
82+
### Dropout & Batch Normalization
7683

77-
[mlp_dropout_bn.py](examples/pydynet/dropout_bn.py)使用三种网络对`fetch_olivetti_faces`人脸(64×64)数据集进行分类并进行性能对比:
84+
The example [mlp_dropout_bn.py](examples/pydynet/dropout_bn.py) compares the performance of three networks on the `fetch_olivetti_faces` dataset (64×64 pixel images):
7885

79-
1. 三层MLP;
80-
2. 三层MLP + Dropout;
81-
3. 三层MLP + BatchNormalization.
82-
83-
学习效果对比:
86+
1. Three-layer MLP;
87+
2. Three-layer MLP with Dropout;
88+
3. Three-layer MLP with Batch Normalization.
8489

8590
<img src="imgs/dropout_bn.png" alt="cnn" style="zoom:67%;" />
8691

87-
### RNN
92+
### Recurrent Neural Network (RNN)
8893

89-
[rnn_sin.py](examples/pydynet/ts_prediction.py)中是一个用GRU做时序预测例子:
94+
The example [ts_prediction.py](examples/pydynet/ts_prediction.py) demonstrates time series prediction using a GRU:
9095

9196
<img src="imgs/rnn.png" alt="RNN" style="zoom:67%;" />
9297

9398
### Transformer
9499

95-
[transformer.py](examples/pydynet/transformer.py)中是一个用Transformer训练文本分类模型的例子. 训练结果:
100+
The example [transformer.py](examples/pydynet/transformer.py) shows how to train a text classification model using a Transformer. The training results are as follows:
96101

97102
<img src="imgs/transformer.png" alt="transformer" style="zoom:67%;" />
98103

99-
> 数据集 (CoLA) 链接: <https://nyu-mll.github.io/CoLA/cola_public_1.1.zip>
104+
> Dataset (CoLA) link: <https://nyu-mll.github.io/CoLA/cola_public_1.1.zip>
100105
101-
## cuda加速
106+
## Cuda Acceleration
102107

103-
在训练batch size为256, 测试batch size为1024情况下,模型在CPU和GPU上的训练速度比较:
108+
PyDyNet supports CUDA acceleration through CuPy. To use it, simply install CuPy and use the same API as NumPy. We compare the performance of PyDyNet with CuPy and NumPy as follows on **Nvidia GeForce RTX 4090**:
104109

105110
| Network structure | Dataset | CPU time (s) per epoch | GPU time (s) per epoch |
106111
| :-----------------: | :---------------: | :--------------------: | :--------------------: |
107112
| 3-layer MLP | MNIST (80000×574) | 7.256±0.138 | 1.203±.0181 |
108113
| LeNet | MNIST (80000×574) | 239.664±2.108 | 2.841±0.026 |
109114
| 1-layer Transformer (dim=512, head=4) | CoLA (8551×45×64) | 17.503±0.251 | 1.075±0.002 |
110-
111-
设备: Nvidia GeForce RTX 4090.

cnREADME.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# PyDyNet:NumPy-based Dynamic Deep Learning Framework
2+
3+
**PyDyNet已被多个技术公众号和社区分享**[居然用Numpy实现了一个深度学习框架](https://segmentfault.com/a/1190000042108301).
4+
5+
[![Downloads](https://pepy.tech/badge/pydynet)](https://pepy.tech/project/pydynet)
6+
[![Downloads](https://static.pepy.tech/personalized-badge/pydynet?period=month&units=international_system&left_color=grey&right_color=orange&left_text=downloads/month)](https://pepy.tech/project/pydynet)
7+
![x](https://img.shields.io/pypi/l/pydynet)
8+
![x](https://img.shields.io/pypi/implementation/numpy)
9+
![x](https://img.shields.io/github/stars/Kaslanarian/PyDyNet?style=social)
10+
![x](https://img.shields.io/github/forks/Kaslanarian/PyDyNet?style=social)
11+
12+
## Towards Large Language Model
13+
14+
**2025.8.12**: 实现了纯推理的llama3 (6-layer Transformer, vocab-size=32000). 参考了[这里](https://github.com/likejazz/llama3.np)的NumPy实现和数据集. 将数据集下载到`llama`文件夹即可运行:
15+
16+
```bash
17+
>>> python -m llama.infer
18+
There was a boy named Timmy. He loved to play with hi toy and run around outside. One day, Timmy' mom asked him to help her with the laundry. Timmy didn't want to help because he wanted to play. But hi mom said, "Timmy, you need to help me. It' important to help out."
19+
Timmy didn't want to help, but he knew he had to. So, he put on hi shoe and went outside to help hi mom. A they were folding the clothe, Timmy saw a big pile of laundry on the floor. He wanted to help, so he started to pick it up. But then, he accidentally knocked over a pile of clothe and they fell on him. Timmy wa okay, but he felt bad.
20+
Hi mom saw what happened and said, "Timmy, you need to be more careful. You could have hurt yourself." Timmy felt bad and said sorry. Hi mom hugged him and said, "It' okay, accident happen. Let' clean up the laundry together." Timmy learned that it' important to be careful and help out when you need it.
21+
22+
Token count: 262, elapsed: 0.87s, 300 tokens/s
23+
```
24+
25+
## Overview
26+
27+
PyDyNet也是纯NumPy(0.0.7版本后加入CuPy,其用法和NumPy一致)实现的神经网络,语法受PyTorch的启发,大致结构如下:
28+
29+
```mermaid
30+
graph LR
31+
N(numpy/cupy.ndarray)--Backend--> A(Tensor) --> ds(Dataset) ---> Data(DataLoader)---> Mission
32+
A --Eager execution--> B(Basic operators:<br> add, exp, etc)
33+
B -.Autograd-.-> A
34+
35+
B --> CO(Complex<br>operators)
36+
--> f(Function:<br>img2col, etc)
37+
--> M(Basic Module:<br>Linear, etc)
38+
--> CM(Advanced Module: CNN, RNN, Transformer, etc)
39+
--> Mission(Learning task)
40+
A --> GD(Optimizer:<br> SGD, Adam, etc) ---> LS(lr_scheduler: <br>StepLR, etc)---> Mission
41+
```
42+
43+
虚线表示用户可以通过`no_grad`来关闭自动微分功能.
44+
45+
## Install
46+
47+
```bash
48+
git clone https://github.com/Kaslanarian/PyDyNet
49+
cd PyDyNet
50+
python setup.py install
51+
```
52+
53+
## Example
54+
55+
[examples/pydynet](./examples/pydynet)中是一些例子,[examples/pytorch](./examples/pytorch)给出等价的pytorch实现. 运行`python examples.pydynet.xxx`即可:
56+
57+
### AutoDiff
58+
59+
[autodiff1d.py](examples/pydynet/autodiff1d.py)利用自动微分,对一个一维凸函数进行梯度下降:
60+
61+
<img src="imgs/ad1d.png" alt="ad1" style="zoom:67%;" />
62+
63+
以及一个多元凸函数的例子: [autodiff2d.py](examples/pydynet/autodiff2d.py)
64+
65+
<img src="imgs/ad2d.png" alt="ad2" style="zoom:67%;" />
66+
67+
### MLP & LeNet
68+
69+
[mlp_cnn.py](examples/pydynet/mnist.py)使用MLP和LeNet对MNIST进行分类. 训练准确率和测试准确率:
70+
71+
<img src="imgs/mlp_cnn.png" alt="dnn" style="zoom:67%;" />
72+
73+
### Dropout & BN
74+
75+
[mlp_dropout_bn.py](examples/pydynet/dropout_bn.py)使用三种网络对`fetch_olivetti_faces`人脸(64×64)数据集进行分类并进行性能对比:
76+
77+
1. 三层MLP;
78+
2. 三层MLP + Dropout;
79+
3. 三层MLP + BatchNormalization.
80+
81+
学习效果对比:
82+
83+
<img src="imgs/dropout_bn.png" alt="cnn" style="zoom:67%;" />
84+
85+
### RNN
86+
87+
[ts_prediction](examples/pydynet/ts_prediction.py)中是一个用GRU做时序预测例子:
88+
89+
<img src="imgs/rnn.png" alt="RNN" style="zoom:67%;" />
90+
91+
### Transformer
92+
93+
[transformer.py](examples/pydynet/transformer.py)中是一个用Transformer训练文本分类模型的例子. 训练结果:
94+
95+
<img src="imgs/transformer.png" alt="transformer" style="zoom:67%;" />
96+
97+
> 数据集 (CoLA) 链接: <https://nyu-mll.github.io/CoLA/cola_public_1.1.zip>
98+
99+
## cuda加速
100+
101+
在训练batch size为256, 测试batch size为1024情况下,模型在CPU和GPU上的训练速度比较:
102+
103+
| Network structure | Dataset | CPU time (s) per epoch | GPU time (s) per epoch |
104+
| :-----------------: | :---------------: | :--------------------: | :--------------------: |
105+
| 3-layer MLP | MNIST (80000×574) | 7.256±0.138 | 1.203±.0181 |
106+
| LeNet | MNIST (80000×574) | 239.664±2.108 | 2.841±0.026 |
107+
| 1-layer Transformer (dim=512, head=4) | CoLA (8551×45×64) | 17.503±0.251 | 1.075±0.002 |
108+
109+
设备: Nvidia GeForce RTX 4090.

0 commit comments

Comments
 (0)