自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

無需寫代碼能力,手搓最簡單BabyGPT模型:前特斯拉AI總監(jiān)新作

人工智能 新聞
GPT 原來這么簡單?

我們知道,OpenAI 的 GPT 系列通過大規(guī)模和預(yù)訓(xùn)練的方式打開了人工智能的新時(shí)代,然而對于大多數(shù)研究者來說,語言大模型(LLM)因?yàn)轶w量和算力需求而顯得高不可攀。在技術(shù)向上發(fā)展的同時(shí),人們也一直在探索「最簡」的 GPT 模式。

近日,特斯拉前 AI 總監(jiān),剛剛回歸 OpenAI 的 Andrej Karpathy 介紹了一種最簡 GPT 的玩法,或許能為更多人了解這種流行 AI 模型背后的技術(shù)帶來幫助。

圖片

是的,這是一個(gè)帶有兩個(gè) token 0/1 和上下文長度為 3 的極簡 GPT,將其視為有限狀態(tài)馬爾可夫鏈。它在序列「111101111011110」上訓(xùn)練了 50 次迭代,Transformer 的參數(shù)和架構(gòu)修改了箭頭上的概率。

例如我們可以看到:

  • 在訓(xùn)練數(shù)據(jù)中,狀態(tài) 101 確定性地轉(zhuǎn)換為 011,因此該轉(zhuǎn)換的概率變得更高 (79%)。但不接近于 100%,因?yàn)檫@里只做了 50 步優(yōu)化。
  • 狀態(tài) 111 以 50% 的概率分別進(jìn)入 111 和 110,模型幾乎已學(xué)會了(45%、55%)。
  • 在訓(xùn)練期間從未遇到過像 000 這樣的狀態(tài),但具有相對尖銳的轉(zhuǎn)換概率,例如 73% 轉(zhuǎn)到 001。這是 Transformer 歸納偏差的結(jié)果。你可能會想這是 50%,除了在實(shí)際部署中幾乎每個(gè)輸入序列都是唯一的,而不是逐字地出現(xiàn)在訓(xùn)練數(shù)據(jù)中。

通過簡化,Karpathy 已讓 GPT 模型變得易于可視化,讓你可以直觀地了解整個(gè)系統(tǒng)。

你可以在這里嘗試它:https://colab.research.google.com/drive/1SiF0KZJp75rUeetKOWqpsA8clmHP6jMg?usp=sharing

實(shí)際上,即使是 GPT 的最初版本,模型的體量很相當(dāng)可觀:在 2018 年,OpenAI 發(fā)布了第一代 GPT 模型,從論文《Improving Language Understanding by Generative Pre-Training》可以了解到,其采用了 12 層的 Transformer Decoder 結(jié)構(gòu),使用約 5GB 無監(jiān)督文本數(shù)據(jù)進(jìn)行訓(xùn)練。

但如果將其概念簡化,GPT 是一種神經(jīng)網(wǎng)絡(luò),它采用一些離散 token 序列并預(yù)測序列中下一個(gè) token 的概率。例如,如果只有兩個(gè)標(biāo)記 0 和 1,那么一個(gè)很小的二進(jìn)制 GPT 可以例如告訴我們:

[0,1,0] ---> GPT ---> [P (0) = 20%, P (1) = 80%]

在這里,GPT 采用位序列 [0,1,0],并根據(jù)當(dāng)前的參數(shù)設(shè)置,預(yù)測下一個(gè)為 1 的可能性為 80%。重要的是,默認(rèn)情況下 GPT 的上下文長度是有限的。如果上下文長度為 3,那么它們在輸入時(shí)最多只能使用 3 個(gè) token。在上面的例子中,如果我們拋出一枚有偏差的硬幣并采樣 1 確實(shí)應(yīng)該是下一個(gè),那么我們將從原始狀態(tài) [0,1,0] 轉(zhuǎn)換到新狀態(tài) [1,0,1]。我們在右側(cè)添加了新位 (1),并通過丟棄最左邊的位 (0) 將序列截?cái)酁樯舷挛拈L度 3,然后可以一遍又一遍地重復(fù)這個(gè)過程以在狀態(tài)之間轉(zhuǎn)換。

很明顯,GPT 是一個(gè)有限狀態(tài)馬爾可夫鏈:有一組有限的狀態(tài)和它們之間的概率轉(zhuǎn)移箭頭。每個(gè)狀態(tài)都由 GPT 輸入處 token 的特定設(shè)置定義(例如 [0,1,0])。我們可以以一定的概率將其轉(zhuǎn)換到新狀態(tài),如 [1,0,1]。讓我們詳細(xì)看看它是如何工作的:

# hyperparameters for our GPT
# vocab size is 2, so we only have two possible tokens: 0,1
vocab_size = 2
# context length is 3, so we take 3 bits to predict the next bit probability
context_length = 3

GPT 神經(jīng)網(wǎng)絡(luò)的輸入是長度為 context_length 的 token 序列。這些 token 是離散的,因此狀態(tài)空間很簡單:

print ('state space (for this exercise) = ', vocab_size ** context_length)
# state space (for this exercise) = 8

