来用 AI 写故事

请注意,这篇文章编写于2020年,其内容显然已经过时,请考虑使用性能比此模型高得多的 ChatGPT 或 GPT4。如果你在寻找开源的 LLM,请考虑 LLama、ChatGLM、千问、天鹰、百川等最新的模型

最近在玩 OpenAI 的 GPT-2。它是一种大型的基于 Transformer 的语言模型。OpenAI使用 15 亿参数和 800 万篇网络文本对其进行训练。训练后可以“ 根据文本中的所有先前单词,预测下一个单词”。它通过提示的小段文字来续写大段连贯文本。OpenAI 认为它生成的文本具有足够的欺骗性,可以“以假乱真”,以担心滥用为由迟迟不肯放出全部代码和模型,气得网友嘲讽其为 CloseAI 。

不过,目前 OpenAI 还是放出了部分代码和 7.74 亿参数的较小预训练模型。也有各路大佬复刻了 15 亿参数的OpenGPT-2,以及支持中文的 GPT2-Chinese、带有15亿参数中文预训练模型的 GPT2-ml。虽然效果或许没有 OpenAI 的 15 亿参数模型好,但也够我们玩一会了。想体验的可前往橘子的实践时间

(2020.6.25 更新,OpenAI 的 15 亿参数模型已放出,已经整理为 Colab 笔记本。)

GPT-2 有多强?

下面是 OpenAI 在其官网发布的 15 亿参数模型输出示例:

人工编写:

In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.

在一个惊人的发现中,科学家找到了一群独角兽,它们生活在安第斯山脉一个偏远的,以前未被开发的山谷中。更让研究人员惊讶的是,独角兽会说完美的英语。

AI续写:

The scientist named the population, after their distinctive horn, Ovid’s Unicorn. These four-horned, silver-white unicorns were previously unknown to science.

Now, after almost two centuries, the mystery of what sparked this odd phenomenon is finally solved.

Dr. Jorge Pérez, an evolutionary biologist from the University of La Paz, and several companions, were exploring the Andes Mountains when they found a small valley, with no other animals or humans. Pérez noticed that the valley had what appeared to be a natural fountain, surrounded by two peaks of rock and silver snow.

Pérez and the others then ventured further into the valley. “By the time we reached the top of one peak, the water looked blue, with some crystals on top,” said Pérez.

Pérez and his friends were astonished to see the unicorn herd. These creatures could be seen from the air without having to move too much to see them – they were so close they could touch their horns.
While examining these bizarre creatures the scientists discovered that the creatures also spoke some fairly regular English. Pérez stated, “We can see, for example, that they have a common ‘language,’ something like a dialect or dialectic.”

Dr. Pérez believes that the unicorns may have originated in Argentina, where the animals were believed to be descendants of a lost race of people who lived there before the arrival of humans in those parts of South America.
While their origins are still unclear, some believe that perhaps the creatures were created when a human and a unicorn met each other in a time before human civilization. According to Pérez, “In South America, such incidents seem to be quite common.”

However, Pérez also pointed out that it is likely that the only way of knowing for sure if unicorns are indeed the descendants of a lost alien race is through DNA. “But they seem to be able to communicate in English quite well, which I believe is a sign of evolution, or at least a change in social organization,” said the scientist.

这位科学家以其独特的角,将这个种群命名为 Ovid’s Unicorn 。这种四角银白色独角兽以前是科学界所不曾了解的。

现在,在将近两个世纪之后,终于解释了引发这种奇怪现象的谜团。

拉巴斯大学的进化生物学家豪尔赫·佩雷斯(JorgePérez)博士和几位同伴在安第斯山脉发现了一个没有其他动物或人类的小山谷。佩雷斯注意到该山谷似乎是一个天然喷泉,周围环绕着两个岩石和银色雪峰。

然后佩雷斯和其他人冒险进了山谷。佩雷斯说:“当我们到达一个山峰的顶部时,水看起来是蓝色的,上面还有一些晶体。”

佩雷斯和他的朋友们惊讶地看到了独角兽群。他们可以隔空看到这些生物,而不必尝试靠近就可以看到它们——它们太近了,他们甚至可以触摸它们的角。

在检查这些怪异的生物时,科学家发现这些生物也讲一些相当普通的英语。佩雷斯说:“例如,我们可以看到它们具有共同的“语言”,例如方言或辩证法。”

佩雷斯博士认为,独角兽可能起源于阿根廷,在那里人们相信这些动物是在人类到达南美这些地区之前居住在那里的一群迷失种族的后代。

