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.

相关推荐
@MMiL31 分钟前
PMSM基于SMO反电动势的PLL位置与速度估计
人工智能·机器学习
小王20432 分钟前
Day 35:DETR与检测Transformer — 目标检测的范式革命
人工智能·深度学习·transformer
明志数科42 分钟前
机器人训练数据质量评估体系:核心维度与全流程质控方法
人工智能·深度学习·机器学习
栀椩1 小时前
CODrone 无人机航拍车辆检测
pytorch·python·yolo
月华路1 小时前
《模型不玄学》第25章 双头模型:共享底座 + 两个分支
人工智能·深度学习·机器学习
盼小辉丶1 小时前
PyTorch计算机视觉(9)——变分自编码器(VAE)详解与实现
人工智能·pytorch·深度学习·计算机视觉
高洁012 小时前
AI智能体落地价值:从工具赋能到产业重构,解锁企业降本增效新逻辑
人工智能·深度学习·机器学习·transformer·知识图谱
clorinda2 小时前
PyTorch 手写数字识别学习笔记
pytorch·笔记·学习
中科院提名者2 小时前
深度学习语音增强架构演进的具体原因,从DNN开始
深度学习·架构·dnn