AdamW is very similar to Adam. It only differs in the way how the weight decay is implemented. The way how it's implemented in Adam came from the good old vanilla SGD optimizers which isn't mathematically correct. AdamW fixes this implementation mistake.
The authors of the original AdamW paper claimed that they were able to solve the generalization issues of the Adam solver with their modification. Empirically speaking, however, it seems that the right hyperparameter settings have a bigger impact than the choice between Adam and AdamW, but AdamW generalizes a bit better.
# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10
# Create random Tensors to hold inputs and outputs.
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
# Use the nn package to define our model and loss function.
model = torch.nn.Sequential(
loss_fn = torch.nn.MSELoss(reduction='sum')
# Use the optim package to define an Optimizer that will update the weights of
# the model for us. Here we will use AdamW; the optim package contains many other
# optimization algorithms. The first argument to the Adam constructor tells the