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 分钟前
机器学习周报四十一
人工智能·机器学习
jr-create(•̀⌄•́)1 小时前
正则化和优化算法区别
pytorch·深度学习·神经网络·算法
kishu_iOS&AI2 小时前
深度学习 —— 神经网络(1)
人工智能·深度学习·神经网络
MOON404☾3 小时前
Chapter 001. Machine Learning Fundamentals
人工智能·机器学习
AC赳赳老秦3 小时前
程序员面试:OpenClaw生成面试题、模拟面试,高效备战面试
人工智能·python·机器学习·面试·职场和发展·deepseek·openclaw
纤纡.5 小时前
轻松实现多语言文字识别与实时检测:PaddleOCR 实战指南
人工智能·深度学习·opencv·paddlepaddle
【建模先锋】5 小时前
精品数据分享 | 锂电池数据集(10)基于阻抗的锂离子电池在不均衡使用情况下的性能预测
人工智能·python·深度学习·锂电池·锂电池寿命预测·锂电池数据集·剩余寿命预测
热爱生活的五柒6 小时前
度量学习-Radar Signal Deinterleaving Using Transformer Encoder and HDBSCAN 论文解析
深度学习·学习·transformer
源码之家6 小时前
计算机毕业设计:Python城市天气数据挖掘与预测系统 Flask框架 随机森林 K-Means 可视化 数据分析 大数据 机器学习 深度学习(建议收藏)✅
人工智能·爬虫·python·深度学习·机器学习·数据挖掘·课程设计
幻风_huanfeng6 小时前
人工智能之数学基础:坐标下降法
人工智能·深度学习·计算机视觉·梯度下降法·坐标下降法