
Pytorch
HJC256ZY
这个作者很懒,什么都没留下…
展开
-
Pytorch 参数管理 torch.nn.parameter
Pytorch 参数管理import torchimport torch.nn as nnfrom torch.nn.parameter import Parameterclass model(torch.nn.Module): def __init__(self,in_put,h,out_put): '''自定义模型的初始化''' super(model,self).__init__() self.linear1=torch.nn.Lin原创 2020-06-08 19:22:01 · 1123 阅读 · 0 评论 -
Pytorch基础知识 一
PyTorch AutogradTensor是一种数据结构,类似于numpy数组,与numpy不同点,可以使用GPU计算。使用 a.is_leaf 来判断a 是否是Tensor import torchtsr = torch.Tensor(2,3)>>> tensor([[9.5510e-39, 1.0010e-38, 8.4490e-39], [9.6429e-39, 8.4490e-39, 1.0561e-38]])requires_grad属性Te原创 2020-06-03 10:58:24 · 320 阅读 · 0 评论 -
nn.ReLU(inplace=True)
nn.ReLU(inplace=True),默认是False不管是true 还是False 都不会改变Relu后的结果。inplace=True利用in-place计算可以节省内(显)存,同时还可以省去反复申请和释放内存的时间import torchfrom torch import nn as nnm = nn.ReLU(inplace=True)n =nn.ReLU(inplace=False)input = torch.randn(10)print(input)print(m(原创 2020-06-03 10:09:03 · 2719 阅读 · 0 评论 -
Pytorch GPU 有关的一些收集
torch.cuda.is_available() #检查Pytorch GPU 是否可用torch.cuda.device_count() #返回gpu的数量torch.cuda.current_device() #返回当前使用的GPU 的索引torch.cuda.get_device_name() #返回GPU的名字,从索引0开始torch.cuda.is_initialized()返回PyTorch的CUDA状态是否已初始化torch.cuda.empty_cache()释原创 2020-06-03 09:54:49 · 4843 阅读 · 1 评论 -
Pytorch 模型中nn.Model 中的forward() 前向传播不调用 解释
在pytorch 中没有调用模型的forward()前向传播,只实列化后把参数传入。定义模型class Module(nn.Module): def __init__(self): super(Module, self).__init__() # ...... def forward(self, x): # ...... return xdata = ..... #输入数据# 实例化一个对象module =原创 2020-05-31 22:23:58 · 8795 阅读 · 1 评论 -
Pytorch 保存 加载模型torch.save() torch.load()
Pytorch 保存和加载模型Pytorch 保存和加载模型后缀:.pt 和.pth保存整个模型:torch.save(model,'save.pt')只保存训练好的权重:torch.save(model.state_dict(), 'save.pt')加载模型:pretrained_dict = torch.load("save.pt")只加载模型参数:model.load_state_dict(torch.load("save.pt")) #model.load_state_d原创 2020-05-31 16:46:39 · 1771 阅读 · 0 评论 -
Pytorch 多GPU训练
PyTorch数据并行:nn.DataParallel 一主机多GPUDistributedParallel 多主机多GPUnet = torch.nn.DataParallel(model)默认所有存在的显卡都会被使用如果我们机子中有很多显卡(例如我们有5张显卡),但我们只想使用0、1、2号显卡net = torch.nn.DataParallel(model, device_ids=[0, 1, 2])...原创 2020-05-31 10:51:09 · 207 阅读 · 0 评论 -
使用torchsummary打印torch模型的每层形状参数
torchsummary打印模型的每层形状参数import torchimport torchvisionfrom torchsummary import summary #使用 pip install torchsummarydevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')vgg = torchvision.models.vgg16().to(device)summary(vgg, in原创 2020-05-29 20:51:20 · 2801 阅读 · 1 评论