让OpenAI GPT3替我写数据竞赛代码!

OpenAI是在美国成立的人工智能研究公司,核心宗旨在于实现安全的通用人工智能(AGI)。他们开发的ChatGPT是一个最先进的自然语言处理模型,可以实时生成类似人类的文本。

ChatGPT 是从 GPT-3.5 系列中的一个模型进行微调的,该模型于 2022 年初完成训练。 GPT-3.5 系列是一系列模型,从 2021 年第四季度开始就混合使用文本和代码进行训练。

由于ChatGPT暂时是没有开源,且比较适合用于对话任务。为了让读者能逐步了解GPT能做什么,本文将介绍OpenAI已经公开的GPT3使用方法,可以使用免费的API来完成NLP和代码生成任务。

GPT3介绍

生成型预训练变换模型 3 (Generative Pre-trained Transformer 3,简称GPT3)是一个自回归语言模型,目的是为了使用深度学习生成人类可以理解的自然语言。GPT3的神经网路包含1750亿个参数,是当时参数最多的神经网路模型。

GPT3 模型拥有非常多个领域的先验知识,当用户通过自然语言向语言模型提出问题时,模型能够回答其中的大多数问题。

GPT3 模型相比以往模型(如 BERT)的另外优势,则是对于大多数常规任务,在使用模型之前无需对其进行微调(Fine-tuning)操作。

GPT-3 模型的优势在于,用户在使用该模型时,只需要告诉GPT3需要完成的任务即可,而不需要预先为想要完成的任务先微调一遍模型,比如:

Translate this into French: 
Where can I find a bookstore? Où puis-je trouver un magasin de livres?

以GPT3为首提出是基于预训练语言模型的新的微调范式:Prompt-Tuning,其通过添加模板的方法来避免引入额外的参数,从而让语言模型可以在小样本(Few-shot)或零样本(Zero-shot)场景下达到理想的效果。

OpenAI-GPT3 API注册

API介绍

OpenAI-GPT3 API 已部署在数以千计的应用程序中,其任务范围从帮助人们学习新语言到解决复杂的分类问题。

  • Github Copilot帮助更快地编写代码
  • Duolingo 使用 GPT-3 进行语法更正

API价格

对于学习者而言,也可以注册免费的GPT3 API,注册页面:https://openai.com/api/

让OpenAI GPT3替我写数据竞赛代码!

对于没有绑定信用卡的用户,可以免费使用约18美元的调用,当然不同的模型费用不同。

  • Ada:$0.0004  / 1K tokens
  • Babbage:$0.0005  / 1K tokens
  • Curie :$0.0020  / 1K tokens
  • Davinci:$0.0200  / 1K tokens

Ada 是最快的模型,而 Davinci 是最强大的。这里的token可以理解为pieces of words,整体的价格还是比较低的。

API使用

GPT3 API提供了多种语言的交互方法,最常见的Python可以参考如下代码:

pip install openai
import os
import openai

# 填写你的API KEY openai.api_key = 'XXX'

response = openai.Completion.create(
model="text-davinci-003",
prompt="Say this is a test",
temperature=0, max_tokens=7
)

更多安装指南:https://platform.openai.com/docs/libraries/community-libraries

API功能

OpenAI 训练了大量的模型,并通过API提供对这些模型的访问,可用于解决几乎任何涉及处理语言的任务。

  • Content generation
  • Summarization
  • Classification
  • Data extraction
  • Translation

让OpenAI GPT3替我写数据竞赛代码!

OpenAI-GPT3 API 模型

OpenAI API 由一系列具有不同功能和价位的模型提供支持:

  • GPT-3:能够理解并生成自然语言的模型
  • Codex:可以理解和生成代码的模型,包括将自然语言翻译成代码
  • Content filter:检测文本是否敏感或不安全的模型

GPT-3可以理解和生成自然语言,OpenAI提供四种主要类似,分别具有不同的功率级别,可适用于不同的任务。

