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.

相关推荐
Luis Li 的猫猫2 小时前
深度学习中的知识蒸馏
人工智能·经验分享·深度学习·学习·算法
带娃的IT创业者3 小时前
机器学习实战(6):支持向量机(SVM)——强大的非线性分类器
算法·机器学习·支持向量机
木觞清4 小时前
PyTorch与TensorFlow的对比:哪个框架更适合你的项目?
人工智能·pytorch·tensorflow
wyg_0311137 小时前
用deepseek学大模型04-模型可视化与数据可视化
人工智能·机器学习·信息可视化
盼小辉丶8 小时前
TensorFlow深度学习实战(8)——卷积神经网络
深度学习·cnn·tensorflow
Donvink9 小时前
【复现DeepSeek-R1之Open R1实战】系列5:SFT源码逐行深度解析
人工智能·深度学习·语言模型·transformer
好评笔记9 小时前
深度学习笔记——循环神经网络之LSTM
笔记·rnn·深度学习
eso198311 小时前
浅谈DNN(深度神经网络)算法原理
深度学习·算法·dnn
白-胖-子11 小时前
DeepSeek系统架构的逐层分类拆解分析,从底层基础设施到用户端分发全链路
人工智能·机器学习·分类·数据挖掘·系统架构·agi·deepseek
taoqick12 小时前
nn.EmbeddingBag把offsets之间的进行归约,offsets从0开始
pytorch·python·深度学习