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.

相关推荐
狮子座明仔39 分钟前
体验式强化学习:让模型学会“吃一堑长一智“
人工智能·深度学习·自然语言处理
癫狂的兔子2 小时前
【Python】【机器学习】支持向量积
python·机器学习
童园管理札记2 小时前
【记录模板】大班科学小游戏观察记录(盐主题:《会变魔术的盐》)
经验分享·深度学习·职场和发展·学习方法·微信公众平台
侧岭灵风3 小时前
人工智能各名词解释
人工智能·机器学习
癫狂的兔子4 小时前
【Python】【机器学习】贝叶斯算法
python·机器学习
CelestialYuxin4 小时前
A.R.I.S.系统:YOLOx在破碎电子废料分拣中的新探索
人工智能·深度学习·算法
Flying pigs~~5 小时前
机器学习之KNN算法
算法·机器学习·大模型·knn·k近邻算法·大数据处理
Asher阿舍技术站5 小时前
【AI基础学习系列】八、机器学习常见名词汇总
人工智能·学习·机器学习·常见名词
勾股导航5 小时前
蚁群优化算法
人工智能·pytorch·python
陈天伟教授6 小时前
人工智能应用- 材料微观:04.微观结构:金属疲劳
人工智能·神经网络·算法·机器学习·推荐算法