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.

相关推荐
芯片-嵌入式16 小时前
具身智能(3):有哪些AI模型
人工智能·深度学习·机器学习
用户28628100934016 小时前
使用 PyTorch 进行模型训练train
pytorch
汀沿河16 小时前
2 模型预训练、微调、强化学习的格式
人工智能·算法·机器学习
王侯相将17 小时前
Claude Code 是什么?
人工智能·深度学习
湘美书院--湘美谈教育17 小时前
湘美书院主理人:AI时代的文雅智能,赏花赏月赏秋香
人工智能·深度学习·神经网络·机器学习·ai写作
Dfreedom.18 小时前
从“阅读小说”到循环网络:一篇搞懂循环神经网络(RNN)
人工智能·rnn·深度学习·神经网络
沪漂阿龙18 小时前
DeepSeek-R1 深度解析:如何炼成最强推理大模型?
人工智能·深度学习·机器学习
AI_Auto19 小时前
【人工智能】- OpenClaw本地化安装
大数据·人工智能·机器学习·数据挖掘
wuxuand19 小时前
DIFT:用扩散模型+Transformer解决物联网入侵检测中的类别不平衡问题
深度学习·物联网·transformer
沃达德软件20 小时前
AI视频监控:智慧场馆安防升级
图像处理·人工智能·深度学习·目标检测·机器学习·计算机视觉·目标跟踪