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 小时前
使用python帮助艺术家完成角色动画和服装模型等任务
开发语言·图像处理·pytorch·python·opencv·ai作画
Vodka~1 小时前
深度学习——数据处理脚本(基于detectron2框架)
人工智能·windows·深度学习
lixy5792 小时前
深度学习之自动微分
人工智能·python·深度学习
神经星星2 小时前
【TVM教程】microTVM TFLite 指南
人工智能·机器学习·编程语言
SunsPlanter3 小时前
机器学习期末
人工智能·机器学习
吹风看太阳3 小时前
机器学习02——RNN
人工智能·rnn·机器学习
三道杠卷胡4 小时前
【AI News | 20250417】每日AI进展
人工智能·pytorch·python·语言模型·github
Tech Synapse4 小时前
基于OpenCV与PyTorch的智能相册分类器全栈实现教程
人工智能·pytorch·opencv
yukoyu4 小时前
PyTorch:学习 CIFAR-10 分类
pytorch·学习·分类
cosmos3154 小时前
深度学习进行网络流识别
深度学习·算法