Neural Network Pattern Recognition


Neural Network Pattern Recognition

Corey
Today we're building an intuition for how neural networks learn patterns. Imagine an extremely simple camera with just four pixels—not four megapixels, literally four—and each pixel can be black, white, or somewhere gray.

Hope
And suppose the camera needs to classify each image as solid white, solid dark, a vertical line, a diagonal line, or a horizontal line. That sounds simple, but straightforward rules based only on which individual pixels are bright can become surprisingly difficult.

Corey
Right. Two images might both represent horizontal lines even though their individual bright and dark pixels are arranged differently. So instead of writing a separate rule for every possible pixel arrangement, we give the network numerical inputs and let it learn useful combinations.

Hope
The four pixels become four input neurons. We encode brightness numerically: a completely white pixel is +1, a completely black pixel is -1, and gray is 0. The resulting list of numbers is called the input vector, or input array.

Corey
A helpful idea here is the receptive field of a neuron. Its receptive field is the set of inputs that can make that neuron's value as high as possible. For an input neuron, this is easy: its receptive field is just its associated pixel, and that neuron is highest when that pixel is completely white.

Hope
The other pixels do not matter to that input neuron. Whether they are white or black has no effect, because the neuron is connected to only its one assigned pixel at this stage.

Corey
Now we create a new neuron. First, it adds up the values coming from the input neurons. For example, the unweighted sum in one case might be 0.50. But the important next step is that each connection has a weight.

Hope
A weight multiplies the value traveling along a connection. It can be +1, -1, zero, or any value in between. A weight of -1 reverses the input, a weight of zero effectively ignores it, and a positive weight preserves its sign while scaling it.

Corey
So the neuron calculates a weighted sum: multiply each input by its connection weight, then add all those products. In diagrams, positive weights can be shown as white links, negative weights as black links, and thicker lines can represent larger magnitudes.

Hope
After calculating that weighted sum, the neuron applies an activation function. One option is the sigmoid, or squashing, function. It has an S shape: as the input increases, the output increases too, but more slowly.

Corey
For instance, an input of 0.5 produces an output slightly below 0.5 in the example. As the input becomes very large, the output approaches but stays below +1. For very negative inputs, it approaches but stays above -1. This keeps the neuron's value bounded between -1 and +1, which helps keep computations stable.

Hope
So one neuron can be summarized as two operations: take a weighted sum of its inputs, then squash the result. In the example, that process produces an output of 0.746.

Corey
And a neural network uses many of these neurons, not just one. A layer might contain four neurons, or 400, or even 4 million. In the simplified diagrams, we'll show only weights of +1, -1, or zero, but in an actual network every neuron can be connected to every neuron in the previous layer with a weight somewhere between -1 and +1.

Hope
The receptive fields become more interesting in the first hidden layer. A neuron might combine two input pixels, so its receptive field is now a particular pair of pixels rather than one pixel.

Corey
For example, a neuron could connect positively to the upper-left and lower-left pixels. It would be most active when both of those pixels are white. Another neuron could connect positively to the upper-right pixel but negatively to the lower-right pixel, making it most active when the upper-right pixel is white and the lower-right pixel is black.

Hope
That is the key progression: later neurons respond not just to individual pixels, but to arrangements of pixels. Because the first layer produces values that resemble the original input values, we can use it as the input to another layer.

Corey
We repeat the same process layer by layer. Each layer receives the previous layer's outputs, forms weighted sums, and applies an activation function. As we add layers, receptive fields become more complex and can eventually cover all four pixels in particular arrangements of black and white.

Hope
In the simplified picture, many connections are omitted because their weights are assumed to be zero. In a typical fully connected network, though, every neuron in one layer is connected to every neuron in the previous layer, and the weights generally are not literally absent.

Corey
The network does not have to use sigmoid at every layer. Another common neuron type uses a rectified linear unit, or ReLU. It still begins with a weighted sum, but instead of squashing it, it keeps positive values and changes negative values to zero.

Hope
Mathematically, ReLU is simple: if the input is positive, the output is that input; if the input is negative, the output is zero. It is easy to compute and has useful stability properties in neural networks.

Corey
When positive and negative weights feed into ReLU neurons, the network can represent receptive fields and their opposites. So one neuron may respond to a certain pixel pattern, while another can respond to the inverse pattern.

Hope
Eventually, after creating as many hidden layers and neurons as needed, we add an output layer. In our example it has four outputs: solid, vertical, diagonal, and horizontal.

Corey
Let's walk through the example from the material. Suppose the input image has dark pixels on top and white pixels on the bottom. The input neurons receive the corresponding negative and positive values, and those values move into the first layer.

Hope
Some first-layer neurons combine a dark value and a light value and get zero. Another combines a dark pixel with a light pixel using a negative weight, producing -1. That makes sense if its receptive field expects the opposite arrangement: an upper-left white pixel and a lower-left black pixel.

