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.

相关推荐
塔楼3 小时前
MiniCPM-V 4.5
人工智能·深度学习
猫天意3 小时前
【即插即用模块】AAAI2025 | 高频 + 空间感知!新 HS-FPN 让“极小目标”不再消失!SCI保二区争一区!彻底疯狂!!!
网络·人工智能·深度学习·学习·音视频
这张生成的图像能检测吗3 小时前
Wonder3D: 跨域扩散的单图像3D重建技术
pytorch·深度学习·机器学习·计算机视觉·3d·三维重建·扩散模型
小孟的CDN3 小时前
使用pytorch进行batch_size分批训练,并使用adam+lbfgs算法——波士顿房价预测
pytorch·算法·batch·代码·adam+lbfgs
爱ZW的小白猿3 小时前
【pytorch】tensorboard的使用
pytorch
AI科技星4 小时前
质量定义方程的物理数学融合与求导验证
数据结构·人工智能·算法·机器学习·重构
deephub4 小时前
llama.cpp Server 引入路由模式:多模型热切换与进程隔离机制详解
人工智能·python·深度学习·llama
wm10434 小时前
机器学习课程day01 机器学习概述
人工智能·机器学习
桓峰基因4 小时前
SCS 60.单细胞空间转录组空间聚类(SPATA2)
人工智能·算法·机器学习·数据挖掘·聚类
Axis tech4 小时前
MANUS数据手套弥合灵巧手训练中虚拟模拟与现实应用的差距
人工智能·深度学习