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.

相关推荐
工业机器视觉设计和实现20 分钟前
动量的好处与困扰(二,摸到pytorch尾灯!)
人工智能·pytorch·cudnn微积分
-拟墨画扇-1 小时前
神经网络 | ⑬ 超参数验证:找到最佳学习超参数
深度学习·神经网络·学习
未知违规用户3 小时前
大模型项目:RAG项目实战与FlagEmbedding模型
人工智能·windows·python·深度学习
李燚9 小时前
RAG 流水线设计:Eino 的 Loader → Transformer → Indexer → Retriever(第60篇-E46)
人工智能·深度学习·transformer·agent·rag·aiagent·eino
小小仙子14 小时前
矢量网络分析仪如何测试S参数的?
人工智能·算法·机器学习
hoLzwEge15 小时前
CLAUDE.md:为 Claude Code 注入项目记忆
深度学习·程序员·代码规范
金斗潼关17 小时前
使用MLP神经网络模型预测质数
人工智能·深度学习·神经网络
深圳佛手18 小时前
梁文锋说,AGI是首要目标。AGI和AI的区别是什么?
人工智能·机器学习