OPENAI?API?微調(diào)?GPT-3?的?Ada?模型
前言
本文主要是介紹了使用 openai 提供的 api 來完成對開放出來的模型進行微調(diào)操作。開放的模型有 curie 、babbage、ada 等,我這里以微調(diào) ada 舉例,其他類似。
需要提前安裝好 openai 所需要的各種庫,我這里的庫版本是 openai-0.25.0 。以及最關(guān)鍵過的 openai key ,這需要科學(xué)上網(wǎng),請自行解決。需要注意的是微調(diào)是要花錢的,不過最開始的注冊賬戶里默認(rèn)都有 5$ ,在開始之前到
https://platform.openai.com/account/usage
這里可以查看是否有余額。另外可以去
https://openai.com/pricing
查看微調(diào)不同模型的費用,對于本文的介紹的內(nèi)容使用免費的 5$ 是足夠的。
數(shù)據(jù)準(zhǔn)備
我們這里使用現(xiàn)成的數(shù)據(jù),從網(wǎng)上可以直接讀取使用,該數(shù)據(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ù)來進行演示效果,因為數(shù)據(jù)多的話,時間消耗會長,而且微調(diào)的費用會和訓(xùn)練數(shù)據(jù)成正比增加。
這里的數(shù)據(jù)一共有兩列,一列是 prompt 表示待分類的文本,一列是 completion 表示對應(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ī)定的格式進行整理,這里使用常見的 jsonl 格式,使用 openai 庫自帶的工具進行處理即可得到訓(xùn)練集 sport2_prepared_train.jsonl 和驗證集 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í)行下面的命令,該命令會使用指定的訓(xùn)練集和驗證集進行微調(diào)的分類任務(wù),并且會計算保留分類常見的指標(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)練的花費,以及當(dāng)前的排隊情況,這個訓(xùn)練過程是在 openai 的服務(wù)器上進行的,有時候長時間因為排隊沒有響應(yīng)會自己斷開數(shù)據(jù)流的傳輸,我們?nèi)绻胍^續(xù)查看任務(wù)情況,只需要找到打印出來的唯一任務(wù)編碼,執(zhí)行下面的命令,我的遠(yuǎn)程服務(wù)器上的訓(xùn)練任務(wù)編碼是 ft-aEHXhd8q9dfG8MOKt43ph7wk ,其實上面的打印信息中都有相應(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 ,這個可以在 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
模型測試
我們隨機挑選驗證集中的一條文本,使用微調(diào)后的模型進行測試,打印出來的分類標(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']
打?。?/p>
' hockey'
另外我們的微調(diào)分類器是非常通用的,不僅在我們使用的訓(xù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']
打印:
' baseball'
總結(jié)
其實使用 openai 的微調(diào) api 只需要四步:
- 準(zhǔn)備環(huán)境和 key
- 準(zhǔn)備規(guī)定格式的數(shù)據(jù)
- 訓(xùn)練模型
- 模型推理
以上就是OPENAI API 微調(diào) GPT-3 的 Ada 模型的詳細(xì)內(nèi)容,更多關(guān)于OPENAI API微調(diào)GPT-3 Ada 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Pycharm創(chuàng)建文件時自動生成文件頭注釋(自定義設(shè)置作者日期)
這篇文章主要介紹了Pycharm創(chuàng)建文件時自動生成文件頭注釋(自定義設(shè)置作者日期),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python標(biāo)準(zhǔn)庫:內(nèi)置函數(shù)max(iterable, *[, key, default])說明
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫:內(nèi)置函數(shù)max(iterable, *[, key, default])說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04