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.

相关推荐
智在碧得35 分钟前
碧服智能体进化:AI赋能意图识别能力,“一问”更智能
大数据·人工智能·机器学习
nap-joker1 小时前
FT-Mamba:一种高效的表回归的新深度学习模型
人工智能·深度学习·ftmamba
m0_372257021 小时前
bert和LLM训练的时候输入输出的格式是什么有什么区别
人工智能·深度学习·bert
杨夏同学2 小时前
AI入门——如何计算神经网络的参数
人工智能·深度学习·神经网络
龙文浩_2 小时前
AI中NLP的注意力机制的计算公式解析
人工智能·pytorch·深度学习·神经网络·自然语言处理
w_t_y_y2 小时前
模型和算法篇(一)监督学习回归问题(2)树形回归
机器学习
赵药师3 小时前
YOLO中task.py改复杂的模块
python·深度学习·yolo
Pelb3 小时前
求导 z = (x + y)^2
人工智能·深度学习·数学建模
断眉的派大星3 小时前
pytorch中链式法则求解梯度
人工智能·pytorch·python
技术小黑3 小时前
TensorFlow学习系列10 | 数据增强
python·深度学习·tensorflow2