Corey
The same kind of combinations continue in the next layers. Zeros can combine to produce zeros, and negative values can combine with negative weights to produce positive values. A neuron whose receptive field is the inverse of the actual input can produce a negative response.

Hope
When the signal reaches a ReLU, any negative value becomes zero. A negative value multiplied by a negative weight becomes positive, though, so that signal can survive the rectification. In the example, by the time the outputs are reached, the horizontal output is positive and the others are zero.

Corey
The network therefore classifies the image as a horizontal line. That example is unusually clean, though. Real neural networks usually do not produce perfect zeros and ones.

Hope
For each input, we define the truth, sometimes called the target answer. For a horizontal image, the target could be zero for solid, vertical, and diagonal, and one for horizontal. The network's actual outputs may differ from those target values.

Corey
The error measures the magnitude of the difference between the truth and the network's answer. We can combine the errors for the outputs to get a total error. Training means adjusting the weights so that this error becomes as small as possible.

Hope
The basic training loop is straightforward in principle: put in an image, calculate the network's output and error, determine how each weight should change, adjust the weights in the direction that reduces error, and repeat with more inputs.

Corey
The difficulty is computational. If we changed one weight and recalculated the entire network from the beginning to see what happened to the error, then repeated that for every weight, training a large network would take far too long.

Hope
You can picture the error as a landscape with one dimension for every weight. Training is like trying to move downhill toward a low point. With many weights, that is a very high-dimensional valley, and checking every possible adjustment directly becomes prohibitively expensive.

Corey
The important insight is that, if the network is designed appropriately, we can calculate the slope directly. That slope tells us how much the error changes when a particular weight changes, so it tells us both the direction and the relative size of the adjustment needed.

Hope
This is written as the change in error divided by the change in weight, abbreviated DE over DW. If increasing a weight by one causes the error to decrease by two, the slope is -2. The negative sign tells us that increasing the weight moves the error downward in that example.

Corey
Calculating these slopes uses derivatives, which are the mathematical way to describe rates of change. For example, if the error function is the square of the weight, its derivative is two times the weight. At a weight of -1, the slope is -2.

Hope
The second major insight is chaining. Consider a tiny network where an input X connects through weight W1 to a value Y, and Y connects through weight W2 to an error E. We have Y equal to X times W1, and E equal to Y times W2.

Corey
To find how E changes when W1 changes, we can calculate the slope of each small step and multiply them. The derivative of Y with respect to W1 is X, and the derivative of E with respect to Y is W2, so the full slope is X times W2.

Hope
That multiplication of local derivatives is the chain rule. In a deep network, we calculate the derivative for each small operation on the path back from the error to the weight, then multiply those derivatives together. This is much cheaper than recalculating the entire network separately for every weight.

Corey
Back propagation applies this idea to each operation in the network. For a weighted connection from A to B, if B is the weighted version of A, then the derivative of B with respect to A is the weight. That gives us the local slope needed to move backward through the connection.

Hope
For a sum, the derivative with respect to any one input is 1. If you increase one element of a sum by one, the total sum also increases by one. So the gradient passes backward through a summation with a one-to-one slope.

Corey
For a sigmoid, we can calculate its derivative analytically, and the function has a convenient property: its derivative can be found by multiplying the sigmoid's output by one minus that output.

Hope
For ReLU, the derivative is piecewise. It is 1 when the input is positive, because the output changes along with the input, and it is 0 when the input is negative, because the output is already fixed at zero.

Corey
Together, those local rules let us back-propagate the effect of changing any weight on the error for a given input. The network can therefore calculate all the necessary weight adjustments efficiently.

Hope
Training begins with a fully connected network whose weights are assigned random values. We provide an input whose correct answer is known, calculate the error, use back propagation to adjust every weight slightly in the direction that reduces that error, and then repeat with another input.

Corey
After thousands or even millions of repetitions, the weights can settle into a low-error region of that many-dimensional landscape. The resulting network can perform close to the correct answer on most images.

Hope
Sometimes the learned weights produce receptive fields that are easy to interpret and a relatively sparse representation, where many weights are small or close to zero. But that is not guaranteed. What training aims for is a representation that adjusts the weights to get as close as possible to the correct answers across the inputs.

Corey
So the central story is: represent inputs numerically, combine them with weighted connections, apply activation functions, stack layers to build more complex receptive fields, compare outputs with the truth, and use gradients and back propagation to reduce the error.

Hope
That is a basic introduction rather than a complete recipe for building a neural network. Further topics include biased neurons and dropout, and the material points learners toward Andrej Karpathy's teaching resources and the article titled 'The Black Magic of Deep Learning' for practical advice.

Ask anything
We've read this whole study set, so questions about it get the best answers.