Skip to content
E
Educatum

Why is bias used in NN?

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

A bias term adds a learnable scalar offset to a neuron's weighted sum before applying the non-linear activation function, playing the same role as the intercept in linear regression. Without bias, the pre-activation hyperplane w^T x = 0 must pass through the origin and the zero-input response stays fixed at phi(0) (such as 0.5 for sigmoid). Including bias lets every neuron shift its activation threshold freely, dramatically expanding representational flexibility.

Study Card: The Role of Bias in Neural Networks

Direct Answer

In an artificial neuron computing y=ϕ(w⊤x+b)y = \phi(\mathbf{w}^\top \mathbf{x} + b), the bias bb is an additive learnable offset applied prior to the activation function ϕ\phi.

Without a bias (b=0b = 0):

  • The linear decision hyperplane w⊤x=0\mathbf{w}^\top \mathbf{x} = 0 is forced to pass through the origin x=0\mathbf{x} = \mathbf{0}.
  • Whenever all input features are zero (x=0\mathbf{x} = \mathbf{0}), the neuron’s output is locked at ϕ(0)\phi(0)—for example, σ(0)=0.5\sigma(0) = 0.5 for sigmoid or 00 for ReLU/tanh⁡\tanh—regardless of what weights w\mathbf{w} learn.

Adding bb turns the linear map into an affine transformation, allowing the activation threshold to translate away from the origin and enabling neurons to adjust their baseline firing propensity independently of input scaling.

Key Terms

  • Bias (bb): Learnable scalar intercept per neuron (or per output channel in CNNs) shifting the pre-activation w⊤x+b\mathbf{w}^\top \mathbf{x} + b.
  • Affine Transformation: A linear map plus a translation (x↦Wx+b\mathbf{x} \mapsto W\mathbf{x} + \mathbf{b}).
  • Activation Threshold: The hyperplane w⊤x+b=0\mathbf{w}^\top \mathbf{x} + b = 0 where activations like ReLU switch on or sigmoid crosses 0.50.5.
  • Intercept Analogy: Directly mirrors β0\beta_0 in linear or logistic regression (y=β0+β⊤xy = \beta_0 + \boldsymbol{\beta}^\top \mathbf{x}).

Example

Try implementing a 2-input AND gate (y=1y = 1 iff x1=1x_1 = 1 and x2=1x_2 = 1, classifying as 11 when σ(z)>0.5  ⟺  z>0\sigma(z) > 0.5 \iff z > 0) with a single sigmoid neuron.

  • Without bias (z=w1x1+w2x2z = w_1 x_1 + w_2 x_2): To predict 00 for (1,0)(1, 0) and (0,1)(0, 1), we need w1≤0w_1 \le 0 and w2≤0w_2 \le 0. That immediately forces w1+w2≤0w_1 + w_2 \le 0 for (1,1)(1, 1)—so no weights (w1,w2)(w_1, w_2) can separate (1,1)(1, 1) from (1,0)(1, 0) and (0,1)(0, 1) without a bias (and (0,0)(0,0) is stuck at σ(0)=0.5\sigma(0) = 0.5).
  • With bias (z=w1x1+w2x2+bz = w_1 x_1 + w_2 x_2 + b): Setting w1=5,w2=5,b=−7.5w_1 = 5, w_2 = 5, b = -7.5 gives z=−7.5z = -7.5 at (0,0)(0, 0), z=−2.5z = -2.5 at (0,1)(0, 1) and (1,0)(1, 0) (all <0  ⟹  σ(z)<0.08< 0 \implies \sigma(z) < 0.08), and z=+2.5z = +2.5 at (1,1)(1, 1) (σ(2.5)≈0.92>0.5\sigma(2.5) \approx 0.92 > 0.5).

Code Implementation

import numpy as np


def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))


def neuron_output(inputs, weights, bias=0.0):
    return sigmoid(np.dot(inputs, weights) + bias)


# Demonstrate 2-input AND gate with vs. without bias
inputs_set = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
weights = np.array([5.0, 5.0])
bias = -7.5

print("AND gate outputs (threshold 0.5):")
for x in inputs_set:
    y_with_bias = neuron_output(x, weights, bias=bias)
    y_no_bias = neuron_output(x, weights, bias=0.0)
    print(f"x={x} -> with bias: {y_with_bias:.4f} (pred={int(y_with_bias > 0.5)}) | "
          f"no bias: {y_no_bias:.4f} (pred={int(y_no_bias > 0.5)})")
  • Batch Normalization / Layer Normalization: Linear or convolutional layers immediately followed by BatchNorm commonly omit biases (use_bias=False) because subtracting the batch mean μB\mu_B cancels any constant bias shift, and BatchNorm’s own β\beta parameter already supplies the shift.
  • Weight Decay / Regularization: Biases are typically excluded from L1/L2 weight decay penalties because each bias controls only a single offset (carrying negligible overfitting variance), while penalizing biases can induce unnecessary underfitting.
  • Augmented Trick: Folding bias into weights by appending a constant feature 11 to x\mathbf{x} simplifies notation (w~⊤x~\tilde{\mathbf{w}}^\top \tilde{\mathbf{x}}), though production libraries track biases separately so weight decay and initialization can treat them distinctly.
  • Backpropagation for Bias: Since ∂z∂b=1\frac{\partial z}{\partial b} = 1, the gradient w.r.t. bias is simply the upstream pre-activation error δ(l)\delta^{(l)} summed over the mini-batch.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.