Loading Python for AI Engineering...
Vectorization delegates batch element calculations to pre-compiled BLAS / C / CUDA kernels. Broadcasting stretches smaller dimensional tensors across larger ones without copying data, according to the rule: dimensions must be equal, or one of them must be 1.
“Broadcasting is like projecting a 1D silhouette with a flashlight onto a 2D wall. The shadow expands across the surface without needing extra physical objects.”
Unintentionally broadcasting arrays with mismatched dimensions, producing silent mathematical shape bugs instead of throwing an error.
import numpy as np
# Normalizing a batch of embeddings:
# Shape: (batch_size=4, embedding_dim=3)
embeddings = np.array([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
[2.0, 4.0, 6.0]
])
# Mean per feature: shape (1, 3) broadcasts over (4, 3)
means = np.mean(embeddings, axis=0, keepdims=True)
normalized = embeddings - means # Stride 0 along axis 0!