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.

相关推荐
Learn Beyond Limits1 小时前
Mean Normalization|均值归一化
人工智能·神经网络·算法·机器学习·均值算法·ai·吴恩达
byzy1 小时前
【论文笔记】VisionPAD: A Vision-Centric Pre-training Paradigm for Autonomous Driving
论文阅读·深度学习·计算机视觉·自动驾驶
ACERT3331 小时前
5.吴恩达机器学习—神经网络的基本使用
人工智能·python·神经网络·机器学习
Aaplloo2 小时前
【无标题】
人工智能·算法·机器学习
可触的未来,发芽的智生3 小时前
触摸未来2025.10.06:声之密语从生理构造到神经网络的声音智能革命
人工智能·python·神经网络·机器学习·架构
动能小子ohhh3 小时前
AI智能体(Agent)大模型入门【6】--编写fasteAPI后端请求接口实现页面聊天
人工智能·python·深度学习·ai编程
我是Feri4 小时前
机器学习之线性回归的特征相关性:避免“双胞胎特征“干扰模型
人工智能·机器学习
拉姆哥的小屋4 小时前
深度学习图像分类实战:从零构建ResNet50多类别分类系统
人工智能·深度学习·分类
盼小辉丶4 小时前
TensorFlow深度学习实战(39)——机器学习实践指南
深度学习·机器学习·tensorflow
蒋星熠4 小时前
反爬虫机制深度解析:从基础防御到高级对抗的完整技术实战
人工智能·pytorch·爬虫·python·深度学习·机器学习·计算机视觉