Vectorized Neural Networks Model Representation
Learn how to represent neural networks in a vectorized form, transforming scalar equations into efficient matrix operations for scalable and optimized computations.
Forward Propagation
For any layer :
Linear Step
Calculate pre activation term
Activation Step
Apply activation
This process is repeated until the output layer.
From Scalar Equations to Vector Form
graph LR
%% Input Layer
subgraph Input Layer
x1(((x1)))
x2(((x2)))
x3(((x3)))
end
%% Hidden Layer 1
subgraph Hidden Layer 1
a1{a1}
a2{a2}
a3{a3}
end
%% Output Layer
subgraph Output Layer
y(((hθx)))
end
%% Connections: Input → Hidden 1
x1 --> a1
x1 --> a2
x1 --> a3
x2 --> a1
x2 --> a2
x2 --> a3
x3 --> a1
x3 --> a2
x3 --> a3
%% Connections: Hidden 2 → Output
a1 --> y
a2 --> y
a3 --> y
Previously, we wrote each neuron separately.
For the hidden layer:
Where
- Superscript indicates layer 2 (hidden layer)
- is the sigmoid function
Final hypothesis is:
Where
It does not scale. So we need to vectorize it for more complex use cases.
Pre Activation Term
Intermediate Variable contain the weighted sum before activation:
Suppose
🧠 Generalized preactivation term
Then
🧠 Generalized Activation
This separates:
- Linear computation
- Nonlinear activation
Vector Representation
Input layer:
Where = 1
Let
Weighted sum vector:
Where:
We can calculate as
Since x = , so we can rewrite it:
🧠 Generalized Vectorized Preactivation term:
Where Vector Dimensions:
Activation Function
Since
Generalized Activation Function:
If using sigmoid:
Add Bias Unit
After computing , add:
Now:
Output Layer
Repeat the same process:
calculate Linear Term z
Apply activation sigmoid of z
Final hypothesis:
🧠 Generalized Hypothesis
The Big Picture
Each layer performs:
followed by
Stacking these layers allows neural networks to represent complex nonlinear functions.
Intuition
If we remove the hidden layer, the model becomes logistic regression:
With hidden layers, the network instead uses learned features:
These are:
- Computed by the hidden layer
- Learned from data
- Controlled by parameters
So a neural network is:
Logistic regression on learned features.
Modern Relevance
Vectorization is why neural networks run on GPUs at all. The scalar z = Σ θᵢ aᵢ per neuron is a sequential bottleneck on CPU. The matrix form Z = ΘA is a GEMM (General Matrix Multiply) — the single operation that all of GPU computing is optimized around.
- NVIDIA H100 Tensor Cores execute BF16 GEMM at ~4,000 TFLOPS. A single transformer attention layer for a 70B model is a sequence of GEMMs across Q, K, V projections — each running at near-peak tensor core throughput.
- cuBLAS and cuDNN are NVIDIA's hand-tuned GEMM libraries. TensorRT selects the optimal GEMM kernel for each layer's exact shape at compile time.
- The
(m × n) × (n × p)matrix multiply shape determines which GPU kernel is most efficient. TRT-LLM uses different GEMM implementations for prefill (large batch) vs decode (small batch, single token) — the shapes are different, so different kernels win. - FP8 Tensor Cores (H100-only) run the same GEMM at 2× the throughput of BF16 with careful scaling to avoid overflow — NIM's FP8 profiles exploit exactly this.
The key insight at the bottom of this post — "a neural network is logistic regression on learned features" — is also the key insight behind transfer learning: freeze the early layers (keep the learned features), only retrain the final layer for a new task.
Related Posts
- Forward Propagation in Neural Networks — the scalar form of forward propagation that this post vectorizes
- AI Programming Model (CUDA) — the matrix-vector multiplications in vectorized forward propagation map directly to CUDA GEMM kernels on GPU Streaming Multiprocessors
- Examples and Intuitions I — worked examples using this vectorized representation
