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.

相关推荐
小超同学你好11 分钟前
Transformer 22. Gemma 1 架构详解:Decoder-only、GeGLU、RoPE 与每一步计算
人工智能·深度学习·transformer
枫叶林FYL2 小时前
第 7 章 感知不确定性估计
人工智能·机器学习
沪漂阿龙2 小时前
从感知机到GPT:一个1957年的“神经元”如何引爆2026年的AI革命?
人工智能·gpt·机器学习
fof9202 小时前
Base LLM | 从 NLP 到 LLM 的算法全栈教程 第七天
人工智能·深度学习
龙文浩_3 小时前
AI深度学习/PyTorch/反向传播与梯度下降
人工智能·pytorch·深度学习
独隅3 小时前
Keras 全面介绍:从入门到实践
人工智能·深度学习·keras
cyyt3 小时前
深度学习周报(3.30~4.5)
人工智能·深度学习
qq_571099354 小时前
学习周报三十九
人工智能·深度学习·机器学习
陈天伟教授4 小时前
小白快速进阶- AI辅助编码
人工智能·神经网络·机器学习·量子计算
AI医影跨模态组学4 小时前
Radiology子刊(IF=6.3)复旦大学附属金山医院强金伟教授等团队:基于多参数MRI的深度学习和影像组学评估早期宫颈癌淋巴结转移
人工智能·深度学习·论文·医学·医学影像