๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #58

Weights and Bias

Weights and bias are the numbers a neural network actually learns during training โ€” everything else about the network (its architecture, its activation functions) is fixed by design before training even starts.

What Weights Represent

Each weight controls how strongly one specific input (or one neuron's output, for weights between hidden layers) influences the next neuron's computation. A large positive weight means that input strongly pushes the neuron toward firing; a large negative weight means it strongly suppresses firing; a weight near zero means that particular connection barely matters for this neuron's decision.

What Bias Represents

\[ z = \mathbf{w}^\top\mathbf{x} + b \]

The bias \(b\) shifts the neuron's decision threshold independent of the inputs โ€” it lets the neuron fire (or not) even when all inputs are zero. Without a bias term, the decision boundary \(\mathbf{w}^\top\mathbf{x}=0\) is forced through the origin, a real geometric restriction. With a bias, the boundary can sit anywhere in the input space.

Numerical Example โ€” Why Bias Matters Geometrically

Consider a single input, single neuron with step activation: without bias, \(z=wx\), and the decision boundary is always at \(x=0\) regardless of \(w\). With bias, \(z=wx+b\), the boundary sits at \(x=-b/w\) โ€” any point, chosen by learning \(b\). This one extra scalar parameter meaningfully expands what the neuron can represent.

Weight Matrices for a Full Layer

A layer with \(n\) inputs and \(m\) output neurons has a weight matrix \(\mathbf{W}\) of shape \((m,n)\) โ€” one row per output neuron, one column per input โ€” and a bias vector \(\mathbf{b}\) of shape \((m,)\), one bias per output neuron:

\[ \mathbf{z} = \mathbf{W}\mathbf{x} + \mathbf{b}, \qquad \mathbf{W} \in \mathbb{R}^{(m,n)},\ \mathbf{b}\in\mathbb{R}^{(m,)} \]

Code โ€” Inspecting a Layer's Weights and Bias

import torch.nn as nn

layer = nn.Linear(in_features=4, out_features=3)
print(layer.weight.shape)   # torch.Size([3, 4]) -- (out_features, in_features)
print(layer.bias.shape)      # torch.Size([3])     -- one bias per output neuron

print(layer.weight)   # randomly initialized, will be updated during training
print(layer.bias)

Where Initial Values Come From

Weights are never initialized to all zeros in practice (this would make every neuron in a layer compute an identical output and gradient, a failure mode called the "symmetry problem" โ€” every neuron would learn the exact same thing forever). Instead, weights are initialized to small random values, often drawn from a scaled normal or uniform distribution (Xavier/He initialization). Biases are commonly initialized to zero, since the symmetry problem doesn't apply to them the same way.

Common Mistakes

  • Initializing all weights to zero (or the same constant) โ€” every neuron in a layer would then receive identical gradients and stay identical forever, effectively wasting the layer's capacity.
  • Forgetting that a weight matrix's shape convention (\((\text{out},\text{in})\) vs \((\text{in},\text{out})\)) differs across frameworks โ€” always check .shape when debugging.

Interview Relevance

Q: "Why can't you initialize all of a neural network's weights to zero?" Every neuron in a layer would compute the exact same output (since they'd receive the same inputs and use the same, identical weights), and during backpropagation they'd receive identical gradients too โ€” the "symmetry problem." The layer would never differentiate its neurons from each other, wasting its representational capacity regardless of how long training runs.

Practice Question

A layer has 128 input features and 64 output neurons. What are the shapes of its weight matrix and bias vector, and how many total learnable parameters does this one layer have?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Weights and Bias โ€“ FAQs

Quick answers about learning Weights and Bias in Deep Learning.

This free note from CodingNow 2.0 explains Weights and Bias in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Weights and Bias, is 100% free with no signup required.
With focused practice, most students grasp Weights and Bias in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now