本文正在参加platform.openai.com/playground 里的模型选择栏中看到自己微调后的模型。
训练信息打印
我们通过任务编码可以获取该任务训练的各种信息,比如随着 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
模型测试
我们随机挑选验证集中的一条文本,使用微调后的模型进行测试,打印出来的分类标签是正确的。
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] + 'nn###nn', max_tokens=1, temperature=0)
res['choices'][0]['text']
打印:
' hockey'
另外我们的微调分类器是非常通用的,不仅在我们使用的训练集和验证集上游泳,它也能用来预测推文。
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 + 'nn###nn', max_tokens=1, temperature=0, logprobs=2)
res['choices'][0]['text']
打印:
' baseball'
总结
其实使用 openai 的微调 api 只需要四步:
- 准备环境和 key
- 准备规定格式的数据
- 训练模型
- 模型推理
是不是很简单!