How to verify your CUDA and pytorch

1.Test CUDA

This script will attempt to create a tensor on the GPU and perform a simple computation.

python 复制代码
import torch

def test_cuda():
    # Check CUDA availability
    if torch.cuda.is_available():
        print("CUDA is available on this device.")
        # Set device to CUDA
        device = torch.device("cuda")
        # Create a tensor and move it to CUDA
        x = torch.rand(10, 10).to(device)
        print("Successfully created a tensor on CUDA:", x)
        # Perform a simple computation
        y = x * x
        print("Output of the computation:", y)
    else:
        print("CUDA is not available. Check your installation and driver.")

if __name__ == "__main__":
    test_cuda()

Run the script: Use a terminal or command prompt to run the script with Python.

bash 复制代码
python test_cuda.py

The script should print whether CUDA is available, show the tensor created on the GPU, and display the output of a simple multiplication operation. If there are any errors during these steps, they will help pinpoint what might be wrong with your CUDA setup.

2.Check PyTorch Compatibility

python 复制代码
# test_torch.py
import torch
print(torch.__version__)
print(torch.cuda.is_available())

Run the script: Use a terminal or command prompt to run the script with Python.

bash 复制代码
python test_torch.py

If torch.cuda.is_available() returns True, then PyTorch is able to use CUDA.

相关推荐
摸鱼仙人~4 小时前
一文详解PyTorch DDP
人工智能·pytorch·python
Salt_07285 小时前
DAY44 简单 CNN
python·深度学习·神经网络·算法·机器学习·计算机视觉·cnn
雍凉明月夜5 小时前
深度学习网络笔记Ⅱ(常见网络分类1)
人工智能·笔记·深度学习
jackylzh5 小时前
配置pytorch环境,并调试YOLO
人工智能·pytorch·yolo
liulanba6 小时前
YOLO-World 端到端详解
机器学习
liulanba6 小时前
YOLOv6 端到端详解
机器学习
RaymondZhao347 小时前
【深度硬核】AI Infra 架构漫游指南
人工智能·深度学习·架构
惊鸿一博7 小时前
深度学习概念_随机梯度下降 与 ADAM 的区别与联系 公式化表达
人工智能·深度学习
xoliu17 小时前
Pytorch核心基础入门
人工智能·pytorch·python
其美杰布-富贵-李7 小时前
PyTorch Lightning Callback 指南
人工智能·pytorch·python·回调函数·callback