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.

相关推荐
STLearner9 小时前
ICML 2026 | LLM×Graph论文总结[1]【图基础模型,文本属性图,多模态属性图,图对齐,图提示学习,关系深度学习
论文阅读·人工智能·python·深度学习·学习·机器学习·数据挖掘
Young Doro12 小时前
SAC 算法
线性代数·算法·机器学习
SoaringPigeon15 小时前
NVIDIA博客-预训练去想象,微调去行动:世界-动作模型 (WAM) 的崛起
人工智能·深度学习·机器人·自动驾驶
Token炼金师16 小时前
注意力的六脉:QKV 数学、头设计、RoPE、Flash、掩码与剪枝 —— Transformer 核心机制全解
人工智能·深度学习·llm
_Meilinger_16 小时前
碎片笔记|分类问题与回归问题的关系
深度学习·机器学习·监督学习·损失函数·回归问题·分类问题·条件概率
卡梅德生物科技小能手16 小时前
卡美德生物科普CSF1(巨噬细胞集落刺激因子1)
经验分享·深度学习·生活
学究天人16 小时前
数学公理体系大全:第七章 连续统假设与力迫法简介
人工智能·算法·机器学习·数学建模·动态规划·图论·抽象代数
FeelTouch Labs17 小时前
深度学习模型架构——Transformer
人工智能·深度学习·transformer
我不是QI18 小时前
周志华《机器学习—西瓜书》九
人工智能·机器学习·支持向量机