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.

相关推荐
sinat_286945191 小时前
AI Coding 时代的 TDD:从理念到工程落地
人工智能·深度学习·算法·tdd
Rabbit_QL3 小时前
【理论分析】信息熵的极值问题:什么时候最小?什么时候最大?
人工智能·深度学习
Z.风止3 小时前
Large Model-learning(3)
人工智能·笔记·后端·深度学习
春末的南方城市4 小时前
比肩顶尖闭源模型!京东开源240亿参数多模态模型JoyAI-Image:统一理解/生成/编辑,重塑AI图像编辑。
人工智能·深度学习·机器学习·计算机视觉·aigc
哥布林学者4 小时前
深度学习进阶(七)Data-efficient Image Transformer
机器学习·ai
做cv的小昊4 小时前
【TJU】应用统计学——第五周作业(3.1 假设检验的基本思想、3.2 单个正态总体参数的假设检验)
学习·线性代数·机器学习·数学建模·矩阵·概率论·tju
kyle-fang4 小时前
大模型微调
人工智能·深度学习·机器学习
EmmaXLZHONG4 小时前
Deep Learning With Pytorch Notes
人工智能·pytorch·深度学习
Master_oid4 小时前
机器学习38:距离度量与特征处理
人工智能·机器学习
龙文浩_4 小时前
AI NLP核心技术指南
人工智能·pytorch·深度学习·神经网络·自然语言处理