Evaluating and Debugging Generative AI : A Deep Dive Into the Second Lesson
# Generative AI
# Machine learning
# MLops
Generative AI (GenAI) is having a moment
August 11, 2023
Agata Mlynarczyk
Generative AI (GenAI) is having a moment. In just the past few months, diffusion and large language models have revolutionized the field of machine learning. From creating realistic images to generating human-like text, not a month goes by where there isn’t a new, powerful model. We’ve come a long way from the avocado chair of the first DALL-E.
Specifically, we’ll learn about diffusion models, how they’re trained, and how to evaluate them using best-in-class tools.
The Magic of Diffusion Models
Diffusion models are denoising models, where the primary task of the model is not to generate images, but to remove noise from images. The model is trained by adding noise to images and forcing it to predict the noise present on the image.
During the training phase, we’ll monitor relevant metrics like the loss curve, but there’s an interesting twist. The loss curve tends to flatten out quite early, which could mislead you into thinking that your model is fully trained. However, by sampling from the model regularly during training, we observe that the quality of generated images continues to improve despite a plateau in loss. Meaning it’s crucial to log not only the loss but also the samples at regular intervals.
t = torch.randint(1, config.timesteps + 1, (x.shape[0],)).to(DEVICE)
x_pert = perturb_input(x, t, noise)
pred_noise = nn_model(x_pert, t / config.timesteps, c=c)
loss = F.mse_loss(pred_noise, noise)
loss.backward()
optim.step()
wandb.log({"loss": loss.item(),
"lr": optim.param_groups[0]['lr'],
"epoch": ep})
# save model periodically
if ep%4==0 or ep == int(config.n_epoch-1):
nn_model.eval()
ckpt_file = SAVE_DIR/f"context_model.pth"
torch.save(nn_model.state_dict(), ckpt_file)
artifact_name = f"{wandb.run.id}_context_model"
at = wandb.Artifact(artifact_name, type="model")
at.add_file(ckpt_file)
wandb.log_artifact(at, aliases=[f"epoch_{ep}"])
samples, _ = sample_ddpm_context(nn_model,
noises,
ctx_vector[:config.num_samples])
wandb.log({
"train_samples": [
wandb.Image(img) for img in samples.split(1)
]})
# finish W&B run
wandb.finish()
That code initiates a Weights & Biases run for our training process and logs the loss, learning rate, and the current epoch at each iteration. We also log the image samples generated at the end of each epoch, allowing us to track the model’s progression visually.
When we train our diffusion model, we set up a training loop. At the start, we initialize a Weights & Biases run to keep track of the training. As the training proceeds, we log the loss, learning rate, and current epoch. Here, the model checkpoints are saved every four epochs, providing a snapshot of the model’s state at that point in time.
Still: the most rewarding part is observing the model’s progress visually. The initially noisy and grainy images generated by the model become clearer and more recognizable as training progresses. This logged in your Weights & Biases workspace where you can view the image generations in real time. You can visit the result of the code from this lesson’s training here(opens in a new tab)!
Finally, once our model starts generating high-quality images, we make it available for the rest of the team using the Model Registry(opens in a new tab) feature in Weights & Biases. This allows team members to view all the best model versions, the lineage of the model, and get back to the training run that produced the model.
Key Takeaways
The “Evaluating and Debugging Generative AI” course offers a blend of theory and hands-on experience. Lesson two focuses on the training part, sign up(opens in a new tab) for the course today and practice:
Managing hyperparameter config
Collecting artifacts for dataset and model versioning
Loging experiment results
Tracing prompts and responses to LLMs over time in complex interactions