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.

相关推荐
Salt_07287 分钟前
DAY 19 数组的常见操作和形状
人工智能·python·机器学习
智能交通技术2 小时前
iTSTech:自动驾驶技术综述报告 2025
人工智能·机器学习·自动驾驶
清云逸仙2 小时前
AI Prompt 工程最佳实践:打造结构化的Prompt
人工智能·经验分享·深度学习·ai·ai编程
松岛雾奈.2303 小时前
深度学习--TensorFlow框架使用
深度学习·tensorflow·neo4j
中杯可乐多加冰4 小时前
逻辑控制案例详解|基于smardaten实现OA一体化办公系统逻辑交互
人工智能·深度学习·低代码·oa办公·无代码·一体化平台·逻辑控制
大佬,救命!!!4 小时前
更换适配python版本直接进行机器学习深度学习等相关环境配置(非仿真环境)
人工智能·python·深度学习·机器学习·学习笔记·详细配置
励志成为糕手5 小时前
循环神经网络(RNN):时序数据的深度学习模型
人工智能·rnn·深度学习·gru·lstm
yLDeveloper7 小时前
致深度学习小白:一文理解拟合问题与经典解决方案
机器学习·dive into deep learning
CoovallyAIHub7 小时前
超越YOLOv8/v11!自研RKM-YOLO为输电线路巡检精度、速度双提升
深度学习·算法·计算机视觉
BagMM8 小时前
FC-CLIP 论文阅读 开放词汇的检测与分割的统一
人工智能·深度学习·计算机视觉