細(xì)節(jié):準(zhǔn)確來說,GPT 可以采用從 1 到 context_length 的任意數(shù)量的 token。因此如果上下文長度為 3,原則上我們可以在嘗試預(yù)測下一個(gè) token 時(shí)輸入 1 個(gè)、2 個(gè)或 3 個(gè) token。這里我們忽略這一點(diǎn)并假設(shè)上下文長度已「最大化」,只是為了簡化下面的一些代碼,但這一點(diǎn)值得牢記。

print ('actual state space (in reality) = ', sum (vocab_size ** i for i in range (1, context_length+1)))
# actual state space (in reality) = 14

我們現(xiàn)在要在 PyTorch 中定義一個(gè) GPT。出于本筆記本的目的,你無需理解任何此代碼。

現(xiàn)在讓我們構(gòu)建 GPT 吧:

config = GPTConfig (
block_size = context_length,
vocab_size = vocab_size,
n_layer = 4,
n_head = 4,
n_embd = 16,
bias = False,
)
gpt = GPT (config)

對于這個(gè)筆記本你不必?fù)?dān)心 n_layer、n_head、n_embd、bias,這些只是實(shí)現(xiàn) GPT 的 Transformer 神經(jīng)網(wǎng)絡(luò)的一些超參數(shù)。

GPT 的參數(shù)(12656 個(gè))是隨機(jī)初始化的,它們參數(shù)化了狀態(tài)之間的轉(zhuǎn)移概率。如果你平滑地更改這些參數(shù),就會平滑地影響狀態(tài)之間的轉(zhuǎn)換概率。

現(xiàn)在讓我們試一試隨機(jī)初始化的 GPT。讓我們獲取上下文長度為 3 的小型二進(jìn)制 GPT 的所有可能輸入:

def all_possible (n, k):
# return all possible lists of k elements, each in range of [0,n)
if k == 0:
yield []
else:
for i in range (n):
for c in all_possible (n, k - 1):
yield [i] + c
list (all_possible (vocab_size, context_length))
[[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]]

這是 GPT 可能處于的 8 種可能狀態(tài)。讓我們對這些可能的標(biāo)記序列中的每一個(gè)運(yùn)行 GPT,并獲取序列中下一個(gè)標(biāo)記的概率,并繪制為可視化程度比較高的圖形:

# we'll use graphviz for pretty plotting the current state of the GPT
from graphviz import Digraph

def plot_model ():
dot = Digraph (comment='Baby GPT', engine='circo')

for xi in all_possible (gpt.config.vocab_size, gpt.config.block_size):

# forward the GPT and get probabilities for next token
x = torch.tensor (xi, dtype=torch.long)[None, ...] # turn the list into a torch tensor and add a batch dimension
logits = gpt (x) # forward the gpt neural net
probs = nn.functional.softmax (logits, dim=-1) # get the probabilities
y = probs [0].tolist () # remove the batch dimension and unpack the tensor into simple list
print (f"input {xi} ---> {y}")

# also build up the transition graph for plotting later
current_node_signature = "".join (str (d) for d in xi)
dot.node (current_node_signature)
for t in range (gpt.config.vocab_size):
next_node = xi [1:] + [t] # crop the context and append the next character
next_node_signature = "".join (str (d) for d in next_node)
p = y [t]
label=f"{t}({p*100:.0f}%)"
dot.edge (current_node_signature, next_node_signature, label=label)

return dot

plot_model ()
input [0, 0, 0] ---> [0.4963349997997284, 0.5036649107933044] 
input [0, 0, 1] ---> [0.4515703618526459, 0.5484296679496765]
input [0, 1, 0] ---> [0.49648362398147583, 0.5035163760185242]
input [0, 1, 1] ---> [0.45181113481521606, 0.5481888651847839]
input [1, 0, 0] ---> [0.4961162209510803, 0.5038837194442749]
input [1, 0, 1] ---> [0.4517717957496643, 0.5482282042503357]
input [1, 1, 0] ---> [0.4962802827358246, 0.5037197470664978]
input [1, 1, 1] ---> [0.4520467519760132, 0.5479532480239868]

我們看到了 8 個(gè)狀態(tài),以及連接它們的概率箭頭。因?yàn)橛?2 個(gè)可能的標(biāo)記,所以每個(gè)節(jié)點(diǎn)有 2 個(gè)可能的箭頭。請注意,在初始化時(shí),這些概率中的大多數(shù)都是統(tǒng)一的(在本例中為 50%),這很好而且很理想,因?yàn)槲覀兩踔粮緵]有訓(xùn)練模型。

下面開始訓(xùn)練:

# let's train our baby GPT on this sequence
seq = list (map (int, "111101111011110"))
seq
[1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0]
# convert the sequence to a tensor holding all the individual examples in that sequence
X, Y = [], []
# iterate over the sequence and grab every consecutive 3 bits
# the correct label for what's next is the next bit at each position
for i in range (len (seq) - context_length):
X.append (seq [i:i+context_length])
Y.append (seq [i+context_length])
print (f"example {i+1:2d}: {X [-1]} --> {Y [-1]}")
X = torch.tensor (X, dtype=torch.long)
Y = torch.tensor (Y, dtype=torch.long)
print (X.shape, Y.shape)

