liveBook · Manning

Adam

As explained in Learn Generative AI with PyTorch

Adam is an adaptive stochastic-gradient optimization algorithm that updates model parameters using moving estimates of the first and second moments of gradients.

Adam is an optimizer that adjusts model parameters to reduce a loss. Unlike basic gradient descent, which considers the current gradient alone, Adam also uses information from earlier gradients. It is a practical choice for training neural networks without designing an update rule from scratch. section 2.2.1 section 2.3.2

Use Adam in a classification model

For a binary image classifier, create `optimizer = torch.optim.Adam(binary_model.parameters(), lr=0.001)` alongside binary cross-entropy loss. For each batch, make predictions, calculate loss, then call `optimizer.zero_grad()`, `loss.backward()`, and `optimizer.step()`. The backward call computes gradients; the step call updates the parameters. section 2.3.2

Adjust the setup to the task:

  • Learning rate (`lr`) controls the size of weight adjustments. It can be selected using a validation set; examples use 0.001 for classification and 0.0001 for a music Transformer. section 2.2.1 section 14.4.1
  • A GAN uses separate Adam optimizers for its generator and discriminator. A DCGAN example sets `lr=0.0002` and `betas=(0.5, 0.999)` for both; the betas govern how recent and past gradient information influences training. section 3.3.3 section 4.5.1
  • A music Transformer clips the gradient norm to 1 after `loss.backward()` and before `optimizer.step()` to guard against exploding gradients. section 14.4.1
Listing 2.6 Training a binary classification model
for i in range(50):                                    #1
    tloss=0
    for n,(imgs,labels) in enumerate(train_loader):            #2
        imgs=imgs.reshape(-1,28*28)                    #3
        imgs=imgs.to(device)
        labels=torch.FloatTensor(\
          [x if x==0 else 1 for x in labels])          #4
        labels=labels.reshape(-1,1).to(device)
        preds=binary_model(imgs)    
        loss=loss_fn(preds,labels)                     #5
        optimizer.zero_grad()
        loss.backward()                                #6
        optimizer.step()
        tloss+=loss.detach()
    tloss=tloss/n
    print(f"at epoch {i}, loss is {tloss}")

Inspect the complete binary-classification loop to see where gradient clearing, backpropagation, and Adam's parameter update occur within each batch. section 2.3.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains gradient history, learning rate, betas, and training-loop calls, but does not derive Adam's update equations.
Examples
Uses a binary clothing classifier, separate GAN optimizers, handwritten-digit reconstruction, and a music Transformer.
What it assumes
Assumes a model and loss function, while introducing how gradients and optimizer steps fit into training.
What you can do after
Apply it via PyTorch `torch.optim.Adam` and place its update step after gradient computation in a training loop.
How it connects
Connects Adam to gradient descent, loss functions, backpropagation, learning-rate tuning, gradient clipping, and generative adversarial networks.

Also explained in

Compare how 7 books explain adam →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.