A Closer Look at the Perceptron

I’ve been learning machine learning, starting with the perceptron. I thought a model this simple, dating back to 1958, would take me half an hour. Then the questions piled up. Why is the bias necessary? What does a hyperplane look like beyond two dimensions? Is training really just finding a dividing line? How can you plot high-dimensional data in two dimensions? I ended up implementing the perceptron in C++ and trying it on real data.

These are my notes from that process. My previous post, Different Ways to Write Weighted Sums, grew out of the same work.

A brief timeline

Where does the perceptron fit in the history of neural networks?

Year Events
1958 Frank Rosenblatt introduces the perceptron, a simple fully connected single-layer network.
1986 Rumelhart and colleagues popularize backpropagation, making multilayer perceptrons practical.
1989 Yann LeCun and colleagues apply a convolutional neural network to handwritten digit recognition, a precursor to LeNet.
1998 LeNet-5 is published and becomes an early, widely known CNN architecture.

The whole network is about 30 years ahead of CNN. CNN emerged in order to address two problems in the processing of images in a full-connected network: too many parameters and neglect of the spatial structure between pixels. And this one goes back to 1958's perceptron, which is the main character to talk about.

What is a perceptron?

Structure: a weighted sum and a step function

A perceptron is a binary linear classifier. It computes a weighted sum of its inputs, adds a bias, and applies a step function that returns either 0 or 1.

Sensor Structure and Step-in Activation Function

Mathematical expression:

$$\hat{y} = f\Big(\sum_{i=1}^{n} w_i x_i + b\Big), \qquad f(z) = \begin{cases} 1, & z > 0 \\ 0, & z \le 0 \end{cases}$$

Geometry: a dividing line

The rules of the perceptron are: \(\sum_i w_i x_i + b > 0\). On the 2D plane, all points set up by the equal sign form a straight line, dividing the plane into two and a half: one side output and one side output 0.

The most intuitive contrast between the two classic questions of AND and XOR:

Linear symmetry between AND and XOR issues

The error-driven learning rule

The way the sensor learns is simple: If you're wrong, reweight the decision-making boundary in a less wrong direction..

$$w \leftarrow w + \eta (y - \hat{y}) x$$

of which \(\eta\) The study rate (e.g. 0.1)\(y\) It's a real label.\(\hat{y}\) It's a projection. Intuitive understanding:

The Learning Process of Sensors

The above figure is a real simulation: starting with random weights, each round takes the wrong-sharp samples out, the decision-making boundary moves over one step at a time, and the fourth round is correctly classified and closed.

There's a famous behind this. Sensor's down to the theorem.(Rosenblatt self-proven): This algorithm guarantees a constriction within a limited number of steps, as long as the data is linear. In turn, when the data is linear, it's forever convulsing -- that's the case with XOR. This "shock" is not a theoretical threat, and the last test of this paper will see it.

Why do we need a bias?

It's my first place to really get stuck. \(b\) Looks like a irrelevant constant, but its mathematical nature is: Give the decision-making boundary a "freedom level of transfer.".

The function of bias.

It can be understood at three levels:

**Geometry: There is no bias, and the border must pass its roots. ** Decision-making boundaries are satisfaction. \(\sum_i w_i x_i + b = 0\) All points. If Forced \(b=0\), the equation becomes \(w \cdot x = 0\)It's the original. \(x=0\) Natural satisfaction -- borders crucified to the point of origin, with only the freedom of "turning." This is the dilemma of the left half of the top figure: the data set can be separated by an oblique line, but not by all the straight lines of the point.

**Algebra level:\(b\) It's the constant of the straight equation. ** The standard two-dimensional line is: \(ax + by + c = 0\),\(c\) Decision of location,\(a\)、\(b\) Decide on the direction. \(w\) Direction,\(b\) Mind your place, one can't be. That's enough. \(b\)The ability to express itself is simply cut to pieces.

**A unified perspective:\(b\) is the weight of the "Virtual Input of Constant One". ** It's a very elegant understanding: to expand the input vector by a dimension.

$$\tilde{x} = (x_1, \dots, x_n, 1), \qquad \tilde{w} = (w_1, \dots, w_n, b)$$

Well.. \(\tilde{w} \cdot \tilde{x} = \sum_i w_i x_i + b\)And the sensor is back in the form of a "pure weighting sum" that doesn't require special treatment. This is how it works in a lot of nervous network frameworks, and this is the technique used in the C++ code at the end.

And you can also use the intuitive example: imagine the sensor as the admission system, without prejudice to the "weighted total must be greater than 0" -- the threshold of 0 is dead. The bias allows you to adjust the intake line independently of the weights of the sections.

One sentence: The bias is not to add flowers, but to be part of a linear model's ability to fully express itself..

Hyperplanes in higher dimensions

