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.

相关推荐
小吴同学·4 分钟前
吴恩达机器学习下:二分类问题、逻辑回归、过拟合问题
人工智能·机器学习
程序员Shawn6 分钟前
【机器学习 | 第四篇】- 逻辑回归
人工智能·机器学习·逻辑回归
tyler_download8 分钟前
揉扁搓圆Transformer架构: 激活函数说明
人工智能·深度学习·transformer
Flying pigs~~12 分钟前
基于TF_IDF和Bagging的文本分类全过程
算法·随机森林·机器学习·nlp·文本分类
STLearner1 小时前
AI论文速读 | 元认知监控赋能深度搜索:认知神经科学启发的分层优化框架
大数据·论文阅读·人工智能·python·深度学习·学习·机器学习
another heaven1 小时前
【深度学习 超参调优】optimizer=‘SGD‘ / ‘AUTO‘
人工智能·深度学习
2501_926978331 小时前
《与AI的妄想对话:如何给机器人造灵魂?》
人工智能·深度学习·机器学习·ai写作·agi
程序员Shawn1 小时前
【机器学习 | 第三篇】- 线性回归
人工智能·机器学习·线性回归
Master_oid2 小时前
机器学习36:机器学习概述
人工智能·机器学习
AI-Ming2 小时前
程序员转行学习 AI 大模型: 踩坑记录,HuggingFace镜像设置未生效
人工智能·pytorch·python·gpt·深度学习·学习·agi