初中级Lower intermediate在线实验室Online lab

感知机:一个会学习的神经元

The Perceptron: A Neuron That Learns

给每个格子一个分数,答错了就改分数。从 3 × 3 到 20 × 20,Python 代码现场就能改。

Every square gets points, and wrong answers change the points. From 3 × 3 to 20 × 20, with Python code you can edit live.

1958 年,Frank Rosenblatt 发表了感知机(Perceptron):一个能从例子里自己学习的“神经元”。它就是初级第四课的方格图,加上第一课 Be a Neuron 的加分和门槛。

In 1958 Frank Rosenblatt published the perceptron, a “neuron” that learns from examples by itself. It is the pixel grid from “How Does a Computer See?” plus the points and threshold from Be a Neuron.

这一课要明白的三件事Three things to understand

  • 电脑“认图”就是给每个格子一个分数,把黑格子的分数加起来,看有没有超过门槛。To recognize a picture, the computer gives every square some points, adds up the points of the black squares, and checks whether the total passes a threshold.
  • 它不靠人写规则,是从例子里学:答错了,就改黑格子的分数。Nobody writes the rules. It learns from examples: when it is wrong, it changes the points of the black squares.
  • 它有做不到的事:横线可以在任何一行时,它永远学不会。这要等多层网络来解决。There are things it can’t do: if a line can be in any row, it never learns. That needs a network with layers.

课堂顺序Class plan

  • 3 × 3 中间的横线或竖线:2 轮学会。每一步先让全班猜灯亮不亮,再手算总分。3 × 3, a flat or standing line in the middle: learned in 2 rounds. Before each step the class guesses whether the light turns on, then adds up the points by hand.
  • 3 × 3 任意位置的线:永远学不会,故意留的悬念。3 × 3, lines anywhere: never learned. A cliffhanger on purpose.
  • 5 × 5 的 7 或 1:4 轮学会,孩子上来画自己的 7 去考它。5 × 5, 7 or 1: learned in 4 rounds. Kids come up and draw their own 7 to test it.
  • 8 × 8 的心或猫:就是学习单上的心和猫,2 轮学会。8 × 8, heart or cat: the heart and cat from the worksheet, learned in 2 rounds.
  • 20 × 20 的 7 或 1:和 1960 年 Mark I 感知机的“眼睛”一样大,3 轮学会,代码和 3 × 3 一模一样。20 × 20, 7 or 1: the size of the Mark I Perceptron’s eye (1960). Learned in 3 rounds with exactly the same code as 3 × 3.

除了画图训练,还能怎么玩More ways to play

  • 先猜再按:每一步之前全班投票,灯会亮吗?分数会怎么变?Guess first: before each step the class votes. Will the light turn on? How will the points change?
  • 你来当老师:3 × 3 先把分数清零,让孩子点格子自己给分,调出能分清横线和竖线的分数。You be the teacher: on 3 × 3, reset the points and let kids click the squares to set points that tell flat lines from standing ones.
  • 骗过它:训练好以后,画一个它认错的 7,比如挪个位置、画得很胖。它只认位置。Fool it: after training, draw a 7 it gets wrong, say moved over or very fat. It only knows positions.
  • 坏老师:只教 7 不教 1,它会把 1 也说成 7。训练数据决定它学到什么。Bad teacher: teach only 7s and no 1s, and it calls the 1s 7 as well. The training data decides what it learns.
  • 读它的心:20 × 20 训练完,只看分数图,猜它学到了什么(能看出一个蓝色的 7)。Read its mind: after training 20 × 20, look only at the points and guess what it learned (you can see a blue 7).

改代码能看到什么What changing the code shows

  • 门槛 total > 0:门槛就是“要多少证据才说是”。“图是空的吗”只有门槛小于 0 才学得会;“任意位置的线”什么门槛都学不会。The threshold total > 0 is how much evidence it needs before saying yes. “Is the picture empty?” is only learnable with a threshold below 0; “lines anywhere” is never learnable, whatever the threshold.
  • change = 1 改成 2:结果一模一样,只是分数全都翻倍。要紧的是分数之间的比例。Change change = 1 to 2: exactly the same answers, just every point doubled. What matters is the ratio between points.
  • 删掉 if guess == answer: 和 return:答对也改分数,7 或 1 就永远停在 7/8。只在答错时改,是整个规则最关键的一行。Delete if guess == answer: and return: it changes points even when right, and 7-or-1 gets stuck at 7 of 8 forever. Learning only from mistakes is the key line.
  • 加 print(total):总分离门槛有多远,就是它有多大把握。Add print(total): how far the total is from the threshold is how sure it is.

原理How it works

  • 图和分数对应位置相乘再加起来,数学上叫点积。超过门槛就说“是”。Multiply each square by its points and add them up: in math this is a dot product. Above the threshold means yes.
  • 每张图是空间里的一个点,感知机用一刀把空间切成两半。两类图能一刀切开,它就一定学得会(Novikoff,1962)。Each picture is a point in space, and the perceptron cuts space in two with one straight cut. If the two kinds of pictures can be cut apart, it always learns (Novikoff, 1962).
  • 三条横线合起来、三条竖线合起来,都正好用到每个格子一次,总分一样。所以不可能横线都“是”、竖线都“不是”。Three flat lines together, and three standing lines together, each use every square exactly once, so they have the same total. So the flat lines can’t all be yes while the standing lines are all no.

下载Downloads

Python 代码(perceptron.py)Python code (perceptron.py)改代码的实验(experiments.py)Code-change experiments (experiments.py)

视频与文章Videos and reading

接着学Keep going