我們可以看到在那個(gè)序列中有 12 個(gè)示例。現(xiàn)在讓我們訓(xùn)練它:

# init a GPT and the optimizer
torch.manual_seed (1337)
gpt = GPT (config)
optimizer = torch.optim.AdamW (gpt.parameters (), lr=1e-3, weight_decay=1e-1)
# train the GPT for some number of iterations
for i in range (50):
logits = gpt (X)
loss = F.cross_entropy (logits, Y)
loss.backward ()
optimizer.step ()
optimizer.zero_grad ()
print (i, loss.item ())
print ("Training data sequence, as a reminder:", seq)
plot_model ()

我們沒有得到這些箭頭的準(zhǔn)確 100% 或 50% 的概率,因?yàn)榫W(wǎng)絡(luò)沒有經(jīng)過充分訓(xùn)練,但如果繼續(xù)訓(xùn)練,你會期望接近。

請注意一些其他有趣的事情:一些從未出現(xiàn)在訓(xùn)練數(shù)據(jù)中的狀態(tài)(例如 000 或 100)對于接下來應(yīng)該出現(xiàn)的 token 有很大的概率。如果在訓(xùn)練期間從未遇到過這些狀態(tài),它們的出站箭頭不應(yīng)該是 50% 左右嗎?這看起來是個(gè)錯誤,但實(shí)際上是可取的,因?yàn)樵诓渴鹌陂g的真實(shí)應(yīng)用場景中,幾乎每個(gè) GPT 的測試輸入都是訓(xùn)練期間從未見過的輸入。我們依靠 GPT 的內(nèi)部結(jié)構(gòu)(及其「歸納偏差」)來適當(dāng)?shù)貓?zhí)行泛化。

大小比較:

  • GPT-2 有 50257 個(gè) token 和 2048 個(gè) token 的上下文長度。所以 `log2 (50,257) * 2048 = 每個(gè)狀態(tài) 31,984 位 = 3,998 kB。這足以實(shí)現(xiàn)量變。
  • GPT-3 的上下文長度為 4096,因此需要 8kB 的內(nèi)存;大約相當(dāng)于 Atari 800。
  • GPT-4 最多 32K 個(gè) token,所以大約 64kB,即 Commodore64。
  • I/O 設(shè)備:一旦開始包含連接到外部世界的輸入設(shè)備,所有有限狀態(tài)機(jī)分析就會崩潰。在 GPT 領(lǐng)域,這將是任何一種外部工具的使用,例如必應(yīng)搜索能夠運(yùn)行檢索查詢以獲取外部信息并將其合并為輸入。

Andrej Karpathy 是 OpenAI 的創(chuàng)始成員和研究科學(xué)家。但在 OpenAI 成立一年多后,Karpathy 便接受了馬斯克的邀請,加入了特斯拉。在特斯拉工作的五年里,他一手促成了 Autopilot 的開發(fā)。這項(xiàng)技術(shù)對于特斯拉的完全自動駕駛系統(tǒng) FSD 至關(guān)重要,也是馬斯克針對 Model S、Cybertruck 等車型的賣點(diǎn)之一。

今年 2 月,在 ChatGPT 火熱的背景下,Karpathy 回歸 OpenAI,立志構(gòu)建現(xiàn)實(shí)世界的 JARVIS 系統(tǒng)。

圖片

最近一段時(shí)間,Karpathy 給大家貢獻(xiàn)了很多學(xué)習(xí)材料,包括詳解反向傳播的課程 、重寫的 minGPT 庫、從零開始構(gòu)建 GPT 模型的完整教程等。

責(zé)任編輯:張燕妮 來源: 機(jī)器之心
相關(guān)推薦

2022-08-21 21:15:28

模型AI

2022-11-01 13:42:54

雷達(dá)智能

2024-04-09 12:23:27

C語言AI

2021-12-09 10:26:35

AI 數(shù)據(jù)人工智能

2023-01-16 14:33:31

GitHubAI

2024-07-09 09:36:17

2023-11-24 13:00:32

2022-03-16 18:38:06

AI特斯拉神經(jīng)網(wǎng)絡(luò)

2023-07-05 15:18:42

AI自動駕駛

2024-01-29 13:56:55

AI數(shù)據(jù)

2025-04-09 03:00:00

簽字板代碼canvas

2023-02-27 09:29:05

GPT模型

2025-02-17 14:43:51

2025-01-13 02:00:00

模型訓(xùn)練數(shù)據(jù)

2023-02-02 13:22:40

AICIFAR數(shù)據(jù)集

2025-04-29 16:12:28

AI模型訓(xùn)練

2021-04-14 14:41:39

AI 數(shù)據(jù)人工智能

2024-01-29 07:05:00

自動駕駛技術(shù)

2025-04-25 09:20:00

數(shù)據(jù)模型AI

2024-12-02 09:20:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號