Here is a concept that deserves clarification: is the straight line a hyperplane in two-dimensional space? The answer is yes, but only on an exceptional basis.

The definition of super-platform is:\(n\) In the space, the dimensions are \(n-1\) Linear Subspace. "hyper" does not mean that it's higher than the plane, but that it's just a dimension lower than it is in space.

The shape of the hyperplane in 2 peacekeeping and 3-dimensional space

Space dimension What's the hyperplane? Hyper-Stand Dimensions
2-D Line 1
3-D Plane 2
784 D (28 x 28 pixels of grey scale) Unimaginable flat structure. 783

No matter how many dimensions, the hyperplatform has two constant properties: one dimension less than space; It happens to cut into two and a half spaces. The decision-making boundary of the sensor is satisfied at any dimension, so it's always a hyperplane -- you can see it as a line in two dimensions, it exists in 784 dimensions but it can't be painted.

Training finds a separating hyperplane. Is it the best one?

And here we can conclude: the sensor training is looking for the split hyperplane. That's what I said. In a big direction, but with two more limits..

First, the sensor is not "calculating" super-platforms, but it's an iterative test coming close: every update. \(w \leftarrow w + \eta(y-\hat{y})x\)Geometrical equivalents rotate and peacefully move borders, moving them in the direction of reducing errors.

Secondly, and more crucial: The insatiable theorem is only a guarantee of a "a certain" hyperplane that can be perfectly classified, not the "best.". It may have passed the edge of the sample and be very sensitive to the noise.

The perceptron compared to the SVM's super flat face.

This is the watershed between the sensor and the SVM:

Sensor. SVM
Objective I found it. One. It'll be fine if it's separated. Look. Maximum interval That one.
Nature of resolution It's not the only one that can solve it. The best, the only.
For new samples It's probably on the edge of the old sample, so it's easy to roll over. As far away as possible from both of them.

So then SVM solved the problem of finding the best hyperplane, the multi-layer neural network solved the problem of "data is not linear" -- the two panels of the sensor, each of which created a technical route.

What does the weighted sum measure?

This is the second place that I'm really stuck in: when I look at the map of the sensor, I think it's a "magnification" of the Gavid to a 2-dimensional plane, but I don't know what the mathematical meaning is. Opening up is actually two issues.

Question one:\(z = \sum_i w_i x_i + b\) What's the result? ** It's a scale, but it's a big geometry. It's... Zoomed version of "Significant Distance"**:

I'll put it in the same way:\(z\) It's a vector. \(x\) Normal \(w\) Heading up. Project length( times) \(\|w\|\) Plus. \(b\)).

Geometric meaning of weighted sum and high-dimensional slices

Question two: 784-D data on which two-dimensional maps are drawn? ** The answer is, we don't have a map, just... Slice**.

Take a positive picture of a cube, two-dimensional, but three-dimensional, you just can't see the depth axis. Photo data is the same -- a 28x28 greyscale map, and the pixels are even with a 784-dimensional vector; A 2D sprawl only shows it \(x_i\)、\(x_j\) Two coordinates, the rest of 782 dimensions still exist, just without drawing.

And it's not a close metaphor, it's a precise mathematical fact: after fixing another 782 variables,\(w_i x_i + w_j x_j + (\text{常数}) = 0\) Yes. \((x_i, x_j)\) There's a straight line on the plane. 783 The cross line between the two-dimensional coordinates is necessarily a straight line..

I'm not sure I'm going to be able to do this.\(\sum_i w_i x_i\) There's a lot of equivalents in the formula itself.PreviousLee, don't repeat it here.

Can we observe it in one dimension?

Following the cut: Since 2D is just a slice, can you use 1D to observe the split? The answer is yes, but a D-observation can throw away information and even be misleading.

Put the border equation \(w_1 x_1 + w_2 x_2 + b = 0\) Lee. \(x_2\) Fixed, deciphering is a 1D threshold point:

$$x_1 = -\frac{w_2 x_2 + b}{w_1}$$

The problem is: This threshold depends on the dimension you're hiding..

1D slice compared to 2D observation

The top left is the complete 2D view, the middle and right is the line. \(x_2 = 1.0\) and \(x_2 = 3.2\) Cut out two 1D numeric axes -- the same hyperplane, cut out the threshold of one in 3.1, one in 1.0, completely different. And in the middle of that, two red dots and blue squares projection close to the threshold, you can't judge their true spatial relationship on 1D.

For example, 1D observation image looks at the room through a door sew, 2D observation image looks through a window -- the window is still a projection, but can see how at least two dimensions decide on the classification together.

Of course, 1D is not useless: to analyse the contribution of individual characteristics, to understand the decision tree (the decision tree is intrinsically a 1D threshold split), to explain to non-technicals that "this indicator exceeds a certain value, and the 1D threshold is the most intuitive. Just be clear to me: It's just a shadow on the direction you've chosen for the slices..

