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.

相关推荐
爱炼丹的James11 分钟前
目标检测任务
人工智能·深度学习
春末的南方城市15 分钟前
消费级显卡迎来实时视频生成!FastVideo 开源 FastWan-QAD,RTX 5090 实现 1.8 秒生成 5 秒 480P 视频!
人工智能·深度学习·计算机视觉·aigc·音视频
@LiX22 分钟前
深度学习基本原理
人工智能·深度学习
其美杰布-富贵-李30 分钟前
Loss Functions:Logistic Loss、Cross Entropy 与 Hinge Loss
深度学习·损失函数
Eloudy1 小时前
一键构建 pytorch cpp lib 前端和 wheel
人工智能·pytorch·gpu
过期的秋刀鱼!2 小时前
使用都热编码的分类特征
人工智能·算法·决策树·机器学习·分类·数据挖掘
FriendshipT3 小时前
Ultralytics:解读 YOLO26 知识蒸馏
人工智能·pytorch·python·深度学习·yolo
watersink3 小时前
机器学习FM、FFM
人工智能·机器学习
eric-sjq13 小时前
10 分钟实战:用 0.6B 本地模型生成中文网页(WanlyFrontend + 编译器全流程)
javascript·机器学习·自然语言处理·html