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.

相关推荐
小牛头#5 小时前
clickhouse 各个引擎适用的场景
大数据·clickhouse·机器学习
kngines9 小时前
【力扣(LeetCode)】数据挖掘面试题0002:当面对实时数据流时您如何设计和实现机器学习模型?
机器学习·数据挖掘·面试题·实时数据
FL16238631299 小时前
如何使用目标检测深度学习框架yolov8训练钢管管道表面缺陷VOC+YOLO格式1159张3类别的检测数据集步骤和流程
深度学习·yolo·目标检测
网安INF10 小时前
深度学习中批标准化与神经网络调优
人工智能·深度学习·神经网络·机器学习
归去_来兮11 小时前
Transformer模型原理概述
人工智能·深度学习·transformer
爱思德学术12 小时前
中国计算机学会(CCF)推荐学术会议-B(计算机体系结构/并行与分布计算/存储系统):SOCC 2025
网络协议·机器学习·云计算·边缘计算
酌沧12 小时前
深度学习 必然用到的 微积分知识
人工智能·深度学习
巴伦是只猫12 小时前
【机器学习笔记 Ⅲ】1 无监督学习
笔记·学习·机器学习
码字的字节12 小时前
深入理解Transformer架构:从理论到实践
深度学习·架构·transformer
蓝婷儿12 小时前
Python 机器学习核心入门与实战进阶 Day 7 - 复盘 + 综合实战挑战
python·机器学习