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.

相关推荐
Frank学习路上1 小时前
【AI技能】跟着费曼学自动驾驶
人工智能·机器学习·自动驾驶
bryant_meng1 小时前
【Reading Notes】(4)Favorite Articles from 2021
人工智能·深度学习·业界资讯
清空mega2 小时前
动手学深度学习——多尺度锚框
人工智能·深度学习·目标跟踪
克里斯蒂亚诺·罗纳尔达2 小时前
智能体学习16——学习与适应(Learning-and-Adaptation)-深入解读
深度学习·学习·机器学习
call me by ur name2 小时前
ERNIE 5.0 Technical Report论文解读
android·开发语言·人工智能·机器学习·ai·kotlin
冰西瓜6002 小时前
深度学习的数学原理(二十六)—— 多头注意力
人工智能·深度学习
lisw053 小时前
《计算机辅助设计与图形学学报》分析评介!
人工智能·机器学习
憨波个3 小时前
【说话人日志】从 LSTM attractor 到 Transformer attractor:EEND-TA
人工智能·深度学习·lstm·transformer·音频·语音识别
这张生成的图像能检测吗3 小时前
(论文速读)基于知识图谱构建的大型工业设备故障诊断模型
人工智能·深度学习·知识图谱·故障诊断
ccLianLian3 小时前
深度学习基础·损失函数
人工智能·深度学习