minGPT TF
1.0.0
mingpt 的 TensorFlow 重新實現
play_math.ipynb
和play_char.ipynb
在 colab 中訓練。 play_char.ipynb
筆記本的batch_size
已減少以適合 colab GPU
記憶體。根據GPU記憶體更改參數。
GPT 訓練的 PyTorch 重新實現。 minGPT 試圖做到小、乾淨、可解釋和具有教育意義,因為目前可用的大多數都有點龐大。 GPT 不是一個複雜的模型,這個實作大約有 300 行程式碼,包括樣板檔案和完全不必要的自訂因果自註意力模組。不管怎樣,所發生的事情就是一系列索引進入一系列變壓器區塊,然後得出下一個索引的機率分佈。其餘的複雜性只是巧妙地進行批次(跨範例和序列長度),以便訓練高效。
核心 minGPT「函式庫」(hah) 是兩個檔案: 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
代碼:
論文+一些實作說明:
麻省理工學院