The “typeerror: weight_decay is not a valid argument, kwargs should be empty for `optimizer_experimental.optimizer`” error occurs when you are trying to pass the “weight_decay” argument to the optimizer in an unsupported way.
The “optimizer_experimental.optimizer” function expects the **kwargs argument to be empty, meaning you should not pass any additional arguments to the optimizer. However, you are passing the “weight_decay” argument which is not supported by this optimizer.
To resolve this issue, you need to remove the “weight_decay” argument from your code. If you still want to include weight decay in your optimization process, you may need to choose a different optimizer that supports this argument.
Here’s an example to illustrate the error and its resolution:
# Import the necessary libraries
import torch
import torch.optim as optim
# Define your model
model = MyModel()
# Define the optimizer
optimizer = optim.optimizer_experimental.optimizer(model.parameters())
# Set the weight decay
weight_decay = 0.001
# This line will raise the error
optimizer = optim.optimizer_experimental.optimizer(model.parameters(), weight_decay=weight_decay)
# To fix this error, remove the weight_decay argument
optimizer = optim.optimizer_experimental.optimizer(model.parameters())
In the above example, the error occurs because the “weight_decay” argument is passed to the “optimizer_experimental.optimizer” function. To fix the error, we simply remove the “weight_decay” argument from the function call.
Always make sure to refer to the documentation of the optimizer you are using to ensure which arguments are supported and how they should be used.