Implementing a perceptron in C++

The theory is over, let's write it down. The sensor is simple enough to the core code.

// 单层感知机:ŷ = step(w·x + b)
struct Perceptron {
    std::vector<double> w;
    double b = 0.0;
    double lr = 0.1;                 // 学习率 η

    explicit Perceptron(size_t n) : w(n, 0.0) {}

    int predict(const std::vector<double>& x) const {
        double z = b;
        for (size_t i = 0; i < x.size(); ++i) z += w[i] * x[i];
        return z > 0 ? 1 : 0;
    }

    // 错误驱动:预测错了才更新 w ← w + η(y-ŷ)x
    void update(const std::vector<double>& x, int y) {
        int d = y - predict(x);
        if (d == 0) return;
        for (size_t i = 0; i < w.size(); ++i) w[i] += lr * d * x[i];
        b += lr * d;
    }
};

predict It's from before. \(\hat{y} = f(\sum_i w_i x_i + b)\),update It's the rules of learning. \(w \leftarrow w + \eta(y-\hat{y})x\)There's nothing else.

The AND gate converges in six epochs

Feed it with 4 samples of the AND problem. Output:

AND: epoch 6 收敛, w=(0.20, 0.10), b=-0.20
  (0, 0) -> 0 (期望 0)
  (0, 1) -> 0 (期望 0)
  (1, 0) -> 0 (期望 0)
  (1, 1) -> 1 (期望 1)

Number six, the line to be learned is \(0.2x_1 + 0.1x_2 - 0.2 = 0\)that is, \(2x_1 + x_2 = 2\)It's perfect. \((1,1)\) Separated from the other three points. Just as the insinuation theorem says: linear, within a limited number of steps.

Real data: watching the weights oscillate

Take it and run a real task: a classic handwritten digital picture data set, 60,000 training + 10,000 test graphs, 28 x 28 greyscales each, 10 categories. The file format is simple -- 4 byte large-end magic + dimension information + original pixel byte, which can be parsed by more than 10 lines of code. The data can be downloaded from the official mirror:storage.googleapis.com/cvdf-datasets/mnist.

Sensor is a second-classifier. one-vs-rest: Train 10 sensors, no. \(k\) The answer is "Isn't it a number?" \(k\)"; Increase in projection \(z\) The biggest one. pixels are unified to \([0,1]\), enter a further one-dimensional constant 1 at the end, and you add the weight to the weight of the bias -- that's the technique that's said in that section. Actual output (learning rate 0.1, update by sample, 10 rounds):

epoch  1: 测试集准确率 85.95%
epoch  2: 测试集准确率 85.88%
epoch  3: 测试集准确率 86.05%
epoch  4: 测试集准确率 87.42%
epoch  5: 测试集准确率 84.95%
epoch  6: 测试集准确率 85.07%
epoch  7: 测试集准确率 89.03%
epoch  8: 测试集准确率 87.95%
epoch  9: 测试集准确率 88.52%
epoch 10: 测试集准确率 87.52%

Note the shape of this curve: It's not stable, it's not subdued, it's just between 85% and 89%.- Round 4 87.42 per cent, while Round 5 fell to 84.95 per cent. This is the other side of the condensing theorem: the pixel space for hand-written numbers is not linear, there is no super-platform with zero error, and the sensor can only be fixed for ever. The concussion of the theoretical prophecy is actually seen.

What do two hidden layers buy us?

I've been running the same data from a three-storey, full-link network before, measuring test accuracy. 97.35% And the accuracy rate has steadily increased with the training. Put it together and watch:

Single-level sensor X10 Three-tier, fully connected.
Structure 784 ⁇ 10 (linearly) 784 → 256 → 128 → 10
Decision-making boundaries 10 Super plane A partial "fraging face."
Test set accuracy rate 85%-89%. The shock didn't stop. 97.35%, steady contraction
Mode of training Error-driven update rule Inverse Dissemination + SGD

And the difference between this is about 10 points, and that's what the two layers of the hidden layer buys: "Only straight lines" to "Fig-flip." The complete IDX resolution and data loading code is not attached (reading magic, reading dimensions, bytes, all manual work), and the core logic is all in the 30 lines above.

Conclusion

A circle of studies, perceptrons, the old model of 1958, more than I thought, chewing:

Without the failure of a sensor, there may not be any subsequent in-depth learning — that is not an exaggeration. And my own harvest is that when you learn this kind of model, I'm asking each symbol in geometric terms. What's the bias, the mark? \(z\) What is it, how it's made? Write the code again, it's much more useful than the back formula.

This is a collection of learning notes, which I do not necessarily understand correctly, and where there is a mistake, the lecture is welcome.

References