NanoGPT Tutorial:修订间差异

来自WHY42
第31行: 第31行:
</syntaxhighlight>
</syntaxhighlight>


== 下载和运行nanoGPT ==
== 下载nanoGPT ==
nanoGPT依赖于一些Python的软件包,在运行之前应该首先进行安装<ref>https://github.com/karpathy/nanoGPT</ref>。
nanoGPT依赖于一些Python的软件包,在运行之前应该首先进行安装<ref>https://github.com/karpathy/nanoGPT</ref>。
这里直接通过pip进行安装即可,
这里直接通过pip进行安装即可,
第45行: 第45行:
$ git clone https://github.com/drriguz/nanoGPT.git
$ git clone https://github.com/drriguz/nanoGPT.git
</syntaxhighlight>
</syntaxhighlight>
== 运行“莎士比亚”例子==


为了测试nanoGPT是否能正确工作,我们可以运行它自带的莎士比亚的例子。
为了测试nanoGPT是否能正确工作,我们可以运行它自带的莎士比亚的例子。

2023年12月11日 (一) 14:28的版本

环境准备

本文所有操作均在MacBook Air(macOS 13.5.1,2020版,M1芯片)和OptiPlex 7080(Linux Mint 21.2)上测试验证。 您也可以在其他的系统上运行,只需要在安装conda时按照官方文档稍作改动即可。

安装Miniconda 和Python

在MacOS下,可以通过以下脚本安装[1]

$ mkdir -p ~/miniconda3
$ curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
$ bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
$ rm -rf ~/miniconda3/miniconda.sh

安装完成后,可以使用conda命令来管理机器学习的Python环境了。默认系统会自动创建一个Python3.11的环境:

$ python --version
Python 3.11.5
$ whereis python
python: /Users/riguz/miniconda3/bin/python

安装PyTorch

由于nanoGPT是机遇PyTorch的,因此需要安装它。在Mac上运行时,可以采用nightly版本, 因为更新的版本会集成Mac自带GPU的支持,“可能”性能上可以得到提升。

$ conda install pytorch-nightly::pytorch torchvision torchaudio -c pytorch-nightly

下载nanoGPT

nanoGPT依赖于一些Python的软件包,在运行之前应该首先进行安装[2]。 这里直接通过pip进行安装即可, 为加快下载速度,可以选择使用国内的pip源,如清华镜像(-i https://pypi.tuna.tsinghua.edu.cn/simple)。

$ pip install torch numpy transformers datasets tiktoken wandb tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple

接下来将nanoGPT克隆到本地:

$ git clone https://github.com/drriguz/nanoGPT.git

运行“莎士比亚”例子

为了测试nanoGPT是否能正确工作,我们可以运行它自带的莎士比亚的例子。 该例子通过一个两三万行的莎士比亚剧本作为训练语料,大概长这样:

First Citizen:
Before we proceed any further, hear me speak.

All:
Speak, speak.

First Citizen:
You are all resolved rather to die than to famish?

All:
Resolved. resolved.
...

运行这个例子分为三步:

  1. 准备数据,通过data/shakespeare_char/prepare.py脚本将语料加载到本地,并将数据切分为训练集和验证集,供训练使用
  2. 通过train.py进行训练
  3. 训练完成后,通过sample.py运行查看结果

首先,数据准备非常简单,直接运行这个脚本即可:

$ cd nanoGPT
$ python data/shakespeare_char/prepare.py
length of dataset in characters: 1,115,394
all the unique characters:
 !$&',-.3:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
vocab size: 65
train has 1,003,854 tokens
val has 111,540 tokens

然后,开始真正的训练过程。由于MacBook上没有支持CUDA的显卡,只能使用CPU推理:

# 如果在不支持CUDA的设备上运行会报错:
# $ python train.py config/train_shakespeare_char.py
# raise AssertionError("Torch not compiled with CUDA enabled")
python train.py config/train_shakespeare_char.py --device=cpu --compile=False --eval_iters=20 --log_interval=1 --block_size=64 --batch_size=12 --n_layer=4 --n_head=4 --n_embd=128 --max_iters=2000 --lr_decay_iters=2000 --dropout=0.0

...
step 2000: train loss 1.7640, val loss 1.8925
saving checkpoint to out-shakespeare-char
iter 2000: loss 1.6982, time 306.45ms, mfu 0.05%

可以看到,train loss(训练损失)会逐渐减少,说明训练起到效果了。 整个过程需要大概1分钟的时间,训练结束后,就可以运行查看效果了:

$ python sample.py --out_dir=out-shakespeare-char --device=cpu
Overriding: out_dir = out-shakespeare-char
Overriding: device = cpu
number of parameters: 0.80M
Loading meta from data/shakespeare_char/meta.pkl...

I by doth what letterd fain flowarrman,
Lotheefuly daught shouss blate thou his though'd that opt--
Hammine than you, not neme your down way.

ELANUS:
I would and murser wormen that more?
...

至此,我们已经亲手训练并运行成功了nanoGPT,是不是很简单! 理论上如果通过GPU训练会更快一些,但是在MacBook上测试通过mps(--device=mps) 训练的时间与CPU几乎相当。