Artificial neurons
What an artificial neuron computes
A neural network is a parameterized function: its output depends on the inputs and adjustable numbers called parameters. An artificial neuron combines input features using weights and a bias, then optionally transforms the result using an activation function. It is a mathematical abstraction, not a faithful simulation of a biological neuron. For example, a fictional package inspection system might combine two already scaled sensor readings into a score. The hand-chosen parameters in this chapter demonstrate inference only; they have not been fitted to inspection data.
Weighted sum and bias
For input vector x = [x₁, x₂] and weight vector w = [w₁, w₂], the pre-activation value is z = w₁x₁ + w₂x₂ + b. More generally, multiply corresponding coordinates, add them, then add the scalar bias b. The bias shifts the response even when all inputs are zero. With x = [2, 3], w = [1, −1] and b = 2, z = 2 − 3 + 2 = 1. Changing the second reading to 5 gives −1. A negative weight means that increasing that feature decreases this pre-activation while other inputs are fixed; it is not evidence of a causal relationship. The function below rejects unequal lengths because zip alone would silently truncate. Its input contract is nonempty sequences of small finite numbers and a finite bias. Output: 1, then −1.
def weighted_sum(inputs, weights, bias): if not inputs or len(inputs) != len(weights): raise ValueError("Expected equally sized nonempty vectors") return sum(x * w for x, w in zip(inputs, weights)) + bias
print(weighted_sum([2, 3], [1, -1], 2))print(weighted_sum([2, 5], [1, -1], 2))Activation functions change the response
The activation a = f(z) follows the weighted sum. ReLU (rectified linear unit) returns max(0, z). It keeps positive values and maps zero and negative values to zero. For the two scores above, the outputs are 1 and 0. Sigmoid returns 1/(1+exp(−z)); mathematically it lies strictly between 0 and 1 for finite z, though floating-point arithmetic may round to an endpoint. At z = 0 its value is 0.5. A sigmoid on a binary-classification logit produces a model probability estimate, not a guarantee of calibration or a class label. A threshold is a separate decision. Tanh is another nonlinear activation, mapping to (−1,1) mathematically. The branch in sigmoid below avoids evaluating exp of a very large positive value. Expected rows: −3 gives ReLU 0 and sigmoid 0.0474; 0 gives 0 and 0.5; 3 gives 3 and 0.9526.
from math import exp
def relu(z): return max(0, z)
def sigmoid(z): if z >= 0: return 1 / (1 + exp(-z)) t = exp(z) return t / (1 + t)
for z in [-3, 0, 3]: print(z, relu(z), round(sigmoid(z), 4))Why nonlinearity matters
An affine map is a weighted sum plus bias. Composing affine maps still yields one affine map, regardless of the number of layers. Nonlinear activations let networks represent functions that cannot be reduced this way. For example, ReLU bends a line at zero. This does not mean every nonlinear network is expressive enough, or that extra layers automatically improve accuracy. The choice depends on the task, data and training behavior. Large positive or negative sigmoid inputs produce nearly flat responses (saturation), which can make gradient-based training difficult. ReLU also has a flat region on negative inputs. Detailed derivatives and training appear in the next chapter.
Common mistakes and practice
Keep input order aligned with weight order. A feature measured in grams instead of kilograms may drastically change the score; use the same fitted preprocessing and units at training and inference. Do not mistake a weight for a probability or an activation for a complete decision rule. Bias is added once per neuron, not once per input. Try x = [0, 0] with the original parameters: z = 2 and ReLU = 2. Then set the bias to −2: z = −2 and ReLU = 0. Explain why the difference occurs. Next, combine multiple neurons into a layer.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Artificial neurons quiz
Check neural network calculations and reasoning.
For inputs [2,3], weights [1,−1], bias 2, what is z?
What does ReLU return for z = −4?
What is the sigmoid at z = 0?
Why does weighted_sum explicitly check that input and weight vectors have equal length?
Why can adding nonlinear activations between affine layers increase representational flexibility?