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.

相关推荐
小蜜蜂爱编程1 天前
deep learning简介
人工智能·深度学习
Learn Beyond Limits1 天前
Clustering vs Classification|聚类vs分类
人工智能·算法·机器学习·ai·分类·数据挖掘·聚类
大连好光景1 天前
LSTM模型做分类任务2(PyTorch实现)
人工智能·pytorch·lstm
LeonDL1681 天前
基于YOLO11深度学习的电梯内车辆识别系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
人工智能·python·深度学习·pyqt5·yolo数据集·yolo11深度学习·电梯内车辆识别系统
Sanlings1 天前
ComfyUI+RX5700XT+Ubuntu25.04运行配置
pytorch·ai·comfyui·amd·rocm·rx5700xt·ubuntu25.04
gihigo19981 天前
MATLAB数值分析方程求解方法详解
算法·机器学习·matlab
盼小辉丶1 天前
TensorFlow深度学习实战——胶囊网络
深度学习·tensorflow·keras
doubao361 天前
如何有效降低AIGC生成内容被识别的概率?
人工智能·深度学习·自然语言处理·aigc·ai写作
Danceful_YJ1 天前
31.注意力评分函数
pytorch·python·深度学习
xier_ran1 天前
深度学习:神经网络中的参数和超参数
人工智能·深度学习