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.

相关推荐
dfsj6601135 分钟前
LLMs 系列科普文(14)
人工智能·深度学习·算法
摘取一颗天上星️41 分钟前
深入解析机器学习的心脏:损失函数及其背后的奥秘
人工智能·深度学习·机器学习·损失函数·梯度下降
只有左边一个小酒窝1 小时前
(六)卷积神经网络:深度学习在计算机视觉中的应用
深度学习·计算机视觉·cnn
山顶听风2 小时前
MLP实战二:MLP 实现图像数字多分类
人工智能·机器学习·分类
carpell3 小时前
【语义分割专栏】3:Segnet实战篇(附上完整可运行的代码pytorch)
人工智能·python·深度学习·计算机视觉·语义分割
智能汽车人3 小时前
自动驾驶---SD图导航的规划策略
人工智能·机器学习·自动驾驶
mengyoufengyu3 小时前
DeepSeek11-Ollama + Open WebUI 搭建本地 RAG 知识库全流程指南
人工智能·深度学习·deepseek
一点.点4 小时前
AlphaDrive:通过强化学习和推理释放自动驾驶中 VLM 的力量
人工智能·机器学习·自动驾驶
vlln4 小时前
2025年与2030年AI及AI智能体 (Agent) 市场份额分析报告
人工智能·深度学习·神经网络·ai
GiantGo4 小时前
信息最大化(Information Maximization)
深度学习·无监督学习·信息最大化