溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

OPENAI?API微調(diào)GPT-3的Ada模型怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2023-04-12 15:49:16 來源:億速云 閱讀:133 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“OPENAI API微調(diào)GPT-3的Ada模型怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

需要提前安裝好 openai 所需要的各種庫,我這里的庫版本是 openai-0.25.0 。以及最關(guān)鍵過的 openai key ,這需要科學(xué)上網(wǎng),請(qǐng)自行解決。需要注意的是微調(diào)是要花錢的,不過最開始的注冊(cè)賬戶里默認(rèn)都有 5$ ,在開始之前到

https://platform.openai.com/account/usage

這里可以查看是否有余額。另外可以去

https://openai.com/pricing

查看微調(diào)不同模型的費(fèi)用,對(duì)于本文的介紹的內(nèi)容使用免費(fèi)的 5$ 是足夠的。

數(shù)據(jù)準(zhǔn)備

我們這里使用現(xiàn)成的數(shù)據(jù),從網(wǎng)上可以直接讀取使用,該數(shù)據(jù)主要有兩類包含棒球和曲棍球。并且會(huì)隨機(jī)打亂數(shù)據(jù),方便后續(xù)的訓(xùn)練??梢钥吹綌?shù)據(jù)的總量不大,只有 1197 條數(shù)據(jù)。

from sklearn.datasets import fetch_20newsgroups
import pandas as pd
import openai
categories = ['rec.sport.baseball', 'rec.sport.hockey']
sports_dataset = fetch_20newsgroups(subset='train', shuffle=True, random_state=42, categories=categories)
len_all, len_baseball, len_hockey = len(sports_dataset.data), len([e for e in sports_dataset.target if e == 0]), len([e for e in sports_dataset.target if e == 1])
print(f"Total examples: {len_all}, Baseball examples: {len_baseball}, Hockey examples: {len_hockey}")

打?。?/p>

Total examples: 1197, Baseball examples: 597, Hockey examples: 600

數(shù)據(jù)處理

為了加速我們的訓(xùn)練,我們這里選用打亂的訓(xùn)練集中的前 100 條數(shù)據(jù)來進(jìn)行演示效果,因?yàn)閿?shù)據(jù)多的話,時(shí)間消耗會(huì)長,而且微調(diào)的費(fèi)用會(huì)和訓(xùn)練數(shù)據(jù)成正比增加。

這里的數(shù)據(jù)一共有兩列,一列是 prompt 表示待分類的文本,一列是 completion 表示對(duì)應(yīng)文本描述的標(biāo)簽,標(biāo)簽只有兩類 baseball 和 hockey 。

labels = [sports_dataset.target_names[x].split('.')[-1] for x in sports_dataset['target']]
texts = [text.strip() for text in sports_dataset['data']]
df = pd.DataFrame(zip(texts, labels), columns = ['prompt','completion']) 
df = df[:100]

微調(diào)模型的輸入數(shù)據(jù)需要按照規(guī)定的格式進(jìn)行整理,這里使用常見的 jsonl 格式,使用 openai 庫自帶的工具進(jìn)行處理即可得到訓(xùn)練集 sport2_prepared_train.jsonl 和驗(yàn)證集 sport2_prepared_valid.jsonl 在當(dāng)前目錄。

df.to_json("sport2.jsonl", orient='records', lines=True)
!openai tools fine_tunes.prepare_data -f sport2.jsonl -q

模型訓(xùn)練

首先將你的 openai key 設(shè)置成環(huán)境變量 OPENAI_API_KEY 才能執(zhí)行下面的命令,該命令會(huì)使用指定的訓(xùn)練集和驗(yàn)證集進(jìn)行微調(diào)的分類任務(wù),并且會(huì)計(jì)算保留分類常見的指標(biāo),我們這里指定的模型為 ada 。

!openai api fine_tunes.create -t "sport2_prepared_train.jsonl" -v "sport2_prepared_valid.jsonl" --compute_classification_metrics --classification_positive_class " baseball" -m ada

打?。?/p>

Uploaded file from sport2_prepared_train.jsonl: file-wx9c3lYQB6Z4pWrrCqBabWUh
Uploaded file from sport2_prepared_valid.jsonl: file-aujZlpbhXZnevKzJNjF06q85
Created fine-tune: ft-aEHXhd8q9dfG8MOKt43ph7wk
Streaming events until fine-tuning is complete...
[2023-03-28 09:57:12] Created fine-tune: ft-aEHXhd8q9dfG8MOKt43ph7wk
[2023-03-28 09:59:16] Fine-tune costs $0.06
[2023-03-28 09:59:16] Fine-tune enqueued. Queue number: 2
[2023-03-28 09:59:32] Fine-tune is in the queue. Queue number: 1
(Ctrl-C will interrupt the stream, but not cancel the fine-tune)
[2023-03-28 09:57:12] Created fine-tune: ft-aEHXhd8q9dfG8MOKt43ph7wk
Stream interrupted (client disconnected).
To resume the stream, run:
  openai api fine_tunes.follow -i ft-aEHXhd8q9dfG8MOKt43ph7wk