尽管其起源尚不清楚,但有人认为,也许这些生物是人类和独角兽在人类文明诞生之前相遇而造的。佩雷斯说:“在南美,此类事件似乎很普遍。”

但是,佩雷斯还指出,要确定独角兽是否确实是迷失的人种的后裔,唯一可能的方法就是通过检测DNA。这位科学家说:“但是他们似乎能够用英语很好地交流,我相信这是进化的迹象,或者至少是社会组织的变化。”

???从未见过如此一本正经的胡说八道,写得跟真的似的,难怪OpenAI担心它被滥用...

GPT-2 生成文本实验

这里是橘子的生成结果。如果想尝试可以前往橘子的实践时间页面瞧瞧哦。

OpenAI官方 7.74亿参数 英语

人工编写:

My time was my own after the afternoon board, Saturdays, and I was accustomed to put it in on a little sail-boat on the bay.

AI续写:

Here, however, I hoped that the water heave would not throw back the sails and prevent my reaching the island. In this I was both disappointed and cautious, leaving out nearly half a mile from the windward side.

It failed to take place, however, and here it is that I reached the island. About three o'clock sharp I was completely startled and alarmed by crashing and gurgling noises; and in dismay and despair I turned to my friends and crew and said,

"What is the matter? has a mast-head been overthrown?" "That's the rule, sir: the water that heave up sometimes keeps the sails down. There was no mast break last night, only some greasey focus from the oars. But the waves have got drunk smooth down on top of the island now, and it looks as if the island was about to split." "Well!" said I, "if I had so much as known what it was about to do, I of course would have given up, but what you say is true. Perhaps it will come upon us soon, and if my boat got ashore we never could have spared ourselves from dashing the gills of a fitfully bobbing, several inches high, to our craft; but we don't know what it will grow to—and probably it will bring the island nearer."

These words interested us; we understand the danger, and while it is not dear to see what groans and groans the Guana Islands will throw up, the thing IS frightfully disturbing, and its action must be very highly commended by those desirous to Avoid troubling mischief in capture aware of its situation at our borders. We though it might prove a waking nightmare and a successive card game, did we spook it any more than by the rustle of our clothes, and the lightening of the load aunts, and that sense of dread which increases as we consider and shudder at the idea that in any shape or small distance the only life that human beings can possibly possess is going to be destroyed to haul ourselves by our clothes!

GPT2-Chinese 散文模型 中文

人工编写:

古人不见今时月,今月曾经照古人。

AI续写:

我走在古道上,回头望,只见月色如勾,月光如玉。古道西端,月华如洗,古道西下,古道西风瘦马鸣,不知几时梦里未央,我已分辨不出月色如霜,但至少月华如霜的夜晚,我望月,却只看见有个老人斑驳起来,佝偻在马路上,指着一横一竖起悲悲悲凄惨惨戚戚的隐曲。

我走在古道、老树下,夜色渐浓,月光透过树梢洒下一地的银光,清辉洒在地上,清辉洒在人身上,仿佛在寻找昨天的辉煌。

月光下面她均好袅娜的披着银辉,她静静地站在那里,她静静地站在那里,我知道,她在等待着谁,她一定在等待谁。

她默默地走到了我的面前,静静地站在那里,一脸灿烂,安详,安详。此刻,我想起了她,她在遥远的地方,她一定在她身旁。

她静静地躺在那里,她微笑着闭上了眼睛,安详地闭上了眼睛。此刻,我知道,她正坐在那里与我同样地看着我,我也在她的眼前。

秋日的余辉已染红了他的白发。

她静静地站在远方,她静静地望着她,静静地望着她。她静静地望着她,她也深情地望着他。

她也许只是静静地看着,也许只有这时她才能读懂他的话。也许只有静静地望着,静静地望着,望着,也许只有静静地看着,静静地望着,静静地看着,看着。他的心一下子就跳了起来,她在他的眼前晃啊晃。

GPT2-ml 15亿参数 中文

人工编写:

科学家在一个人迹罕至的山谷中发现了一群独角兽,它们能说流利的中文。

AI续写

