Perceptron and dense layers
Quick answer
A dense layer transforms each input row into a new vector using weights and biases. A perceptron applies a hard threshold to a weighted sum; a multilayer network uses differentiable or almost-everywhere differentiable activations to learn nonlinear boundaries. Static examples verified locally with Python 3.13.5 and NumPy 2.3.5 where imported. ByteMind execution is unverified.
Perceptron AND gate
X=[[0,0],[0,1],[1,0],[1,1]]print([int(a+b-1.5>=0) for a,b in X])Expected output: [0, 0, 0, 1]. A threshold at 1.5 selects only the row with both inputs equal to one.
How it works
Use rows for samples: X has shape (N,D), W has shape (D,H), b has shape (H,), and Z = X @ W + b has shape (N,H). Each column of W connects all D inputs to one output unit. A dense layer has DH + H trainable scalars. A classic binary perceptron returns 1 when z >= 0, otherwise 0; with labels 0/1, its mistake-driven update is w += eta(y-p)x and b += eta(y-p). A single affine threshold cannot represent XOR: the two diagonal positives and two diagonal negatives have intersecting convex hulls. Composing only affine layers remains affine, so hidden nonlinear activations are essential. Frameworks may store transposed weights; follow the declared convention.
Dense layer shape
import numpy as npX=np.array([[1,2],[3,4]])W=np.array([[1,0,-1],[2,1,1]])b=np.array([0,1,2])Z=X@W+bprint(Z.tolist(), Z.shape, W.size+b.size)Expected output: [[5, 3, 3], [11, 5, 3]] (2, 3) 9. Two rows become three coordinates each; six weights and three biases are shared across rows.
Common mistakes
Do not use a hard step in ordinary gradient descent: its derivative is zero almost everywhere. A matrix product is not elementwise multiplication. Bias broadcasts across samples, not across arbitrary axes. Two affine layers alone do not solve XOR.
Practice and next steps
Compute the output width and parameter count for 3 inputs and 4 units, then manually trace one row through a dense layer. Next, add a nonlinear activation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Perceptron and dense layers — knowledge check
Apply perceptron and dense layers to small examples and debugging scenarios.
What is the shape of Z for X (5,3) and W (3,4)?
How many parameters in a dense layer with 3 inputs and 4 units?
Why cannot two affine layers solve XOR by themselves?
Which update follows a missed positive perceptron example?
What does the bias vector do?