Latest model Description Max request
text-davinci-003 Most capable GPT-3 model. Can do any task the other models can do. 4,000 tokens
text-curie-001 Very capable, but faster and lower cost than Davinci. 2,048 tokens
text-babbage-001 Capable of straightforward tasks, very fast, and lower cost. 2,048 tokens
text-ada-001 Capable of very simple tasks, usually the fastest model in the GPT-3 series, and lowest cost. 2,048 tokens

更多模型介绍细节:https://platform.openai.com/docs/models/overview

模型论文和实现细节:https://platform.openai.com/docs/model-index-for-researchers

OpenAI-GPT3 场景案例

Text completion

https://platform.openai.com/docs/guides/completion/introduction

文本分类

  • 单个文本分类Prompt
Decide whether a Tweet's sentiment is positive, neutral, or negative.

Tweet: I loved the new Batman movie!

Sentiment:

  • 多个文本分类Prompt
Classify the sentiment in these tweets:

1. "I can't stand homework" 2. "This sucks. I'm bored ?" 3. "I can't wait for Halloween!!!" 4. "My cat is adorable ❤️❤️" 5. "I hate chocolate"

Tweet sentiment ratings:

文本生成

  • 文本续写Prompt
Brainstorm some ideas combining VR and fitness:
  • 文本对话Prompt
The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.

Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?
Human:

  • 文本翻译Prompt
Translate this into French, Spanish and Japanese:

What rooms do you have available?

代码补全

https://platform.openai.com/docs/guides/code/best-practices

  • 写Python代码
"""
1. Create a list of first names
2. Create a list of last names
3. Combine them randomly into a list of 100 full names
""" 
  • 写MySQL代码
"""
Table customers, columns = [CustomerId, FirstName, LastName, Company, Address, City, State, Country, PostalCode, Phone, Fax, Email, SupportRepId]
Create a MySQL query for all customers in Texas named Jane
""" query =

OpenAI-GPT3 使用感受

在申请了免费的API后,笔者使用了多个例子,并尝试使用GPT3来编写一些竞赛代码。GPT3的功能比较多,确实比较强大,但也有一些缺点和注意事项。

  • OpenAI-GPT3对英文支持比较好,将英文转换为代码 的效果优于 将中文转换为代码的效果。
  • OpenAI-GPT3的结果效果取决于:
    • prompt文本
    • 给模型的提示或案例
    • 选择的模型版权
  • OpenAI-GPT3对对话支持的效果不如ChatGPT,但在完成特定任务上,效果比ChatGPT更好。

如下为编写的一段使用案例:

response = openai.Completion.create(
  model="text-davinci-003",
  prompt = '''
Convert this text to a python programmatic command

1. read train.csv and test.csv by pandas.
2. Is_Lead is the target variable, conver it to binary.
3. use decision tree from sklearn to train and predict on test.
'

'' ,
  max_tokens=512,
  frequency_penalty=0.0,
  presence_penalty=0.0
)

print(response['choices'][0]['text'].strip())


输出代码:

import pandas as pd
train = pd.read_csv('train.csv') test = pd.read_csv('test.csv')
train['Is_Lead'] = train['Is_Lead'].apply(lambda x: 1 if x == 'Yes' else 0) test['Is_Lead'] = test['Is_Lead'].apply(lambda x: 1 if x == 'Yes' else 0)

from sklearn.tree import DecisionTreeClassifier
DTmodel = DecisionTreeClassifier(random_state=2)
DTmodel.fit(train.drop('Is_Lead', axis=1), train.Is_Lead)
prediction = DTmodel.predict(test.drop('Is_Lead', axis=1))

参考资料

本文代码资料都来自OpenAI官网,更多资料和使用指南如下:

https://platform.openai.com/examples

https://platform.openai.com/docs/introduction

【竞赛报名/项目咨询请加微信:mollywei007】

微信扫一扫,分享到朋友圈

让OpenAI GPT3替我写数据竞赛代码!
上一篇

雅思目标6分的考生都不用太关注CC(Coherence and Cohesion)

下一篇

2023 AIME1 真题解析及答案公布

你也可能喜欢

  • 暂无相关文章!

关注热点

返回顶部