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.

相关推荐
工业机器视觉设计和实现35 分钟前
我的第三个cudnn程序(cifar10改cifar100)
人工智能·深度学习·机器学习
熊猫钓鱼>_>38 分钟前
PyTorch深度学习框架入门浅析
人工智能·pytorch·深度学习·cnn·nlp·动态规划·微分
祝余Eleanor1 小时前
Day 31 类的定义和方法
开发语言·人工智能·python·机器学习
背心2块钱包邮1 小时前
第6节——微积分基本定理(Fundamental Theorem of Calculus,FTC)
人工智能·python·机器学习·matplotlib
Wishell20152 小时前
日拱一卒之pytorch中的矩阵乘法
pytorch
Element_南笙2 小时前
吴恩达新课程:Agentic AI(笔记11)
大数据·人工智能·笔记·算法·机器学习
ziwu2 小时前
【车型识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
人工智能·深度学习·图像识别
B站_计算机毕业设计之家3 小时前
python招聘数据 求职就业数据可视化平台 大数据毕业设计 BOSS直聘数据可视化分析系统 Flask框架 Echarts可视化 selenium爬虫技术✅
大数据·python·深度学习·考研·信息可视化·数据分析·flask
熊猫钓鱼>_>3 小时前
TensorFlow深度学习框架入门浅析
深度学习·神经网络·tensorflow·neo4j·张量·训练模型·评估模型
玩具猴_wjh4 小时前
线性规划核心知识点
人工智能·机器学习