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.

相关推荐
极客BIM工作室6 分钟前
理清 BERT 中 [CLS] 向量的核心逻辑:训练双向更新与推理作用不矛盾
人工智能·机器学习·bert
IT·小灰灰12 分钟前
基于Python的机器学习/数据分析环境搭建完全指南
开发语言·人工智能·python·算法·机器学习·数据分析
Keep_Trying_Go23 分钟前
LightningCLI教程 + 视频讲解
人工智能·pytorch·语言模型·大模型·多模态·lightning
AI即插即用2 小时前
即插即用系列 | 2024 SOTA LAM-YOLO : 无人机小目标检测模型
pytorch·深度学习·yolo·目标检测·计算机视觉·视觉检测·无人机
AIminminHu2 小时前
底层视觉及图像增强-项目实践理论补充(十六-0-(25):PyTorch---》底层视觉及图像增强):从奥运大屏到手机小屏,快来挖一挖里面都有什么
pytorch
金融小师妹2 小时前
基于机器学习与深度强化学习:非农数据触发AI多因子模型预警!12月降息预期骤降的货币政策预测
大数据·人工智能·深度学习·1024程序员节
walnut_oyb3 小时前
arXiv|SARLANG-1M:用于 SAR 图像理解的视觉-语言建模基准
论文阅读·人工智能·机器学习·计算机视觉·语言模型·自然语言处理
brave and determined3 小时前
可编程逻辑器件学习(day29):Verilog HDL可综合代码设计规范与实践指南
深度学习·fpga开发·verilog·fpga·设计规范·硬件编程·嵌入式设计
zhanglei5000383 小时前
一、机器学习概述
机器学习
Caven773 小时前
【2025版李宏毅机器学习系列课程】CH2 机器学习 Training Guide
人工智能·机器学习