minGPT TF
1.0.0
mingpt 的 TensorFlow 重新实现
play_math.ipynb
和play_char.ipynb
在 colab 中训练。每个笔记本顶部都有用于在 colab 上训练模型的链接。 play_char.ipynb
笔记本的batch_size
已减小以适合 colab GPU
内存。根据GPU内存更改参数。
GPT 训练的 PyTorch 重新实现。 minGPT 试图做到小、干净、可解释和具有教育意义,因为当前可用的大多数都有点庞大。 GPT 不是一个复杂的模型,这个实现大约有 300 行代码,包括样板文件和完全不必要的自定义因果自注意力模块。不管怎样,所发生的事情就是一系列索引进入一系列变压器块,然后得出下一个索引的概率分布。其余的复杂性只是巧妙地进行批处理(跨示例和序列长度),以便训练高效。
核心 minGPT“库”(哈)是两个文件: mingpt/model.py
包含实际的 Transformer 模型定义, mingpt/trainer.py
是训练模型的(独立于 GPT 的)PyTorch 样板文件。随附的 Jupyter 笔记本展示了如何使用“库”(哈哈)来训练序列模型:
play_math.ipynb
训练一个专注于加法的 GPT(受到 GPT-3 论文中加法部分的启发)play_char.ipynb
将 GPT 训练为任意文本上的字符级语言模型,类似于我的旧 char-rnn,但使用变压器而不是 RNNplay_words.ipynb
尚不存在的 BPE 版本使用 bpe 编码器、分布式训练以及也许 fp16,此实现可能能够重现 GPT-1/GPT-2 结果,尽管我还没有尝试过 $$$。 GPT-3 可能遥不可及,因为我的理解是它不适合 GPU 内存,需要更仔细的模型并行处理。
这段代码很简单,只需内联即可破解,而不是“使用”,但当前的 API 如下所示:
# you're on your own to define a class that returns individual examples as PyTorch LongTensors
from torch . utils . data import Dataset
train_dataset = MyDataset (...)
test_dataset = MyDataset (...)
# construct a GPT model
from mingpt . model import GPT , GPTConfig
mconf = GPTConfig ( vocab_size , block_size , n_layer = 12 , n_head = 12 , n_embd = 768 ) # a GPT-1
model = GPT ( mconf )
# construct a trainer
from mingpt . trainer import Trainer , TrainerConfig
tconf = TrainerConfig ( max_epochs = 10 , batch_size = 256 )
trainer = Trainer ( model , train_dataset , test_dataset , tconf )
trainer . train ()
# (... enjoy the show for a while... )
# sample from the model (the [None, ...] and [0] are to push/pop a needed dummy batch dimension)
from mingpt . utils import sample
x = torch . tensor ([ 1 , 2 , 3 ], dtype = torch . long )[ None , ...] # context conditioning
y = sample ( model , x , steps = 30 , temperature = 1.0 , sample = True , top_k = 5 )[ 0 ]
print ( y ) # our model filled in the integer sequence with 30 additional likely integers
代码:
论文+一些实施说明:
麻省理工学院