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.

相关推荐
埃菲尔铁塔_CV算法几秒前
深度学习驱动下的目标检测技术:原理、算法与应用创新(二)
深度学习·算法·目标检测
白杆杆红伞伞6 分钟前
04_决策树
算法·决策树·机器学习
KangkangLoveNLP43 分钟前
Llama:开源的急先锋
人工智能·深度学习·神经网络·算法·机器学习·自然语言处理·llama
久邦科技1 小时前
《Deepseek从入门到精通》清华大学中文pdf完整版
人工智能·深度学习·机器学习
来自于狂人1 小时前
给大模型“贴膏药”:LoRA微调原理说明书
人工智能·深度学习·transformer
tyatyatya2 小时前
MATLAB中进行深度学习网络训练的模型评估步骤
网络·深度学习·matlab
phoenix@Capricornus2 小时前
K均值(K-Means) & 高斯混合模型(GMM)——K均值是高斯混合模型的特例
机器学习·均值算法·kmeans
carpell3 小时前
【语义分割专栏】:FCN原理篇
人工智能·深度学习·计算机视觉·语义分割
东莞呵呵4 小时前
吴恩达机器学习(1)——机器学习算法分类
算法·机器学习·分类
小技工丨6 小时前
LLaMA-Factory:环境准备
机器学习·大模型·llama·llama-factory