AI and Deep Learning

How to fix the "unexpected keyword argument" error in PyTorch Lightning Trainer?

KA Asked by Karen Miller · 12-05-2025
0 upvotes 4,937 views 0 comments
The question

I recently started migrating my deep learning research to PyTorch Lightning to reduce boilerplate, but I keep hitting a TypeError: __init__() got an unexpected keyword argument 'gpus' when initializing the Trainer. I’m following an older tutorial. Was this parameter deprecated in the newer 2.x versions, or am I missing a specific import?

3 answers

0
LI
Answered on 15-05-2025

In the transition from PyTorch Lightning 1.x to 2.x, several arguments in the Trainer class were renamed to provide a more unified interface for different hardware types. The gpus argument was officially deprecated and removed. To fix this, you should now use the accelerator and devices arguments. For example, instead of Trainer(gpus=1), you should use Trainer(accelerator="gpu", devices=1). This change allows PyTorch Lightning to handle CPUs, GPUs, TPUs, and even IPUs using a consistent syntax. If you are on a Mac with M1/M2 chips, you would use accelerator="mps". Always check your version using lightning.__version__ to ensure your code matches the documentation.

0
MI
Answered on 18-05-2025

Are you also seeing issues with the strategy argument when trying to run distributed training across multiple nodes? I noticed that some older plugins also seem to throw keyword errors in the latest version.

DA 20-05-2025

Yes, Michael, the strategy argument replaced several individual flags. If you're trying to use Distributed Data Parallel, you now pass strategy="ddp" directly into the Trainer. This helps PyTorch Lightning manage the environment setup without you needing to manually configure the rank or world_size as you would in raw PyTorch code.

0
ST
Answered on 22-05-2025

You just need to change gpus=1 to devices=1 and set accelerator='auto' for the easiest fix. It’s a common hurdle when using PyTorch Lightning for the first time.

KA 23-05-2025

Steven is right; using accelerator='auto' is a lifesaver because it detects if a GPU is available without crashing your script on a CPU-only machine.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session