PenGPT:基于Transformer的手写与素描生成模型及数据构建复盘
PenGPT: A Generative Transformer for Handwriting and Sketches
A small transformer that writes and sketches from text. The effort was 80% data, 15% speed and 5% architecture: my first project that follows the Bitter Lesson.
一个能从文本中书写和素描的小型 Transformer。这项工作的投入比例是:数据占 80%,速度优化占 15%,架构设计占 5%:这是我第一个遵循‘苦涩教训’(Bitter Lesson)的项目。
Left. One phrase in the hands of 21 writers who were held out of training. The model read one to three real words by each of them and carried on in their hand. Right. Drawings from typed prompts. I picked these by eye from the best third of the samples; there are unpicked ones further down.
左图:一位短语由 21 位未参与训练的写作者执笔完成。模型阅读了每人提供的一到三个真实单词,并延续他们的笔迹风格。右图:基于输入提示生成的素描。我是通过肉眼从表现最好的前三分之一样本中挑选出来的;下方还有未被选中的样本。
Read the paper Get the code Download weights
阅读论文 获取代码 下载权重
The text of this post (except the conclusion) was written by Claude. I plan to rewrite it in my own words over Thanksgiving Break but needed to get the project out. So apologies for this move - all of my other posts were written by me, and all future posts will be as well. Even if you find Claude more comprehensible than my meandering thoughts, meander nonetheless I shall.
本文正文(除结论部分外)均由 Claude 撰写。我计划在感恩节假期期间用我自己的语言重写它,但需要先发布这个项目。因此对这一做法致歉——我之前的所有文章都是我自己写的,未来的文章也将如此。即使你觉得 Claude 的文字比我自己那些迂回曲折的想法更易于理解,我依然会坚持这种迂回的叙述方式。
Last year Zach Wimpee and I trained a tiny GPT to write cursive. It worked but it only knew one hand (mine) and it learned from 3,500 words that I wrote on a trackpad.
去年,Zach Wimpee 和我训练了一个小型 GPT 来书写草书。它确实有效,但它只认识一种笔迹(我的),并且是从我在触控板上书写的 3,500 个单词中学习而来的。
So here’s the obvious next question: can one small, ordinary transformer learn to hold a pen in lots of hands and draw as well as write? PenGPT is my attempt. There are two models. A 6M-parameter one writes print and cursive and picks up the handwriting of someone it has never seen from a few of their words. A 54M-parameter one draws from a text prompt.
那么显而易见的下一个问题是:一个小型、普通的 Transformer 能否学会在多种手中握笔,并像写字一样画画?PenGPT 是我的尝试。这里有两个模型。一个拥有 600 万参数的模型可以书写印刷体和草书,并能根据几个单词识别出它从未见过的人的笔迹。另一个拥有 5400 万参数的模型则能根据文本提示进行绘画。
Then I took the thing apart to find out why it works. The answer wasn’t what I expected.
然后我把这个模型拆解开来,以探究其工作原理。答案并非我所预期的那样。
How it works
工作原理
The whole recipe. Pen points snap to a grid (b) so a word becomes a walk in eight directions plus pen-down and pen-up. Byte-pair encoding merges common runs of moves into tokens (c): this "world" goes from 156 moves to 47 tokens. Everything else is a plain decoder that cross-attends to what it's told.
整体配方。笔尖坐标对齐到网格(b),因此一个单词变成八种方向加上落笔和抬笔的移动序列。字节对编码(Byte-pair encoding)将常见的移动序列合并为标记(c):这个“世界”从 156 次移动减少到 47 个标记。其余部分是一个普通的解码器,它对给定的指令进行交叉注意力计算。
That’s it for the modality-specific part: ten symbols and a merge count. The same tokenizer handles handwriting and drawings. For handwriting the model is told the text; for drawings it’s told a SigLIP 2 vector so a typed phrase can stand in for a picture.
这就是模态特定部分的全部内容:十个符号和一个合并计数。同一个分词器同时处理手写和素描。对于手写,模型接收文本信息;对于素描,它接收 SigLIP 2 向量,从而使输入的短语可以替代图片。
The data
数据
Almost nobody records pen strokes so I made the handwriting data from scans: 495,590 words by 1,255 writers, mostly from two research collections plus old letters and diaries from public archives. A vision-language model finds legible pages and transcribes them, the pages get cut into words, InkSight traces each word back into strokes and a set of checks throws out bad traces. Between a third and a half of the words survive.
几乎没有人记录笔触轨迹,所以我通过扫描制作了手写数据:包含 1,255 位写作者的 495,590 个单词,主要来自两个研究数据集,以及公共档案馆中的旧信件和日记。视觉-语言模型负责找出可读页面并进行转录,页面被切割成单词,InkSight 将每个单词回溯追踪为笔触轨迹,并通过一系列检查剔除错误的轨迹。最终约有三分之一到一半的单词得以保留。
The drawings are 477,670 sketches from ten public datasets. Quick, Draw! has 50 million and I kept the best 0.5%. I’m not releasing the datasets but the paper’s appendix explains how to build them.
这些图纸是来自十个公开数据集的477,670个草图。Quick, Draw! 拥有5000万张,我保留了其中最好的0.5%。我不会发布这些数据集,但论文的附录解释了如何构建它们。
Every writer the model knows, saying hello. Each one is placed by its learned vector, with print on the left and cursive on the right. Nobody told the model which was which. Color is the source the pages came from.
模型所知的每一位作者都在打招呼。每一个都被放置在其学习到的向量位置上,左侧是印刷体,右侧是手写连笔体。没有人告诉模型哪边是哪边。颜色反映了页面来源。
Training a model
训练模型
I compared PenGPT with older approaches then changed one thing at a time and retrained. Here are the rows I found most interesting; the paper has the rest.
我将PenGPT与旧方法进行了比较,然后一次只改变一个变量并重新训练。以下是我发现最有趣的几行;其余内容在论文中。
| Handwriting | Score change | Style rank | Legible |
|---|---|---|---|
| Graves-style LSTM, same tokens | +0.029 | 2.2 | 49% |
| ScribeTokens BPE, 60x the vocabulary | −0.001 | 1.9 | 53% |
| PenGPT | 0.716 | 1.9 | 54% |
| A tenth of the data | +0.050 | 3.0 | 47% |
| No augmentation | +0.028 | 2.0 | 52% |
| One word per sequence | +0.009 | 6.0 | 52% |
| No pen position | +0.001 | 1.9 | 52% |
| No BPE merges | −0.010 | – | 55% |
| Twice the parameters | −0.004 | 1.9 | 55% |
| Real ink | 66% |
| 手写 | 分数变化 | 风格排名 | 可读性 |
|---|---|---|---|
| Graves式LSTM,相同token | +0.029 | 2.2 | 49% |
| ScribeTokens BPE,词汇量60倍 | −0.001 | 1.9 | 53% |
| PenGPT | 0.716 | 1.9 | 54% |
| 十分之一的数据 | +0.050 | 3.0 | 47% |
| 无增强 | +0.028 | 2.0 | 52% |
| 每序列一个词 | +0.009 | 6.0 | 52% |
| 无笔位置 | +0.001 | 1.9 | 52% |
| 无BPE合并 | −0.010 | – | 55% |
| 参数翻倍 | −0.004 | 1.9 | 55% |
| 真实墨水 | 66% |
| Drawings | Score change | Matches label | Matches caption |
|---|---|---|---|
| sketch-rnn-style LSTM, same tokens | +0.049 | 40% | 20% |
| Trained on Quick, Draw! only | +0.002 | 74% | 37% |
| PenGPT | 0.591 | 76% | 43% |
| No augmentation | +0.013 | 4% | 6% |
| No pen position | +0.008 | 74% | 40% |
| Half the layers, half the width | +0.022 | 62% | 31% |
| 绘图 | 分数变化 | 匹配标签 | 匹配标题 |
|---|---|---|---|
| sketch-rnn式LSTM,相同token | +0.049 | 40% | 20% |
| 仅在Quick, Draw!上训练 | +0.002 | 74% | 37% |
| PenGPT | 0.591 | 76% | 43% |
| 无增强 | +0.013 | 4% | 6% |
| 无笔位置 | +0.008 | 74% | 40% |
| 层数减半,宽度减半 | +0.022 | 62% | 31% |
Rows above the line are comparisons with other approaches; rows below it each change one thing about PenGPT. Score is nats per pen move on held-out data, lower is better; the base rows vary by about 0.001 between seeds. Style rank is how well the model tells whose hand it is continuing (1 is perfect, 8.5 is chance). Legible is how often an OCR model reads a sampled word exactly. The drawing columns are how often SigLIP 2 picks a drawing's own prompt out of a few hundred.
线以上的行是将与其他方法的比较;线以下的各行则分别改变了PenGPT的一个方面。分数是在保留数据上每个笔移动的nats值,越低越好;基础行的种子间差异约为0.001。风格排名衡量模型判断其延续的是谁的手迹的能力(1为完美,8.5为随机)。可读性是指OCR模型准确读取采样单词的频率。绘图列显示SigLIP 2从几百个选项中选出该绘图自身提示的频率。
Data and how you show it. Those are the big rows. Cut the data and everything gets worse. Drop augmentation and the drawing model stops following its prompts entirely. Train on one word at a time and the model can no longer pick up a hand: style rank goes from 1.9 to 6.0.
数据和展示方式。那些是主要的行。削减数据会导致一切变差。去掉增强后,绘图模型完全不再遵循其提示。一次只训练一个词,模型就无法捕捉手迹:风格排名从1.9变为6.0。
The clever parts barely register. Telling the model where its pen is does nothing for handwriting. The BPE merges I was proud of make the score slightly worse; they buy speed (sequences are 3.2x shorter) and that’s all. A vocabulary sixty times larger changes nothing. Doubling the model changes nothing.
巧妙的部分几乎没什么影响。告诉模型笔的位置对手写没有帮助。我曾引以为傲的BPE合并使分数略微变差;它们换取了速度(序列缩短了3.2倍),仅此而已。六十倍大的词汇量毫无变化。模型翻倍也毫无变化。
Even the transformer is optional, sort of. An LSTM on the same tokens is a bit behind on handwriting and still picks up new hands. On drawings, where sequences are several times longer, it falls well behind.
甚至 Transformer 也是可选的,某种程度上如此。在相同 token 上运行的 LSTM 在手写识别方面稍显落后,但仍能捕捉新的笔迹。而在序列长度数倍的绘图任务中,它的表现则远远落后。
Samples
样本
Enough tables. Here’s what the two models make.
表格够了。以下是两个模型生成的结果。
Forty-eight of the 1,192 hands it trained on. The top two rows are sampled greedily so the differences there are all the writer. Further down the temperature rises and the letters loosen.
在其训练的 1,192 种笔迹中的 48 种。前两行是贪婪采样(greedy sampling),因此那里的差异完全来自书写者。往下温度逐渐升高,字母变得松散。
The drawing model takes a few controls besides the prompt. One of them is how much ink to use.
绘图模型除了提示词外还需要一些控制参数。其中之一是使用多少墨水。
Six prompts, with more ink requested from left to right. The house gains a door, then windows, then a chimney and the racket's strings turn into a cross-hatch. At the far right the model has run out of parts to add and starts going over its own lines.
六个提示词,从左到右要求使用更多墨水。房子先增加一扇门,然后是窗户,接着是烟囱,球拍的弦变成交叉网格。在最右侧,模型已无部件可添加,开始重复描画已有的线条。
It can also draw some things it was never shown as long as a blend of two familiar ideas will do.
只要结合两种熟悉的概念,它也能绘制从未展示过的东西。
Prompts that appear nowhere in the training data, one family per row: a face or legs on an object, wheels and wings, hats and ears and one thing shaped like another. I picked these from several tries each. None of them depends on word order, which turns out to matter (see the end of the post).
训练数据中完全不存在的提示词,每行一个家族:物体上的脸或腿、轮子和翅膀、帽子和耳朵,以及一个形状像另一个的东西。我从多次尝试中挑选了这些。它们都不依赖词序,而词序实际上很重要(见文章末尾)。
Picking up a new hand
捕捉新笔迹
The trick is almost embarrassingly simple. Each training sequence holds up to four words by one writer. That teaches the model that later words look like earlier ones so at sampling time you hand it a few real words and it keeps going. No style encoder, no fine-tuning. That’s how the 21 hands at the top of this post were written.
这个技巧几乎简单得令人尴尬。每个训练序列包含由同一位书写者书写的最多四个单词。这教会了模型后续单词看起来与前面的单词相似,因此在采样时,你给它几个真实单词,它就会继续生成。无需风格编码器,也无需微调。这就是本文顶部展示的 21 种笔迹的生成方式。
It also works from a photo. There are no strokes in a photo so I trace a few of its words with InkSight and fit one new writer vector to the traces, with the rest of the model frozen.
它也可以从照片中学习。照片中没有任何笔画,所以我用 InkSight 追踪其中的几个单词,并为这些追踪结果拟合一个新的书写者向量,同时冻结模型的其余部分。
Two hands learned from photos: a letter by the poet Sara Teasdale and a paragraph someone printed on a NIST form in 1989. From the top: the photo, words found in it, the model's copy of those words and new text in that hand. The printed copy keeps "genera", a word the cutting step clipped. The model copies what it's given.
从照片学习的两种笔迹:诗人萨拉·蒂丝代尔(Sara Teasdale)的一封信,以及某人在 1989 年打印在 NIST 表格上的一段文字。从上到下依次为:照片、从中找到的单词、模型复制的这些单词,以及该笔迹的新文本。打印副本保留了“genera”一词,这是裁剪步骤截断的结果。模型忠实复制所给内容。
Reading the letters
阅读信件
What the cross-attention heads read. (a) A few heads in layers 3 to 5 lock onto one position: this letter, the next letter or both. (b) Silencing the "this letter" head costs more than any other. (c) That head stepping through "letter" and "toward" as the pen writes them. Look at the t: it gets read twice, once for the stem and again for the cross and in cursive the cross doesn't come until the word is finished.
交叉注意力头读取的内容。(a) 第 3 至 5 层中的几个头锁定在一个位置:当前字母、下一个字母或两者兼有。(b) 抑制“当前字母”头的代价比其他任何头都高。(c) 当笔尖书写“letter”和“toward”时,该头依次经过这两个词。看字母 t:它被读取两次,一次用于竖笔,另一次用于横笔;而在草书中,横笔直到单词写完才出现。
One head in layer 4 puts 81% of its attention on the letter being written. I looked for heads that specialize in cursive or print and found none; every head reads the same positions in both.
第4层的一个注意力头将其81%的注意力集中在正在书写的字母上。我寻找专门处理草书或印刷体的注意力头,但未找到;每个注意力头在两种字体中读取相同的位置。
Do you need captions?
你需要字幕吗?
Captions are the expensive part of the drawing data. Since SigLIP puts pictures and text in the same space, maybe you could train on picture vectors alone and type a prompt at the end?
字幕是绘图数据中成本较高的部分。由于SigLIP将图片和文本置于同一空间,也许你可以仅使用图片向量进行训练,并在末尾输入提示词?
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力