python-pytorchHow can I use Python and PyTorch with a CUDA-enabled GPU?
To use Python and PyTorch with a CUDA-enabled GPU, you need to install the CUDA Toolkit and the PyTorch library. After installation, you can use Python and PyTorch to create and run programs on the GPU.
To demonstrate, here is an example of a simple program that adds two numbers on the GPU:
import torch
# Create two random tensors on the GPU
a = torch.randn(3, device="cuda")
b = torch.randn(3, device="cuda")
# Add the two tensors on the GPU
c = a + b
# Print the result
print(c)
Output example
tensor([-1.6619, 0.0403, 0.9107], device='cuda:0')
The code consists of the following parts:
- Import the torch library:
import torch
- Create two random tensors on the GPU:
a = torch.randn(3, device="cuda")
andb = torch.randn(3, device="cuda")
- Add the two tensors on the GPU:
c = a + b
- Print the result:
print(c)
For more information, see the PyTorch Documentation and CUDA Documentation.
More of Python Pytorch
- How do I use Pytorch with Python 3.11 on Windows?
- How can I use Python and PyTorch to parse XML files?
- How can I use Python and PyTorch to create a Zoom application?
- How can I use Yolov5 with PyTorch?
- How can I use Python, PyTorch, and YOLOv5 to build an object detection model?
- How do I install PyTorch on a Windows computer?
- How can I use Python and PyTorch to create a U-Net architecture?
- What is the most compatible version of Python to use with PyTorch?
- How do I check which versions of Python are supported by PyTorch?
- How do I install a Python PyTorch .whl file?
See more codes...