python-pytorchHow can I use Python PyTorch without CUDA?
You can use Python PyTorch without CUDA by setting the device
to cpu
when creating the Tensors. For example:
import torch
x = torch.rand(5, 3)
print(x.device)
device = torch.device("cpu")
x = torch.rand(5, 3, device=device)
print(x.device)
Output example
cpu
cpu
import torch
imports the PyTorch library.x = torch.rand(5, 3)
creates a tensor of size (5, 3) on the default device, which is usually the GPU.print(x.device)
prints the device on which the tensor is located.device = torch.device("cpu")
creates a CPU device object.x = torch.rand(5, 3, device=device)
creates a tensor of size (5, 3) on the CPU device.print(x.device)
prints the device on which the tensor is located.
Helpful links
More of Python Pytorch
- How can I use Python and PyTorch to create a Zoom application?
- How can I use Python, PyTorch, and YOLOv5 to build an object detection model?
- How can I use Yolov5 with PyTorch?
- How can I use Python and PyTorch to parse XML files?
- How do I use Pytorch with Python 3.11 on Windows?
- How can I use Python PyTorch with CUDA?
- How can I install Python PyTorch on Ubuntu using ROCm?
- How do I use PyTorch with Python version 3.11?
- How do I install PyTorch on a Windows computer?
- What is the most compatible version of Python to use with PyTorch?
See more codes...