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.

相关推荐
tellmewhoisi28 分钟前
机器学习:集成学习4(XGBoost前置知识泰勒展开式4)
人工智能·机器学习·集成学习
2601_962177302 小时前
小白安装Claude Code完整教程:Windows从零装好并接入Crazyrouter(附403解决方法)
人工智能·深度学习·目标检测·机器学习·数据挖掘
xx_xxxxx_2 小时前
论文阅读-CoTTA
人工智能·深度学习·机器学习
空奈qwq3 小时前
PyTorch 进阶指南:从张量操作到模型训练全流程
人工智能·pytorch·深度学习
会议咨询3 小时前
2026年计算机工程、数据处理与机器学习国际会议(CDML 2026)
人工智能·机器学习·数据处理
田园诗人之园3 小时前
深度学习归一化 fully 指南:BN/LN/IN/GN/RMSNorm 公式推导 + PyTorch 实验全验证(附完整可运行代码)
深度学习·深度学习归一化
龙腾AI白云3 小时前
AI微调技术:让通用大模型精准适配垂直行业
数据库·人工智能·机器学习·flask·scikit-learn
uncle_ll4 小时前
分类任务解决样本数据不均衡的落地实战指南
人工智能·深度学习·机器学习·分类·数据处理
一切皆是因缘际会4 小时前
掌控信息论:同源星际通信零延迟架构
人工智能·深度学习·ai·系统架构·信息与通信·星际通信·同源通信
tellmewhoisi4 小时前
机器学习:聚类算法
算法·机器学习·聚类