测试TensorFlow/PyTorch的GPU版本是否启用

后续遇到好的会不断更新。。。


1. Pytorch测试代码

import torch
def gpu_is_available():
    print('\nGPU details:')
    print(f'    gpu_is_available      : ', torch.cuda.is_available())
    print(f'    cuda_device_count     : ', torch.cuda.device_count())
    print(f'    cuda_device_name      : ', torch.cuda.get_device_name())
    print(f'    cuda_device_capability: ', torch.cuda.get_device_capability(0))
gpu_is_available()

来源:“PyTorch快速安装并验证GPU是否可用

#测试pytorch-gpu是否能用
import torch
flag = torch.cuda.is_available()
print(flag)
ngpu= 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print('cuda设备名:',device)
print('gpu名称:',torch.cuda.get_device_name(0))
print('pytorch版本:',torch.__version__)
print('cuda版本:',torch.version.cuda)
print('cudnn版本号:',torch.backends.cudnn.version())
print('定义一个torch格式的3*3的矩阵:',torch.rand(3,3).cuda())

来源:“如何测试pytorch-gpu版本和tensorflow-gpu版本是否安装成功

import torch
# 使用GPU训练
if not torch.cuda.is_available():
    print('CUDA is not available.  Training on CPU ...')
else:
    print('CUDA is available.  Training on GPU ...')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

来源:“深入学习之anaconda、pytorch、cuda安装

#coding=gbk
import torch

# 定义张量的形状和大小
shape = (100, 1000)
num_tensors = 50000

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
data = [torch.rand(shape, device=device) for _ in range(num_tensors)]

total_sum = torch.tensor([0.0])
for tensor in data:
    total_sum += tensor.sum().cpu()

print('Total sum:', total_sum.item())

来源:“测试pytorch-gpu

2. TensorFlow测试代码

#测试tensorflow-gpu是否能用
import tensorflow as tf
print('\n\nGPU',tf.config.list_physical_devices('GPU'))
a = tf.constant(2.)
b = tf.constant(4.)
print('打印a*b:',a * b)
print("tensorflow版本:", tf.__version__)

来源:“如何测试pytorch-gpu版本和tensorflow-gpu版本是否安装成功

import tensorflow as tf
print(tf.test.is_gpu_available())

来源:“检测安装Tensorflow后是否成功调用GPU

相关推荐

  1. 测试TensorFlow/PyTorchGPU版本是否启用

    2023-12-20 11:34:05       75 阅读
  2. 测试pytorch(GPU)安装,并输出版本

    2023-12-20 11:34:05       59 阅读
  3. 阿里新开源GPU版本FunASR安装避坑

    2023-12-20 11:34:05       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-20 11:34:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-20 11:34:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-20 11:34:05       82 阅读
  4. Python语言-面向对象

    2023-12-20 11:34:05       91 阅读

热门阅读

  1. Jenkins在window下配置Android打包配置

    2023-12-20 11:34:05       58 阅读
  2. 云安装nginx

    2023-12-20 11:34:05       54 阅读
  3. uniapp request.js封装例子

    2023-12-20 11:34:05       51 阅读
  4. UI Grounding 学习笔记

    2023-12-20 11:34:05       59 阅读
  5. 6.如何做项目技术选型

    2023-12-20 11:34:05       56 阅读
  6. CentOS 8.2 安装 nginx-1.18.0

    2023-12-20 11:34:05       64 阅读
  7. rpc和消息队列区别

    2023-12-20 11:34:05       51 阅读
  8. js对象转换为excel,excel转换为js对象

    2023-12-20 11:34:05       64 阅读
  9. MATLAB 平面拟合并旋转到水平面 (43)

    2023-12-20 11:34:05       52 阅读