魚羊 發自 凹非寺量子位 報導 | 公眾號 QbitAI
OpenAI會講故事的文本生成模型GPT-2,現在有了更易用的封裝版本。
輕鬆上手,輕量級,速度快。這就是這隻名叫gpt2-client的包裝器的突出特性。
作者小哥 Rishabh Anand 表示,gpt2-client 簡化了整個文本生成過程,有了它,任何人都可以毫不費力地開始使用文本生成模型。
Rishabh Anand 小哥來自新加坡國家初級學院,是的你沒看錯,這是一位高中生。
五行代碼文本生成
gpt2-client 是 GPT-2 原始倉庫的包裝器。OpenAI 帶來的功能,gpt2-client 一樣能實現,並且這個「客戶端」更具可訪問性,可理解性,也更加易用。
不管是 117M(小型)GPT-2 模型,還是擁有15億參數的 345M(中型)GPT-2 模型,都能在五行代碼以內輕鬆玩轉。還能用輕鬆適配自定義數據集。
就像這樣。
實測幾分鐘之後,GPT-2就開始編故事了。
難怪網友會說,這真是令人難以置信:
Rishabh Anand 還放出了 Colab 在線 Demo,涵蓋了gpt2-client的基本功能,打開瀏覽器即可食用。
一起來看看效果。
輸入一個提示:In latest news(最新消息)
Enter a prompt got GPT-2 >> In latest newsprompt: In latest newsGenerating 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 GPT2Clientgpt2 = 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 GPT2Clientgpt2 = GPT2Client(117M』) # 可以是 345Mgpt2.generate (interactive=True) # 要求用戶輸入提示gpt2.generate (n_samples=4) # 生成 4 個文本片段text = gpt.generate (return_text=True) # 生成文本並返回 arraygpt2.generate (interactive=True, n_samples=3) #每次都有不同提示
生成選項非常靈活,可以根據需要生成的文本類型進行混合和匹配。
最後,根據自定義數據集微調 GPT-2。
from gpt2_client import GPT2Clientgpt2 = GPT2Client (『117M』)my_corpus = 『./data/shakespeare.txt』custom_text = gpt2.finetune (my_corpus, return_text = True) # 加載自定義數據集
這一步最好是用上 GPU 或 TPU,也可以試試Google Colab。
最後,再多說一句,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
— 完 —