最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OPENAI?API?微調(diào)?GPT-3?的?Ada?模型

 更新時間:2023年04月12日 13:59:35   作者:我是王大你是誰  
這篇文章主要為大家介紹了OPENAI?API?微調(diào)?GPT-3?的?Ada?模型使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

本文主要是介紹了使用 openai 提供的 api 來完成對開放出來的模型進行微調(diào)操作。開放的模型有 curie 、babbage、ada 等,我這里以微調(diào) ada 舉例,其他類似。

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

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

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

https://openai.com/pricing 

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

數(shù)據(jù)準備

我們這里使用現(xiàn)成的數(shù)據(jù),從網(wǎng)上可以直接讀取使用,該數(shù)據(jù)主要有兩類包含棒球和曲棍球。并且會隨機打亂數(shù)據(jù),方便后續(xù)的訓練??梢钥吹綌?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ù)處理

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

這里的數(shù)據(jù)一共有兩列,一列是 prompt 表示待分類的文本,一列是 completion 表示對應文本描述的標簽,標簽只有兩類 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 庫自帶的工具進行處理即可得到訓練集 sport2_prepared_train.jsonl 和驗證集 sport2_prepared_valid.jsonl 在當前目錄。

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

模型訓練

首先將你的 openai key 設置成環(huán)境變量 OPENAI_API_KEY 才能執(zhí)行下面的命令,該命令會使用指定的訓練集和驗證集進行微調(dià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

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

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)的結果模型叫 ada:ft-personal-2023-03-28-02-15-26 ,這個可以在 platform.openai.com/playground 里的模型選擇欄中看到自己微調(diào)后的模型。

訓練信息打印

我們通過任務編碼可以獲取該任務訓練的各種信息,比如隨著 epoch 變化的 loss 、acc 等信息??梢钥闯鲈谖覀兊挠柧毤嫌柧毜姆诸悳蚀_率為 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)后的模型進行測試,打印出來的分類標簽是正確的。

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)分類器是非常通用的,不僅在我們使用的訓練集和驗證集上游泳,它也能用來預測推文。

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 的微調(diào) api 只需要四步:

  • 準備環(huán)境和 key
  • 準備規(guī)定格式的數(shù)據(jù)
  • 訓練模型
  • 模型推理

以上就是OPENAI API 微調(diào) GPT-3 的 Ada 模型的詳細內(nèi)容,更多關于OPENAI API微調(diào)GPT-3 Ada 的資料請關注腳本之家其它相關文章!

相關文章

最新評論

延长县| 奉新县| 中阳县| 师宗县| 察哈| 潜江市| 雅江县| 黎平县| 济南市| 广南县| 长沙市| 桑日县| 乌审旗| 裕民县| 舟曲县| 黄骅市| 宣汉县| 特克斯县| 嘉鱼县| 日土县| 南城县| 屏山县| 贵德县| 防城港市| 固镇县| 红桥区| 留坝县| 桑植县| 泰兴市| 科技| 新巴尔虎左旗| 滁州市| 和静县| 长武县| 昌邑市| 株洲市| 疏勒县| 格尔木市| 齐齐哈尔市| 和硕县| 永济市|