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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 像Transformer一样思考
|
2 |
+
- [论文](https://arxiv.org/pdf/2106.06981.pdf)来自Gail Weiss, Yoav Goldberg,Eran Yahav.
|
3 |
+
- 博客参考[ Sasha Rush ](https://rush-nlp.com/)和[ Gail Weiss ](https://sgailw.cswp.cs.technion.ac.il/)
|
4 |
+
- 库和交互Notebook:[ srush/raspy ](https://github.com/srush/RASPy)
|
5 |
+
|
6 |
+
|
7 |
+
Transformer 模型是AI系统的基础。已经有了数不清的关于" Transformer 如何工作"的核心结构图表。
|
8 |
+
|
9 |
+
<center>
|
10 |
+
|
11 |
+
.svg)
|
12 |
+
|
13 |
+
</center>
|
14 |
+
|
15 |
+
但是这些图表没有提供任何直观的计算该模型的框架表示。当研究者对于 Transformer 如何工作抱有兴趣时,直观的获取他运行的机制变得十分有用。
|
16 |
+
|
17 |
+
[像Transformers一样思考](https://arxiv.org/pdf/2106.06981.pdf)提出 transformer 类计算框架。这个框架直接计算和模仿 Transformer 计算。使用[ RASP ](https://github.com/tech-srl/RASP)编程语言,使每个程序编译成一个特殊的 Transformer 。
|
18 |
+
|
19 |
+
在这篇博客中,我用 python 复现了 RASP 的变体( RASPy )。该语言大致与原始版本相当,但是多了一些我认为很有趣的变化。通过这些语言,作者 Gail Weiss 的工作,提供了一套具有挑战性的有趣且正确的方式可以帮助了解其工作原理。
|
20 |
+
|
21 |
+
```bash
|
22 |
+
!pip install git+https://github.com/srush/RASPy
|
23 |
+
```
|
24 |
+
|
25 |
+
在说起语言本身前,让我们先看一个例子,看看用 Transformers 编码是什么样的。这是一些计算翻转的代码,即反向输入序列。代码本身用两个 Transformer 层应用 attention 和数学计算到达这个结果。
|
26 |
+
|
27 |
+
```python
|
28 |
+
def flip():
|
29 |
+
length = (key(1) == query(1)).value(1)
|
30 |
+
flip = (key(length - indices - 1) == query(indices)).value(tokens)
|
31 |
+
return flip
|
32 |
+
flip()
|
33 |
+
```
|
34 |
+
<center>
|
35 |
+
|
36 |
+
.svg)
|
37 |
+
|
38 |
+
</center>
|
39 |
+
## 表格目录
|
40 |
+
|
41 |
+
- 部分一: Transformers 作为代码
|
42 |
+
- 部分二: 用 Transformers 编写程序
|
43 |
+
|
44 |
+
## Transformers 作为代码
|
45 |
+
|
46 |
+
我们的目标是定义一套计算形式来最小化 Transformers 的表达。我们将通过类比进行此过程,描述其 Transformers 方面的每个语言构造。(正式语言规范请看[全文](https://arxiv.org/pdf/2106.06981.pdf))
|
47 |
+
|
48 |
+
这个语言的核心单元是将一个序列转换成相同长度的另一个序列的序列操作。我后面将其称之为 transforms
|
49 |
+
|
50 |
+
### 输入
|
51 |
+
|
52 |
+
在一个 Transformer 中,基本曾是一个模型的前馈输入。这个输入通常包含原始的token和位置信息。
|
53 |
+
|
54 |
+
<center>
|
55 |
+
|
56 |
+
.svg)
|
57 |
+
|
58 |
+
</center>
|
59 |
+
|
60 |
+
在代码中, tokens 的特征表示最简单的 transform 。他返回经过模型的 tokens 。默认输入序列是" hello "
|
61 |
+
|
62 |
+
```python
|
63 |
+
tokens
|
64 |
+
```
|
65 |
+
|
66 |
+
<center>
|
67 |
+
|
68 |
+
.svg)
|
69 |
+
|
70 |
+
</center>
|
71 |
+
|
72 |
+
如果我们想要改变 transform 里的输入,我们使用输入方法进行传值。
|
73 |
+
|
74 |
+
```python
|
75 |
+
tokens.input([5, 2, 4, 5, 2, 2])
|
76 |
+
```
|
77 |
+
|
78 |
+
<center>
|
79 |
+
|
80 |
+
.svg)
|
81 |
+
|
82 |
+
</center>
|
83 |
+
|
84 |
+
|
85 |
+
作为 Transformers ,我们不能直接接受这些序列的位置。但是为了模拟位置嵌入,我们可以获取位置的索引
|
86 |
+
|
87 |
+
```python
|
88 |
+
indices
|
89 |
+
```
|
90 |
+
|
91 |
+
<center>
|
92 |
+
|
93 |
+
.svg)
|
94 |
+
|
95 |
+
</center>
|
96 |
+
|
97 |
+
```python
|
98 |
+
sop = indices
|
99 |
+
sop.input("goodbye")
|
100 |
+
```
|
101 |
+
|
102 |
+
<center>
|
103 |
+
|
104 |
+
.svg)
|
105 |
+
|
106 |
+
</center>
|
107 |
+
|
108 |
+
|
109 |
+
### 前馈网络
|
110 |
+
|
111 |
+
经过输入层后,我们到达了前馈网络层。在 Transformer 中,这一步可以对于序列的每一个元素独立的应用数学运算。
|
112 |
+
|
113 |
+
<center>
|
114 |
+
|
115 |
+
.svg)
|
116 |
+
|
117 |
+
</center>
|
118 |
+
|
119 |
+
在代码中,我们通过在 transforms 上计算表示这一步。在每一个序列的元素中都会进行独立的数学运算。
|
120 |
+
|
121 |
+
```python
|
122 |
+
tokens == "l"
|
123 |
+
```
|
124 |
+
|
125 |
+
<center>
|
126 |
+
|
127 |
+
.svg)
|
128 |
+
|
129 |
+
</center>
|
130 |
+
|
131 |
+
结果是一个新的 transform 。一旦重构新的输入就会按照重构方式计算
|
132 |
+
|
133 |
+
```python
|
134 |
+
model = tokens * 2 - 1
|
135 |
+
model.input([1, 2, 3, 5, 2])
|
136 |
+
```
|
137 |
+
|
138 |
+
<center>
|
139 |
+
|
140 |
+
.svg)
|
141 |
+
|
142 |
+
</center>
|
143 |
+
|
144 |
+
该运算可以组合多个 Transforms .举个例子,以上述的 token 和 indices 为例,这里可以类别 Transformer 可以跟踪多个片段信息
|
145 |
+
|
146 |
+
```python
|
147 |
+
model = tokens - 5 + indices
|
148 |
+
model.input([1, 2, 3, 5, 2])
|
149 |
+
```
|
150 |
+
|
151 |
+
<center>
|
152 |
+
|
153 |
+
.svg)
|
154 |
+
|
155 |
+
</center>
|
156 |
+
|
157 |
+
|
158 |
+
```python
|
159 |
+
(tokens == "l") | (indices == 1)
|
160 |
+
```
|
161 |
+
|
162 |
+
<center>
|
163 |
+
|
164 |
+
.svg)
|
165 |
+
|
166 |
+
</center>
|
167 |
+
|
168 |
+
|
169 |
+
我们提供了一些辅助函数让写 transforms 变得更简单,举例来说,where 提供了一个类似 if 功能的结构。
|
170 |
+
|
171 |
+
```python
|
172 |
+
where((tokens == "h") | (tokens == "l"), tokens, "q")
|
173 |
+
```
|
174 |
+
|
175 |
+
<center>
|
176 |
+
|
177 |
+
.svg)
|
178 |
+
|
179 |
+
</center>
|
180 |
+
|
181 |
+
`map`使我们可以定义自己的操作,例如一个字符串以 int 转换。(用户应谨慎使用可以使用的简单神经网络计算的操作)
|
182 |
+
|
183 |
+
```python
|
184 |
+
atoi = tokens.map(lambda x: ord(x) - ord('0'))
|
185 |
+
atoi.input("31234")
|
186 |
+
```
|
187 |
+
|
188 |
+
<center>
|
189 |
+
|
190 |
+
.svg)
|
191 |
+
|
192 |
+
</center>
|
193 |
+
|
194 |
+
|
195 |
+
|
196 |
+
当连接了这些 transforms ,可以更容易的编写功能。举例来说,下面是应用了 where 和 atoi 和加2的操作
|
197 |
+
|
198 |
+
```python
|
199 |
+
def atoi(seq=tokens):
|
200 |
+
return seq.map(lambda x: ord(x) - ord('0'))
|
201 |
+
|
202 |
+
op = (atoi(where(tokens == "-", "0", tokens)) + 2)
|
203 |
+
op.input("02-13")
|
204 |
+
```
|
205 |
+
|
206 |
+
<center>
|
207 |
+
|
208 |
+
.svg)
|
209 |
+
|
210 |
+
</center>
|
211 |
+
|
212 |
+
### 注意力筛选器
|
213 |
+
到开始应用注意力机制事情就变得开始有趣起来了。这将允许序列间的不同元素进行信息交换。
|
214 |
+
|
215 |
+
<center>
|
216 |
+
|
217 |
+
.svg)
|
218 |
+
|
219 |
+
</center>
|
220 |
+
|
221 |
+
我们开始定义 Key 和 query 的标记。key 和 Query 可以直接从上面的 transforms 创建。举个例子,如果我们想要定义一个key我们称作key。
|
222 |
+
|
223 |
+
```python
|
224 |
+
key(tokens)
|
225 |
+
```
|
226 |
+
|
227 |
+
<center>
|
228 |
+
|
229 |
+
.svg)
|
230 |
+
|
231 |
+
</center>
|
232 |
+
|
233 |
+
|
234 |
+
对于 query 也一样
|
235 |
+
|
236 |
+
```python
|
237 |
+
query(tokens)
|
238 |
+
```
|
239 |
+
|
240 |
+
<center>
|
241 |
+
|
242 |
+
.svg)
|
243 |
+
|
244 |
+
</center>
|
245 |
+
|
246 |
+
|
247 |
+
标量可以作为 key 或 query 使用。他们会广播到基础序列的长度。
|
248 |
+
|
249 |
+
```python
|
250 |
+
query(1)
|
251 |
+
```
|
252 |
+
|
253 |
+
<center>
|
254 |
+
|
255 |
+
.svg)
|
256 |
+
|
257 |
+
</center>
|
258 |
+
|
259 |
+
|
260 |
+
我们创建了筛选器来应用 key 和 query 之间的操作。这对应于一个二进制矩阵,指示每个 query 要关注哪个 key。与 Transformers 不用,这个注意力矩阵未加入权重。
|
261 |
+
|
262 |
+
```python
|
263 |
+
eq = (key(tokens) == query(tokens))
|
264 |
+
eq
|
265 |
+
```
|
266 |
+
|
267 |
+
<center>
|
268 |
+
|
269 |
+
.svg)
|
270 |
+
|
271 |
+
</center>
|
272 |
+
|
273 |
+
|
274 |
+
一些例子:
|
275 |
+
- 选择器的匹配位置偏移1。
|
276 |
+
|
277 |
+
```python
|
278 |
+
offset = (key(indices) == query(indices - 1))
|
279 |
+
offset
|
280 |
+
```
|
281 |
+
|
282 |
+
<center>
|
283 |
+
|
284 |
+
.svg)
|
285 |
+
|
286 |
+
</center>
|
287 |
+
|
288 |
+
|
289 |
+
- key 早于 query 的选择器。
|
290 |
+
|
291 |
+
```python
|
292 |
+
before = key(indices) < query(indices)
|
293 |
+
before
|
294 |
+
```
|
295 |
+
|
296 |
+
<center>
|
297 |
+
|
298 |
+
.svg)
|
299 |
+
|
300 |
+
</center>
|
301 |
+
|
302 |
+
|
303 |
+
- key 晚于 query 的选择器。
|
304 |
+
|
305 |
+
```python
|
306 |
+
after = key(indices) > query(indices)
|
307 |
+
after
|
308 |
+
```
|
309 |
+
|
310 |
+
<center>
|
311 |
+
|
312 |
+
.svg)
|
313 |
+
|
314 |
+
</center>
|
315 |
+
|
316 |
+
选择器可以通过布尔操作合并。比如,这个选择器befoe和eq做合并,我们通过在矩阵中包含一对键和值来显示这一点。
|
317 |
+
|
318 |
+
```python
|
319 |
+
before & eq
|
320 |
+
```
|
321 |
+
|
322 |
+
<center>
|
323 |
+
|
324 |
+
.svg)
|
325 |
+
|
326 |
+
</center>
|
327 |
+
|
328 |
+
|
329 |
+
## 使用注意力机制
|
330 |
+
给一个注意力选择器,我们可以提供一个序列值做聚合操作。我们通过累加那些选择器选过的真值做聚合。
|
331 |
+
|
332 |
+
(笔记:在原始论文中,他们使用一个平均聚合操作并且展示了一个巧妙的结构,其中平均聚合能够代表总和计算。RASPy 默认情况下使用累加来使其简单化并避免碎片化。实际上,这意味着 raspy 可能低估了将基于平均模型转换为2倍的层数)
|
333 |
+
|
334 |
+
注意聚合操作使我们能够计算直方图之类的功能。
|
335 |
+
|
336 |
+
```python
|
337 |
+
(key(tokens) == query(tokens)).value(1)
|
338 |
+
```
|
339 |
+
|
340 |
+
<center>
|
341 |
+
|
342 |
+
.svg)
|
343 |
+
|
344 |
+
</center>
|
345 |
+
|
346 |
+
视觉上我们遵循图表结构, Query 在左边, Key 在上边, Value 在下面,输出在右边
|
347 |
+
|
348 |
+
<center>
|
349 |
+
|
350 |
+
.svg)
|
351 |
+
|
352 |
+
</center>
|
353 |
+
|
354 |
+
|
355 |
+
一些注意力机制操作甚至不需要用到输入 token 。举例来说,去计算序列长度,我们创建一个" select all "的注意力筛选器并且给他赋值。
|
356 |
+
|
357 |
+
```python
|
358 |
+
length = (key(1) == query(1)).value(1)
|
359 |
+
length = length.name("length")
|
360 |
+
length
|
361 |
+
```
|
362 |
+
|
363 |
+
<center>
|
364 |
+
|
365 |
+
.svg)
|
366 |
+
|
367 |
+
</center>
|
368 |
+
|
369 |
+
|
370 |
+
这里有更多复杂的例子,下面将一步一步展示。(这有点像做采访一样)
|
371 |
+
|
372 |
+
我们想要计算一个序列的相邻值的和。首先我们向前截断。
|
373 |
+
|
374 |
+
```python
|
375 |
+
WINDOW=3
|
376 |
+
s1 = (key(indices) >= query(indices - WINDOW + 1))
|
377 |
+
s1
|
378 |
+
```
|
379 |
+
|
380 |
+
<center>
|
381 |
+
|
382 |
+
.svg)
|
383 |
+
|
384 |
+
</center>
|
385 |
+
|
386 |
+
然后我们向后截断。
|
387 |
+
|
388 |
+
```python
|
389 |
+
s2 = (key(indices) <= query(indices))
|
390 |
+
s2
|
391 |
+
```
|
392 |
+
<center>
|
393 |
+
|
394 |
+
.svg)
|
395 |
+
|
396 |
+
</center>
|
397 |
+
|
398 |
+
两者相交
|
399 |
+
|
400 |
+
```python
|
401 |
+
sel = s1 & s2
|
402 |
+
sel
|
403 |
+
```
|
404 |
+
|
405 |
+
<center>
|
406 |
+
|
407 |
+
.svg)
|
408 |
+
|
409 |
+
</center>
|
410 |
+
|
411 |
+
最终聚合
|
412 |
+
|
413 |
+
```python
|
414 |
+
sum2 = sel.value(tokens)
|
415 |
+
sum2.input([1,3,2,2,2])
|
416 |
+
```
|
417 |
+
|
418 |
+
<center>
|
419 |
+
|
420 |
+
.svg)
|
421 |
+
|
422 |
+
</center>
|
423 |
+
|
424 |
+
这里有个可以计算累计求和的例子,我们这里介绍 transform 的能力来帮助你调试。
|
425 |
+
|
426 |
+
```python
|
427 |
+
def cumsum(seq=tokens):
|
428 |
+
x = (before | (key(indices) == query(indices))).value(seq)
|
429 |
+
return x.name("cumsum")
|
430 |
+
cumsum().input([3, 1, -2, 3, 1])
|
431 |
+
```
|
432 |
+
|
433 |
+
<center>
|
434 |
+
|
435 |
+
.svg)
|
436 |
+
|
437 |
+
</center>
|
438 |
+
|
439 |
+
|
440 |
+
### 层
|
441 |
+
这个语言支持编译更加复杂的 transforms 。他同时通过跟踪每一个运算操作计算层。
|
442 |
+
|
443 |
+
<center>
|
444 |
+
|
445 |
+
.svg)
|
446 |
+
|
447 |
+
</center>
|
448 |
+
|
449 |
+
|
450 |
+
这里有个2层 transform 的例子,第一个对应于计算长度,第二个对应于累积总和。
|
451 |
+
|
452 |
+
```python
|
453 |
+
x = cumsum(length - indices)
|
454 |
+
x.input([3, 2, 3, 5])
|
455 |
+
```
|
456 |
+
|
457 |
+
<center>
|
458 |
+
|
459 |
+
.svg)
|
460 |
+
|
461 |
+
</center>
|
462 |
+
|
463 |
+
|
464 |
+
## 用transformers进行编程
|
465 |
+
|
466 |
+
使用这个函数库,我们可以编写完成一个复杂任务
|
467 |
+
|
468 |
+
Gail Weiss,给我一个极其挑战的问题来打破这个步骤
|
469 |
+
|
470 |
+
我们可以加载一个添加任意长度数字的 Transformer 吗?
|
471 |
+
|
472 |
+
例: 给一个字符串 "19492+23919",我们可以加载正确的输出吗?
|
473 |
+
|
474 |
+
如果你想自己尝试,我们提供了一个[版本](https://colab.research.google.com/github/srush/raspy/blob/main/Blog.ipynb)你可以自己试试。
|
475 |
+
|
476 |
+
### 挑战一 :选择一个给定的索引
|
477 |
+
|
478 |
+
加载一个在索引i处全元素都有值的序列
|
479 |
+
|
480 |
+
```python
|
481 |
+
def index(i, seq=tokens):
|
482 |
+
x = (key(indices) == query(i)).value(seq)
|
483 |
+
return x.name("index")
|
484 |
+
index(1)
|
485 |
+
```
|
486 |
+
|
487 |
+
<center>
|
488 |
+
|
489 |
+
.svg)
|
490 |
+
|
491 |
+
</center>
|
492 |
+
|
493 |
+
|
494 |
+
### 挑战二 :转换
|
495 |
+
|
496 |
+
通过i位置将所有 token 移动到右侧。
|
497 |
+
|
498 |
+
```python
|
499 |
+
def shift(i=1, default="_", seq=tokens):
|
500 |
+
x = (key(indices) == query(indices-i)).value(seq, default)
|
501 |
+
return x.name("shift")
|
502 |
+
shift(2)
|
503 |
+
```
|
504 |
+
|
505 |
+
<center>
|
506 |
+
|
507 |
+
.svg)
|
508 |
+
|
509 |
+
</center>
|
510 |
+
|
511 |
+
### 挑战三 :最小化
|
512 |
+
|
513 |
+
计算序列的最小值。(这一步开始变得困难,我们版本用了2层注意力机制)
|
514 |
+
|
515 |
+
```python
|
516 |
+
def minimum(seq=tokens):
|
517 |
+
sel1 = before & (key(seq) == query(seq))
|
518 |
+
sel2 = key(seq) < query(seq)
|
519 |
+
less = (sel1 | sel2).value(1)
|
520 |
+
x = (key(less) == query(0)).value(seq)
|
521 |
+
return x.name("min")
|
522 |
+
minimum()([5,3,2,5,2])
|
523 |
+
```
|
524 |
+
|
525 |
+
<center>
|
526 |
+
|
527 |
+
.svg)
|
528 |
+
|
529 |
+
</center>
|
530 |
+
|
531 |
+
### 挑战四:第一索引
|
532 |
+
|
533 |
+
计算有 token q 的第一索引(2层)
|
534 |
+
|
535 |
+
```python
|
536 |
+
def first(q, seq=tokens):
|
537 |
+
return minimum(where(seq == q, indices, 99))
|
538 |
+
first("l")
|
539 |
+
```
|
540 |
+
|
541 |
+
<center>
|
542 |
+
|
543 |
+
.svg)
|
544 |
+
|
545 |
+
</center>
|
546 |
+
|
547 |
+
### 挑战五 :右对齐
|
548 |
+
|
549 |
+
右对齐一个填充序列。例: " ralign().inputs(’xyz___‘) =’—xyz’ "(2层)
|
550 |
+
|
551 |
+
```python
|
552 |
+
def ralign(default="-", sop=tokens):
|
553 |
+
c = (key(sop) == query("_")).value(1)
|
554 |
+
x = (key(indices + c) == query(indices)).value(sop, default)
|
555 |
+
return x.name("ralign")
|
556 |
+
ralign()("xyz__")
|
557 |
+
```
|
558 |
+
|
559 |
+
<center>
|
560 |
+
|
561 |
+
.svg)
|
562 |
+
|
563 |
+
</center>
|
564 |
+
|
565 |
+
### 挑战六:分离
|
566 |
+
|
567 |
+
把一个序列在 token "v" 处分离成两部分然后右对齐(2层)
|
568 |
+
|
569 |
+
```python
|
570 |
+
def split(v, i, sop=tokens):
|
571 |
+
|
572 |
+
mid = (key(sop) == query(v)).value(indices)
|
573 |
+
if i == 0:
|
574 |
+
x = ralign("0", where(indices < mid, sop, "_"))
|
575 |
+
return x
|
576 |
+
else:
|
577 |
+
x = where(indices > mid, sop, "0")
|
578 |
+
return x
|
579 |
+
split("+", 1)("xyz+zyr")
|
580 |
+
```
|
581 |
+
|
582 |
+
<center>
|
583 |
+
|
584 |
+
.svg)
|
585 |
+
|
586 |
+
</center>
|
587 |
+
|
588 |
+
```python
|
589 |
+
split("+", 0)("xyz+zyr")
|
590 |
+
```
|
591 |
+
|
592 |
+
<center>
|
593 |
+
|
594 |
+
.svg)
|
595 |
+
|
596 |
+
</center>
|
597 |
+
|
598 |
+
### 挑战七:滑动
|
599 |
+
|
600 |
+
将特殊 token “ <”替换为最接近的“ <”value.(2层)
|
601 |
+
|
602 |
+
```python
|
603 |
+
def slide(match, seq=tokens):
|
604 |
+
x = cumsum(match)
|
605 |
+
y = ((key(x) == query(x + 1)) & (key(match) == query(True))).value(seq)
|
606 |
+
seq = where(match, seq, y)
|
607 |
+
return seq.name("slide")
|
608 |
+
slide(tokens != "<").input("xxxh<<<l")
|
609 |
+
```
|
610 |
+
|
611 |
+
<center>
|
612 |
+
|
613 |
+
.svg)
|
614 |
+
|
615 |
+
</center>
|
616 |
+
|
617 |
+
## 挑战八:增加
|
618 |
+
|
619 |
+
您要执行两个数字的添加。这是步骤。
|
620 |
+
|
621 |
+
```python
|
622 |
+
add().input("683+345")
|
623 |
+
```
|
624 |
+
|
625 |
+
0.分成两部分。转制成整形。加入
|
626 |
+
|
627 |
+
>“683+345” => [0, 0, 0, 9, 12, 8]
|
628 |
+
|
629 |
+
1. 计算携带条款。三种可能性:1个携带,0不携带,<也许有携带。
|
630 |
+
|
631 |
+
>[0, 0, 0, 9, 12, 8] => “00<100”
|
632 |
+
|
633 |
+
2. 滑动进位系数
|
634 |
+
|
635 |
+
> “00<100” => 001100"
|
636 |
+
|
637 |
+
3. 完成加法
|
638 |
+
|
639 |
+
这些都是1行代码。完整的系统是6个注意力机制。(尽管 Gail 说,如果您足够细心则可以在5个中完成!)。
|
640 |
+
|
641 |
+
```python
|
642 |
+
def add(sop=tokens):
|
643 |
+
# 0) Parse and add
|
644 |
+
x = atoi(split("+", 0, sop)) + atoi(split("+", 1, sop))
|
645 |
+
# 1) Check for carries
|
646 |
+
carry = shift(-1, "0", where(x > 9, "1", where(x == 9, "<", "0")))
|
647 |
+
# 2) In parallel, slide carries to their column
|
648 |
+
carries = atoi(slide(carry != "<", carry))
|
649 |
+
# 3) Add in carries.
|
650 |
+
return (x + carries) % 10
|
651 |
+
add()("683+345")
|
652 |
+
```
|
653 |
+
<center>
|
654 |
+
|
655 |
+

|
656 |
+
|
657 |
+
</center>
|
658 |
+
|
659 |
+
```python
|
660 |
+
683 + 345
|
661 |
+
```
|
662 |
+
|
663 |
+
```python
|
664 |
+
1028
|
665 |
+
```
|
666 |
+
非常整洁的东西。如果您对此主题更感兴趣,请务必查看论文:
|
667 |
+
|
668 |
+
像 Transformer 和 RASP 语言一样思考。
|
669 |
+
|
670 |
+
如果您通常对形式语言和神经网络 ( FLaNN ) 的联系感兴趣,或者认识感兴趣的人,那么这里有一个在线社区,到目前为止非常友好且相当活跃!
|
671 |
+
|
672 |
+
在 https://flann.super.site/ 上查看。
|
673 |
+
|
674 |
+
这篇博文,包括库、可视化和博文,可在 https://github.com/srush/raspy 获取。
|
675 |
+
|
676 |
+
<hr>
|
677 |
+
|
678 |
+
>>>> 英文原文:[Thinking Like Transformers](https://srush.github.io/raspy/)
|
679 |
+
>>>> 译者:innovation64 (李洋)
|