Menu
快讀
  • 旅遊
  • 生活
    • 美食
    • 寵物
    • 養生
    • 親子
  • 娛樂
    • 動漫
  • 時尚
  • 社會
  • 探索
  • 故事
  • 科技
  • 軍事
  • 国际
快讀

五行代碼玩轉GPT-2,新加坡高中生開源輕量級GPT-2“客戶端”

2021 年 3 月 11 日 江干发布

魚羊 發自 凹非寺 
量子位 報道 | 公衆號 QbitAI

OpenAI會講故事的文本生成模型GPT-2,現在有了更易用的封裝版本。

輕松上手,輕量級,速度快。這就是這只名叫gpt2-client的包裝器的突出特性。

33ac522077ccf5f407e8172ffbfcec29.jpeg

作者小哥 Rishabh Anand 表示,gpt2-client 簡化了整個文本生成過程,有了它,任何人都可以毫不費力地開始使用文本生成模型。

Rishabh Anand 小哥來自新加坡國家初級學院,是的你沒看錯,這是一位高中生。

五行代碼文本生成

gpt2-client 是 GPT-2 原始倉庫的包裝器。OpenAI 帶來的功能,gpt2-client 一樣能實現,並且這個“客戶端”更具可訪問性,可理解性,也更加易用。

不管是 117M(小型)GPT-2 模型,還是擁有15億參數的 345M(中型)GPT-2 模型,都能在五行代碼以內輕松玩轉。還能用輕松適配自定義數據集。

就像這樣。

80495dec4a440d072384be568e935af3.png

實測幾分鍾之後,GPT-2就開始編故事了。

7e459c3e29fea013b4e43ea9306356a5.png

而原來,單單准備模型就需要三行代碼:

git clone http://github.com/nshepperd/gpt-2
pip install -r requirements.txt
python download_model.py 117M

難怪網友會說,這真是令人難以置信:

8d38092a756957591bdea24abd8af575.png

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。

69cb3737b0b769b36ec8d1b65716db1a.jpeg

最後,再多說一句,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

— 完 —

加入社群 | 與優秀的人交流

1a70bf22b7a0d74cdcbc12110d19a98d.jpeg

小程序 | 全類別AI學習教程

0e88c6e9848c883cffcda733eaae5829.jpeg

10c04e2be37becfe97f991d7e7625772.jpeg

量子位 QbitAI · 頭條號簽約作者

վ’ᴗ’ ի 追蹤AI技術和産品新動態

喜歡就點「好看」吧 !

相關文章:

  • UFC格鬥之夜162綜述:地面纏鬥對決瑪雅裸絞成功逆轉
  • 庫克治下,蘋果營收和股價雙雙創曆史新高!iPhone貢獻70%
  • 余承東:國內用華爲P40 Pro+就能拍照測體溫,還很精准
  • 余承東:P40系列生産去年年底已開啓,在中國可拍照測體溫
  • 楊元慶發布新財年內部信,將首次以網絡直播舉行全球誓師大會
  • 這家僞AI創業公司被扒:曾創歐洲單輪融資紀錄,然而有人工沒智能
美食

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

©2025 快讀 | 服務協議 | DMCA | 聯繫我們