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.

相关推荐
多年小白4 小时前
【周末消息面汇总】2026年5月10日(周日)
人工智能·科技·机器学习·ai·金融
MediaTea4 小时前
Scikit-learn:从数据到结构——无监督学习的最小闭环
人工智能·学习·算法·机器学习·scikit-learn
Honey Ro5 小时前
深度学习中的参数更新方法
深度学习·神经网络·自然语言处理·cnn
nap-joker5 小时前
阿尔茨海默病分期早期检测的多模式深度学习模型
人工智能·深度学习·adni
赵药师5 小时前
Cityscape数据集转YOLO
人工智能·深度学习·yolo
o_insist5 小时前
多层感知机判断氨基酸亲疏水性(PyTorch版)
人工智能·深度学习·机器学习
星光技术人7 小时前
投机采样 Speculative Decoding 核心笔记
人工智能·笔记·深度学习·计算机视觉·语言模型·自动驾驶
qq_283720057 小时前
RecursiveCharacterTextSplitter 核心参数深度指南:chunk_size 与 chunk_overlap 原理、实战、调优全解
人工智能·机器学习
倔强的胖蚂蚁8 小时前
Transformer 大模型原理 完整入门指南
人工智能·深度学习·云原生·transformer