栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch使用GPU

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch使用GPU

import torch
from torch import nn
判断是否可用GPU
torch.cuda.is_available()

True 可用 False不可用

torch.cuda.device_count() #查询有几个GPU可用

定义使用GPU和CPU的函数

def try_gpu(i=0):  #@save
    """如果存在,则返回gpu(i),否则返回cpu()"""
    if torch.cuda.device_count() >= i + 1:
        return torch.device(f'cuda:{i}')
    return torch.device('cpu')

def try_all_gpus():  #@save
    """返回所有可用的GPU,如果没有GPU,则返回[cpu(),]"""
    devices = [torch.device(f'cuda:{i}')
             for i in range(torch.cuda.device_count())]
    return devices if devices else [torch.device('cpu')]

try_gpu(), try_gpu(10), try_all_gpus()
神经网络在GPU上运行
net = nn.Sequential(nn.Linear(3,1))
net = net.to(device = try_gpu())  # net.to() 选择gpu
net(X)
net[0].weight.data.device  #查看模型参数存贮在那个位置
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号