📐 Mathematical Foundations for Machine Learning
Learning Path Stage 1 of 4 • Next: Stage 2: Classic Machine Learning
Animora provides declarative, high-level primitives for visualizing machine learning optimization landscapes, contour elevations, vector flow fields, and multi-dimensional tensor heatmaps.
1. Gradient Descent Optimization
Animora executes exact numerical gradient descent on any user-provided 2D scalar loss function $f(x, y)$, recording step-by-step optimization diagnostics and rendering the descent trajectory over contour level curves in a single call:
from animora.core import Scene
from animora.ml import gradient_descent
from animora.theme import ModernDark, use_theme
class GradientDescentDemo(Scene):
def construct(self) -> None:
with use_theme(ModernDark):
# Define 2D loss surface f(x, y)
def loss(x: float, y: float) -> float:
return (x**2) + (y**2)
# One-call: runs real numerical gradient descent and animates trajectory
self.play(
*gradient_descent(
loss,
start=(2.5, 2.5),
learning_rate=0.1,
steps=25,
)
)
2. Mathematical Primitives
SurfacePlot
Renders 2D contour maps from scalar functions $f(x, y)$, automatically calculating iso-level curves and mapping data coordinates via Axes:
VectorField
Renders directional vector flows and gradient arrows over a regular 2D grid:
TensorGrid
Renders 2D matrices, neural network weights, and attention heatmaps with automatic cell styling, value labeling, and cell highlight animations:
⚠️ Common Pitfalls
- Learning Rate Divergence: Setting an excessively large learning rate in
gradient_descent(e.g.learning_rate=2.0on a steep quadratic) will cause the trajectory to explode beyond the coordinate bounds. Keep learning rates well conditioned (e.g.0.01to0.2). - Surface Range Matching: Ensure
startis within thex_rangeandy_rangeof the renderedSurfacePlotso the optimization steps remain visible on screen.
🔗 Related Guides & API
- Continue to Stage 2: Classic Machine Learning
- Reference:
GradientDescentModel,SurfacePlot,TensorGrid