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.

相关推荐
罗西的思考44 分钟前
【Agentic RL】Dressage Staleness 控制深度分析
人工智能·算法·机器学习
Rocky Ding*2 小时前
深入解析GPT-6 Astra提示词工程:从Skills、AGENTS.md到可验证的Agent交付
论文阅读·人工智能·深度学习·机器学习·aigc·ai-native·提示词工程
江润舟3 小时前
万物|炼器:从零手搓工业级旋转目标检测网络·卷5 —— YAML蓝图与parse_model动态引擎(一)
人工智能·深度学习
EasyGBS3 小时前
水域乱象难管控?国标GB28181视频平台EasyGBS视频+AI双引擎赋能河湖监管
深度学习·安全·gb28181
田里的水稻4 小时前
PyTorch_Dataloader\TensorBoard---数据加载与可视化
人工智能·pytorch·python·神经网络·机器学习
田里的水稻4 小时前
FA_CNN卷积神经网络三---非线性激活函数PyTorch
人工智能·pytorch·神经网络·机器学习·cnn
小李同学_LHY5 小时前
第二章 机器学习分类
人工智能·机器学习
老鱼说AI6 小时前
脉冲神经网络:神经网络发展历史背景
人工智能·深度学习·神经网络·机器学习·自然语言处理·学习方法
frankzch6 小时前
Claude 刷新量子电路最优:LLM 与评估器的进化循环怎么搭
人工智能·深度学习·机器学习·语言模型·量子计算
拜小白6 小时前
【论文解读】PanoSwin:为全景理解打造的“全景版“ Swin Transformer
深度学习·计算机视觉·全景图