從打印信息中我們能看到此次訓(xùn)練的花費(fèi),以及當(dāng)前的排隊(duì)情況,這個(gè)訓(xùn)練過程是在 openai 的服務(wù)器上進(jìn)行的,有時(shí)候長時(shí)間因?yàn)榕抨?duì)沒有響應(yīng)會(huì)自己斷開數(shù)據(jù)流的傳輸,我們?nèi)绻胍^續(xù)查看任務(wù)情況,只需要找到打印出來的唯一任務(wù)編碼,執(zhí)行下面的命令,我的遠(yuǎn)程服務(wù)器上的訓(xùn)練任務(wù)編碼是 ft-aEHXhd8q9dfG8MOKt43ph7wk ,其實(shí)上面的打印信息中都有相應(yīng)的提示。

openai api fine_tunes.follow -i ft-aEHXhd8q9dfG8MOKt43ph7wk
[2023-03-28 09:57:12] Created fine-tune: ft-aEHXhd8q9dfG8MOKt43ph7wk
[2023-03-28 09:59:16] Fine-tune costs $0.06
[2023-03-28 09:59:16] Fine-tune enqueued. Queue number: 2
[2023-03-28 09:59:32] Fine-tune is in the queue. Queue number: 1
[2023-03-28 10:12:20] Fine-tune is in the queue. Queue number: 0
[2023-03-28 10:13:54] Fine-tune started
[2023-03-28 10:14:22] Completed epoch 1/4
[2023-03-28 10:14:37] Completed epoch 2/4
[2023-03-28 10:14:50] Completed epoch 3/4
[2023-03-28 10:15:03] Completed epoch 4/4
[2023-03-28 10:15:26] Uploaded model: ada:ft-personal-2023-03-28-02-15-26
[2023-03-28 10:15:27] Uploaded result file: file-YZ2VNHkFnAJAhBeTKJ2AxfLK
[2023-03-28 10:15:27] Fine-tune succeeded

從打印信息中我們可以看到微調(diào)的結(jié)果模型叫 ada:ft-personal-2023-03-28-02-15-26 ,這個(gè)可以在 platform.openai.com/playground 里的模型選擇欄中看到自己微調(diào)后的模型。

訓(xùn)練信息打印

我們通過任務(wù)編碼可以獲取該任務(wù)訓(xùn)練的各種信息,比如隨著 epoch 變化的 loss 、acc 等信息??梢钥闯鲈谖覀兊挠?xùn)練集上訓(xùn)練的分類準(zhǔn)確率為 100% 。

!openai api fine_tunes.results -i ft-aEHXhd8q9dfG8MOKt43ph7wk > result.csv
results = pd.read_csv('result.csv')
results[results['classification/accuracy'].notnull()].tail(1)

打印信息:

	step	elapsed_tokens	elapsed_examples	training_loss	training_sequence_accuracy	training_token_accuracy	validation_loss	validation_sequence_accuracy	validation_token_accuracy	classification/accuracy	classification/precision	classification/recall	classification/auroc	classification/auprc	classification/f1.0
316	317	143557	317	0.02417	1.0	1.0	NaN	NaN	NaN	1.0	1.0	1.0	1.0	1.0	1.0

模型測試

我們隨機(jī)挑選驗(yàn)證集中的一條文本,使用微調(diào)后的模型進(jìn)行測試,打印出來的分類標(biāo)簽是正確的。

test = pd.read_json('sport2_prepared_valid.jsonl', lines=True)
res = openai.Completion.create(model= 'ada:ft-personal-2023-03-28-02-15-26', prompt=test['prompt'][0] + '\n\n###\n\n', max_tokens=1, temperature=0)
res['choices'][0]['text']

打印:

' hockey'

另外我們的微調(diào)分類器是非常通用的,不僅在我們使用的訓(xùn)練集和驗(yàn)證集上游泳,它也能用來預(yù)測推文。

sample_hockey_tweet = """Thank you to the 
@Canes
 and all you amazing Caniacs that have been so supportive! You guys are some of the best fans in the NHL without a doubt! Really excited to start this new chapter in my career with the 
@DetroitRedWings
 !!"""
res = openai.Completion.create(model='ada:ft-personal-2023-03-28-02-15-26', prompt=sample_hockey_tweet + '\n\n###\n\n', max_tokens=1, temperature=0, logprobs=2)
res['choices'][0]['text']

打?。?/p>

' baseball'

“OPENAI API微調(diào)GPT-3的Ada模型怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI