Skip to content
Blog

Mathematics of the perceptron neuron model

One of the hottest topics of artificial intelligence and machine learning are neural networks. These are computational models based on the brain’s structure, whose most significant property is their ability to learn from data.

Neural networks are usually arranged as sequences of layers. In turn, layers are made up of individual neurons. Therefore, neurons are the basic information processing units in neural networks.

The most widely used neuron model is the perceptron. This is the neuron model behind perceptron layers (also called dense layers), which are present in the majority of neural networks.

This post takes the perceptron apart one piece at a time. It starts from the equation that defines it, follows a complete numerical example with four inputs, and ends with the step from a single neuron to a dense layer:

  1. The perceptron in one equation.
  2. Computing a neuron by hand.
  3. What the weights and the bias do.
  4. Choosing the activation function.
  5. From one perceptron to a dense layer.
  6. Conclusions.

The perceptron in one equation

A perceptron turns a list of numbers into a single number, and it does so in two stages. The first stage is linear: it multiplies every input by a weight, adds the results and adds a constant. The second stage is nonlinear: it passes that sum through a fixed function. The whole computation fits in one line,

$$ y = a\left(b + \sum_{i=1}^{n} w_i\, x_i\right). $$

The following table summarizes the symbols in this formula.

SymbolNameMeaning
\( \mathbf{x}=(x_1,\ldots,x_n) \)InputsIn the first layer of a model, the scaled values of the input variables; deeper in the network, the outputs of the previous layer.
\( \mathbf{w}=(w_1,\ldots,w_n) \)WeightsOne for each input; they set how much, and in which direction, each input counts.
\( b \)BiasA constant term that does not depend on the inputs.
\( c = b + \sum_{i} w_i x_i \)CombinationThe result of the linear stage, also known as net input or pre-activation.
\( a(\cdot) \)Activation functionThe nonlinear function applied to the combination.
\( y = a(c) \)OutputThe value that the neuron passes on to the next layer or returns as a prediction.

Perceptron diagram: each input is multiplied by its weight, the products and the bias are added into the combination c, and the activation function turns c into the output y

Only the weights and the bias change during training. A perceptron with \( n \) inputs therefore has \( n+1 \) trainable parameters. The activation function, on the other hand, is part of the design of the network and stays fixed.

Computing a neuron by hand

A numerical example makes the equation concrete. Consider a neuron in the first hidden layer of a model with four input variables. The scaling layer has already transformed the raw data, and the neuron receives

$$ \mathbf{x} = (0.6,\ {-1.3},\ 0.5,\ 0.9). $$

Its parameters, obtained by training, are a bias \( b = 0.35 \) and the weights \( \mathbf{w} = (1.5,\ 0.4,\ {-0.7},\ {-1.2}) \), five parameters in total. The neuron uses the hyperbolic tangent as activation function.

Worked example: four inputs with their weights, the bias 0.35, the combination -0.70 and the output tanh(-0.70) = -0.604

Step 1: the combination. Each input is multiplied by its weight:

InputValue \( x_i \)Weight \( w_i \)Product \( w_i x_i \)
10.61.50.90
2−1.30.4−0.52
30.5−0.7−0.35
40.9−1.2−1.08
Sum−1.05

Adding the bias to the sum of the products gives

$$ c = 0.35 + (-1.05) = -0.70. $$

Step 2: the activation. The hyperbolic tangent of the combination is the output of the neuron:

$$ y = \tanh(-0.70) \approx -0.604. $$

That is all a perceptron does in a forward pass: four multiplications, four additions and one evaluation of the activation function. A dense layer repeats this for each of its neurons, and a network repeats it layer after layer.

What the weights and the bias do

A weight tells how sensitive the combination is to its input. If \( x_i \) changes by an amount \( \Delta \) and the other inputs stay the same, the combination changes by exactly \( w_i \Delta \). The sign of the weight gives the direction of the effect and its magnitude gives the strength; a weight near zero means that the neuron practically ignores that input.

Back to the example: if the second input rises from −1.3 to 1.3, the combination grows by \( 0.4 \times 2.6 = 1.04 \), from −0.70 to 0.34, and the output changes from −0.604 to \( \tanh(0.34) \approx 0.327 \). The same change in the third input would move the combination by \( -0.7 \times 2.6 = -1.82 \): in the opposite direction and 1.75 times as far.

The bias is the value that the combination takes when all the inputs are zero. It lets the neuron give a nonzero response to a zero input, and it shifts the region of the input space where the activation function moves from low to high values. A common notational shortcut treats it as one more weight: add a constant input \( x_0 = 1 \) with weight \( w_0 = b \), and the combination becomes the single sum \( c = \sum_{i=0}^{n} w_i x_i \).

There is also a geometric way to see this. The inputs for which \( c = 0 \) form a hyperplane in the input space: a line when there are two inputs, a plane when there are three. The weight vector is perpendicular to that hyperplane, and its distance from the origin is \( |b| / \lVert \mathbf{w} \rVert \). The combination is positive on one side and negative on the other, so a single perceptron can only separate two classes if a hyperplane can split them. Combining many perceptrons removes that limitation.

Choosing the activation function

The combination is a linear function of the inputs. The activation function is where nonlinearity enters the model: without it, any stack of layers would reduce to a single linear transformation, however deep it was. Which function to use depends on the position of the neuron in the network and on what its output has to represent. Neural Designer supports several of them; these four cover most dense layers:

To compare them, each subsection applies the function to the combination of our example, \( c = -0.70 \).

Linear activation

The linear activation returns the combination unchanged:

$$ a(c) = c. $$

Linear activation function

Its output can take any real value, so it places no bounds on the prediction. That makes it the standard choice for the output layer of approximation models, whose targets are continuous quantities such as a temperature or a price. In the example, the output would be −0.70.

Logistic activation

The logistic function, also called the sigmoid, maps any real number into the interval between 0 and 1:

$$ a(c) = \frac{1}{1+e^{-c}}. $$

Logistic activation function

Large negative combinations give outputs close to 0, large positive ones give outputs close to 1, and \( c = 0 \) gives exactly 0.5. Because the output always lies strictly between 0 and 1, it can be read as a probability, and Neural Designer uses this function in the output layer of binary classification models. In the example, \( a(-0.70) = 1/(1+e^{0.70}) \approx 0.332 \).

Hyperbolic tangent activation

The hyperbolic tangent has the same S shape as the logistic function, but it is symmetric about the origin and its values lie between −1 and 1:

$$ a(c) = \tanh(c) = \frac{e^{c}-e^{-c}}{e^{c}+e^{-c}}. $$

Hyperbolic tangent activation function

The two functions are closely related, since \( \tanh(c) = 2\,\sigma(2c) – 1 \), where \( \sigma \) denotes the logistic function. Close to \( c = 0 \), the hyperbolic tangent is almost the identity; for large \( |c| \), it flattens out towards −1 or 1. Its outputs are centered on zero, which usually makes the training of the next layer easier, and it is a frequent choice for the hidden layers of approximation and classification models. In the example, \( \tanh(-0.70) \approx -0.604 \).

Rectified linear (ReLU) activation

The rectified linear unit sets negative combinations to zero and lets zero or positive ones pass unchanged:

$$ a(c) = \max(0,\ c). $$

rectified linear activation function

Evaluating it only takes a comparison, and its slope is exactly 1 for every positive combination, so the gradients used in training do not shrink as they travel back through many active neurons. For these reasons, it is the usual hidden activation in deep and convolutional networks. In the example, the combination is negative and the output is 0: for this input the neuron is inactive and, during training, it would pass no gradient to its weights.

The following table summarizes the four functions and their outputs for the combination of the example.

ActivationRangeOutput for \( c=-0.70 \)Usual place in the network
Linear\( (-\infty,\ \infty) \)−0.700Output layer of approximation and forecasting models
Logistic\( (0,\ 1) \)0.332Output layer of binary classification models
Hyperbolic tangent\( (-1,\ 1) \)−0.604Hidden layers
ReLU\( [0,\ \infty) \)0Hidden layers, especially in deep networks

From one perceptron to a dense layer

A single perceptron produces one number and can only draw one hyperplane through the input space. Useful models need many of them working together. A dense layer, also called a perceptron layer, places \( m \) perceptrons side by side: all of them read the same \( n \) inputs, and each one has its own weights and bias.

If the weight vectors are stacked as the rows of an \( m \times n \) matrix \( \mathbf{W} \) and the biases are collected in a vector \( \mathbf{b} \), the whole layer is computed at once as

$$ \mathbf{y} = a\left(\mathbf{b} + \mathbf{W}\mathbf{x}\right), $$

where the activation function acts on each of the \( m \) combinations separately. Such a layer has \( m(n+1) \) parameters: if the neuron of our example were one of three neurons in a dense layer with four inputs, the layer would have \( 3 \times (4+1) = 15 \) parameters.

Dense layers are chained: what one layer produces is what the next one receives. With nonlinear activations in the hidden layers, a chain of such layers can represent very complex relationships between inputs and targets, and training adjusts all the weights and biases together to minimize the loss index. For classification with more than two classes, the output layer uses the softmax function instead of an element-wise activation, so that the outputs are probabilities that add up to 1. The Neural network tutorial describes dense layers and the other layer types in more detail.

Conclusions

A perceptron is a weighted sum followed by a nonlinear function. Its weights and bias decide how much each input counts, and its activation function decides the shape and the range of the response. Working through a neuron by hand, as we did with four inputs, is the quickest way to see what each parameter does.

By itself, a perceptron can only split the input space with a hyperplane. That limit disappears when many perceptrons work together: grouped into dense layers, with the layers chained one after another, they can learn highly nonlinear relationships from data.

Deep Neural Network

Although we have seen the functioning of the perceptron in this post, other neuron models have different characteristics and are used for different purposes.

Some of them are the scaling neuron, the principal components neuron, or the unscaling neuron.

Some neuron models only make sense when they are contextualized in a layer and cannot be defined individually. Some of these are the recurrent, long-short term memory (LSTM) or probabilistic layers.