1. Objective
Previously, logistic regression required:
- A loop over training examples
- A loop over features
The goal of vectorization is:
Process all training examples simultaneously
Eliminate all explicit for-loops
2. Data Representation
Instead of processing one example at a time, we stack all inputs into a matrix:
- Shape:
- Each column represents one training example
3. Vectorized Computation of
For a single example:
For all examples at once:
- is a row vector of shape
- Contains
Implementation:
Z = np.dot(W.T, X) + bPython automatically broadcasts across all columns
4. Vectorized Computation of
Apply sigmoid function to all elements:
- contains all predictions:
Implementation:
A = sigmoid(Z)5. Forward Propagation (Vectorized)
Instead of computing each example separately:
- Compute all in one step
- Compute all in one step
Entire dataset processed simultaneously
6. Broadcasting Concept
In:
- is a scalar (or )
- It is automatically expanded to match ‘s shape
This is called:
broadcasting
7. Key Insight
Vectorization transforms:
- separate computations
→ into one matrix operation
8. Computational Advantage
Benefits:
- Eliminates all loops
- Enables parallel computation
- Significantly faster execution
Especially important for:
- Large datasets
- Deep learning models
9. Extension to Backpropagation
Vectorization is not limited to forward propagation:
Backward propagation (gradient computation)
can also be fully vectorized
This allows:
- Efficient gradient computation
- Scalable training
10. Key Takeaways
- Stack training examples into matrix
- Compute in one step
- Apply activation:
- Use broadcasting for bias addition
- Remove all explicit loops
