Skip to main content

optim API Reference

Optimization algorithms for training neural networks.

SGD

class leanpass.optim.SGD(parameters, lr=0.01)

Stochastic Gradient Descent — the classic optimizer. Simple, effective, no frills.

ParameterTypeDefaultDescription
parameterslist[Tensor]List of trainable tensors (from model.parameters())
lrfloat0.01Learning rate

step()

Performs a single optimization step. Updates each parameter:

param.data -= lr * param.grad
optimizer = SGD(model.parameters(), lr=0.1)

for epoch in range(100):
loss = model(x)
optimizer.zero_grad()
loss.backward()
optimizer.step()

zero_grad()

Resets gradients of all tracked parameters to zero. Shorthand for model.zero_grad() at the optimizer level.

optimizer.zero_grad()

Adam

class leanpass.optim.Adam(parameters, lr=0.001, betas=(0.9, 0.999), eps=1e-8)

Adam optimizer — adaptive moment estimation with bias correction.

ParameterTypeDefaultDescription
parameterslist[Tensor]List of trainable tensors
lrfloat0.001Learning rate
betastuple[float, float](0.9, 0.999)Coefficients for running averages of gradient and squared gradient
epsfloat1e-8Small constant for numerical stability

step()

Performs a single optimization step with bias-corrected Adam updates.

zero_grad()

Resets gradients of all tracked parameters to zero.

Choosing an Optimizer

OptimizerWhen to UseTypical LR
SGDSmall datasets, simple problems, when you want to see gradients clearly0.01 — 0.1
AdamLarger datasets, faster convergence, works well out of the box0.001 — 0.01

Both optimizers support the same interface: step() and zero_grad().