PyTorch中torch.cuda()函数报错求助,TensorFlow可正常使用GPU
Hey there! I’ve run into this exact frustration before—TensorFlow hums along using the GPU, but PyTorch just refuses to play nice. Let’s walk through the most likely fixes for your setup (Windows 10 64bit, Python 3.6, CUDA 9.0, GTX 965M):
1. Double-Check You Installed the CUDA-Compatible PyTorch Version
It’s super easy to accidentally install the CPU-only PyTorch package, even if you have CUDA set up. For your CUDA 9.0 + Python 3.6 setup, you’ll need a specific PyTorch build. Run this command in your terminal (make sure you’re in the right virtual environment if you use one):
pip install torch==1.1.0 torchvision==0.3.0
(Note: This is the last stable PyTorch version that officially supports CUDA 9.0 and Python 3.6.)
To verify if PyTorch sees your GPU, run these lines in a Python shell:
import torch print(torch.cuda.is_available()) # Should return True if GPU is detected print(torch.version.cuda) # Should output 9.0 if the right build is installed
2. Confirm CUDA & cuDNN Are Properly Configured
Even though TensorFlow works, PyTorch can be pickier about file placements and environment variables:
- cuDNN File Placement: Make sure your cuDNN files are copied to the correct CUDA 9.0 directories:
- Copy
cudnn.htoC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include - Copy
cudnn.libtoC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64 - Copy
cudnn64_7.dll(note: CUDA 9.0 requires cuDNN 7.x, not older versions) toC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
- Copy
- Environment Variables: Check that these are set in your system:
CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0- Add
%CUDA_PATH%\binand%CUDA_PATH%\libnvvpto yourPATHvariable
- Restart your terminal/IDE after making changes—environment variables won’t take effect until you do!
3. Verify GPU Driver Compatibility
GTX 965M needs a driver version that supports CUDA 9.0. The minimum required driver is 384.81. You can check your current driver version by opening NVIDIA Control Panel > Help > System Information. If it’s older than 384.81, update it from NVIDIA’s website (don’t worry, this won’t break your TensorFlow setup).
4. Rule Out Virtual Environment/IDE Issues
- If you’re using a virtual environment, make sure you installed PyTorch inside it (not the global Python installation).
- In your IDE (like PyCharm or VS Code), double-check that the selected interpreter is the one with PyTorch installed. Sometimes IDEs default to the global Python, which might not have the GPU-enabled PyTorch package.
If none of these steps work, share the output of these commands and we can dig deeper:
import torch print(torch.cuda.is_available()) print(torch.version.cuda) print(torch.cuda.device_count()) print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else "No GPU detected")
内容的提问来源于stack exchange,提问作者Neo H

