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.

相关推荐
weifengma-wish1 小时前
Transformer 注意力为什么用内积? 而不用余弦相识度和cor等
人工智能·深度学习·transformer
AndrewHZ2 小时前
【LLM技术全景】预训练与微调:大模型如何“学习“
人工智能·深度学习·大模型·llm·微调·预训练·rlhf
DeniuHe4 小时前
深度学习中的MLP层
人工智能·深度学习
救救孩子把5 小时前
88-机器学习与大模型开发数学教程-8-6 矩阵分解与低秩近似在推荐系统中的应用
人工智能·机器学习·矩阵
冰西瓜6005 小时前
深度学习的数学原理(四十二)—— 分布式训练
人工智能·分布式·深度学习
CJH(本人账号)5 小时前
【AI安全】大模型安全威胁:Prompt注入与模型防御策略
人工智能·安全·机器学习·语言模型·云计算·prompt
moMo6 小时前
# 不懂机器学习也能玩 NLP?用DeepSeek 跑通四种经典任务
人工智能·机器学习