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.

相关推荐
不会学习的小白O^O4 小时前
双通道深度学习框架可实现从无人机激光雷达点云中提取橡胶树冠
人工智能·深度学习·无人机
丁浩6665 小时前
Python机器学习---6.集成学习与随机森林
python·随机森林·机器学习
救救孩子把6 小时前
30-机器学习与大模型开发数学教程-3-4 矩阵的逆与伪逆
线性代数·机器学习·矩阵
Coovally AI模型快速验证7 小时前
OmniNWM:突破自动驾驶世界模型三大瓶颈,全景多模态仿真新标杆(附代码地址)
人工智能·深度学习·机器学习·计算机视觉·自动驾驶·transformer
青春不败 177-3266-05208 小时前
GPT、DeepSeek等大语言模型应用
人工智能·gpt·深度学习·机器学习·语言模型·科研绘图
渡我白衣8 小时前
C++ 同名全局变量:当符号在链接器中“相遇”
开发语言·c++·人工智能·深度学习·microsoft·语言模型·人机交互
java1234_小锋10 小时前
PyTorch2 Python深度学习 - PyTorch2安装与环境配置
开发语言·python·深度学习·pytorch2
CClaris10 小时前
深度学习——反向传播的本质
人工智能·python·深度学习
哲此一生98410 小时前
YOLO11追踪简单应用
人工智能·pytorch·深度学习
双翌视觉11 小时前
机器视觉的手机模组背光贴合应用
人工智能·机器学习·智能手机·1024程序员节