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.

相关推荐
CoovallyAIHub19 分钟前
CVPR 2026 | GS-CLIP:3D几何先验+双流视觉融合,零样本工业缺陷检测新SOTA,四大3D工业数据集全面领先!
深度学习·算法·计算机视觉
数据智能老司机4 小时前
PyTorch 深度学习——使用神经网络来拟合数据
pytorch·深度学习
数据智能老司机4 小时前
PyTorch 深度学习——用于图像的扩散模型
pytorch·深度学习
数据智能老司机4 小时前
PyTorch 深度学习——Transformer 是如何工作的
pytorch·深度学习
yiyu071615 小时前
3分钟搞懂深度学习AI:环境安装与工具使用
人工智能·深度学习
数据智能老司机1 天前
PyTorch 深度学习——使用张量表示真实世界数据
pytorch·深度学习
数据智能老司机1 天前
PyTorch 深度学习——它始于一个张量
pytorch·深度学习
yiyu07162 天前
3分钟搞懂深度学习AI:自我进化的最简五步法
人工智能·深度学习
yiyu07163 天前
3分钟搞懂深度学习AI:反向传播:链式法则的归责游戏
人工智能·深度学习
哥布林学者3 天前
高光谱成像(四)最小噪声分数变换 MNF
机器学习·高光谱成像