科学家得出结论:这些独角兽可能是人类的祖先。通过对它们的dna分析,科学家 发现了它们的dna序列。独角兽生活在非洲索马里,经常在人的食物里下一粒肉。独角兽都是食肉目动物,因为只有食肉目动物才会保留大脑。约翰·霍普金 斯大学的卡夫卡博士在一篇论文中指出。但是,它们的非洲基因主要来自人类,但它们还有所不同。每年,这些可能出现在世界各地的独角兽大约可以延长至30亿年,而为了生存,一批批濒临灭绝的动物化石正在面临着灭绝的威胁。神秘物种:人类和独角兽之间的奇怪关系,从一个孤零零的动物园开始!它们可能很快就重现世界,而大部分的发现过程甚至都没有发生。人类和独角兽之间的奇怪关系,从一个孤零零的动物园开始!这是国际自然保护联盟发表的一份最新报告 ,宣称人类和独角兽之间的关系已经从极限运动,变成了生存竞赛。这个地球上几乎没有生物繁衍的能力,它们正面临灭绝的威胁。研究人员指出,在最新的研究中,他们发现了人类和独角兽之间一种基因突变大脑基因突变。这种突变可能是由dna和其他基因相互作用的结果。也就是说,造成这种基因突变的基因不是今天人类和其他生物基因中决定性的突变,而是早在古人类时期就已经存在了。当然,研究成果并不完全准确,却足以引起科学家的注意,因为在过去的四五十年中,科学家就发现了这一基因突变,此时科学家就已经确认了这个突变是多么重要,每一只独角兽都无法逃脱灭绝的命运。

关于 GPT-2

以下为科普性质而非技术研究性质,如有不正确的地方请大佬指正。

GPT-2 是基于 Transformer 的大型语言模型。与上一代模型GPT相比,它拥有10倍的参数和10倍的数据量。该模型不但可以生成文本的连贯段落,而且无需特殊训练就可以完成简单的阅读理解,翻译,问答和文本摘要。

就像我们带联想功能的输入法一样,GPT-2 预测下一个单词。

跟输入法不同的是,GPT-2 不是仅通过前一个字或词来预测,而是通过前面全文来预测。而且它具有巨大的数据集(大小为 40GB 的 800 万篇网页)。

GPT-2 将会为词表里的每个词计算一个概率,并选择概率最大(或从前几个中选取)的词作为输出。 它先将用户输入文本作为模型输入,每个单词产生后,会加在单词序列(文本)的最后,并将新序列作为模型的输入。这种机制叫做 自回归 (auto-regression) 。模型通过不断自回归,最终将输出一篇文章。

模型结构

GPT-2 使用 Transformers 进行语言建模。 Transformer 是一个带有自注意力( Self-Attention )机制的自然语言模型。它由编码器( Encoder )、解码器( Decoder )堆叠而成。

Transformer 结构,图片引用自 Jay Alammar : The Illustrated Transformer

在论文 「Generating Wikipedia by Summarizing Long Sequences」 中,提出了仅用 Transformer Decoder 构建语言模型。该模型通过去掉了 Encoder-Decoder Attention 层的 Decoder 堆叠而成。GPT-2 与之相似,越大的 GPT-2 模型需要的 Decoder 越多。

Transformer-Decoder 模型结构,图片引用自 Jay Alammar : The Illustrated GPT-2

GPT-2 结构,图片引用自 Jay Alammar : The Illustrated GPT-2

Decoder 也分为 自注意力层( Masked Self-Attention )和 前馈神经网络( Feed-Forward Neural Network )两部分,其中 自注意力 负责在处理单词时关注序列中所有单词,对每个单词在语段中的相关度进行评分。

Decoder 结构 和 自注意力机制 的应用,图片引用自 Jay Alammar : The Illustrated GPT-2

剩下还能再理解的一点点东西

词嵌入算法

模型输入的单词序列中每个单词输入第一个Decoder时,通过 词嵌入向量(Token Embedding) 将单词用数学方法表示。

  • 模型训练的时候会生成一个 嵌入矩阵 ( Embedding Matrix )。
  • 矩阵中每一行都是一个词嵌入向量。矩阵的行数为模型词汇表的长度,列数为每个词嵌入向量的维数。在GPT-2最大模型中,每个向量维数为1600。

Embedding Matrix 结构,图片引用自 Jay Alammar : The Illustrated GPT-2

  • 通过词嵌入向量还可以衡量两个单词的相关性,只需要通过计算两个词嵌入向量夹角即可。Similarity = cosθ .

毕竟还是高中生的底子,很多东西基础不行还是搞不定,以后看懂了再更。(咕咕咕)


参考资料:

OpenAI : Better Language Models and Their Implications

OpenAI : Improving Language Understanding with Unsupervised Learning

OpenAI : Language Models Are Unsupervised Multitask Learners

Jay Alammar : The Illustrated GPT-2

Jay Alammar : The Illustrated Transformer