五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

加快GPT-2实验速度

鱼羊 发自 凹非寺

量子位 报道 | 公众号 QbitAI

OpenAI会讲故事的文本生成模型GPT-2,现在有了更易用的封装版本。

轻松上手轻量级速度快。这就是这只名叫gpt2-client的包装器的突出特性。

五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

作者小哥 Rishabh Anand 表示,gpt2-client 简化了整个文本生成过程,有了它,任何人都可以毫不费力地开始使用文本生成模型。

Rishabh Anand 小哥来自新加坡国家初级学院,是的你没看错,这是一位高中生。

五行代码文本生成

gpt2-client 是 GPT-2 原始仓库的包装器。OpenAI 带来的功能,gpt2-client 一样能实现,并且这个“客户端”更具可访问性,可理解性,也更加易用。

不管是 117M(小型)GPT-2 模型,还是拥有15亿参数的 345M(中型)GPT-2 模型,都能在五行代码以内轻松玩转。还能用轻松适配自定义数据集。

就像这样。

五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

实测几分钟之后,GPT-2就开始编故事了。

五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

难怪网友会说,这真是令人难以置信:

五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

Rishabh Anand 还放出了 Colab 在线 Demo,涵盖了gpt2-client的基本功能,打开浏览器即可食用。

一起来看看效果。

输入一个提示:In latest news(最新消息)

Enter a prompt got GPT-2 >> In latest news

prompt: In latest news

Generating sample…

————————————SAMPLE————————————

[‘ Zeta-Jones/The Washington PostSen.

泽塔琼斯/华盛顿邮报。

Bernie Sanders and House Minority Leader Nancy Pelosi made their remarks in response to a question on the role religion played in the 2016 election.”We were talking about religious liberty and we saw that in our country, and my faith,”

波尼·桑德斯和众议院少数党领袖南希·佩洛西在回答关于宗教在2016年大选中扮演的角色的问题时发表了上述言论。“我们谈论的是宗教自由,我们在我们国家和我的信仰中看到了这一点,”

Watch the video below, via MSNBC, at the top of the post.

观看帖子顶部 MSNBC 的视频。

One of my favorite things about the summer months is when I start going through a few pictures with some friends of mine. It means a lot that I’m having this crazy-easy weekend where I have some friends in the house and have this friend and all these interesting things going on. This is especially fun for me because I have no idea what’s going on but I keep thinking in a pretty good way how we should all be doing something together.

夏天的几个月里,我最喜欢的事情之一是和我的朋友们一起拍照。这意味着很多:我有一个疯狂的周末,我的一些朋友们在我家里,我拥有朋友和所有这些有趣的事情。这对我来说特别有趣,因为我不知道发生了什么,但我一直在思考我们如何用精彩的方式一起做些事。

…]

食用指南

想要亲自体验一下 gpt2-client,并不困难。

Python 版本 ≥ 3.5,TensorFlow 版本 ≥ 1.X,就能得到完美支持。如果选择Python 2.X,那么可能需要重新安装或升级某一些库。

首先,还是 pip 安装:

pip install gpt2-client

下载模型权重和检查点:

from gpt2_client import GPT2Client
gpt2 = GPT2Client(’117M’, save_dir = ‘models’)# 想用 345M 模型,就换成 ‘345M’
gpt2.download_model(force_download = False)# 使用缓存(如果有)。将 force_download 设置成 true 能重新下载文件

经过这一步,当前工作目录中会生成一个名为 models 的新目录,并下载模型所需的权重,检查点,模型 JSON 和超参数。一旦调用了download_model()函数,就假设文件已经在 models 目录中下载完成,无需再次调用。

接着就可以开始生成文本了:

from gpt2_client import GPT2Client
gpt2 = GPT2Client(117M’) # 可以是 345M
gpt2.generate (interactive=True) # 要求用户输入提示
gpt2.generate (n_samples=4) # 生成 4 个文本片段
text = gpt.generate (return_text=True) # 生成文本并返回 array
gpt2.generate (interactive=True, n_samples=3) #每次都有不同提示

生成选项非常灵活,可以根据需要生成的文本类型进行混合和匹配。

最后,根据自定义数据集微调 GPT-2。

from gpt2_client import GPT2Client
gpt2 = GPT2Client (‘117M’)
my_corpus = ‘./data/shakespeare.txt’
custom_text = gpt2.finetune (my_corpus, return_text = True) # 加载自定义数据集

这一步最好是用上 GPU 或 TPU,也可以试试Google Colab。

五行代码玩转GPT-2,新加坡高中生开源轻量级GPT-2“客户端”

最后,再多说一句,Rishabh Anand 小哥还是个写博客小能手,感兴趣的话可以 follow 他哟。

传送门

GitHub地址:

https://github.com/rish-16/gpt2client

Colab在线调教地址:

https://colab.research.google.com/drive/1RZwp1n6XeWxvhBjt1e3ATSOy4Mj9GEEl

Rishabh Anand 博客:

https://medium.com/@mail.rishabh.anand

— 完 —

版权所有,未经授权不得以任何形式转载及使用,违者必究。