pytorch注意事项和学习笔记

9 篇文章 0 订阅
4 篇文章 0 订阅

pytorch注意事项和学习笔记

查看torch版本:torch.__version__
查看cuda版本:torch.version.cuda
torch.cuda.is_available() # 判断gpu是否可用

tensor和numpy相互转化

torch.from_numpy(np_data)
torch_data.numpy()


CPU和GPU的Tensor之间转换

cpu2gpu:data.cuda()
gpu2cpu:data.cpu()


设置随机种子复现实验结果

(1) Pytorch

torch.manual_seed(seed)       #为CPU设置随机种子
torch.cuda.manual_seed(seed)  #为当前GPU设置随机种子
torch.cuda.manual_seed_all(seed)  #为所有GPU设置随机种子

(2)Python & Numpy

random.seed(seed)
np.random.seed(seed)

(3)CUDNN
cudnn中对卷积操作进行了优化,牺牲了精度来换取计算效率。如果需要保证可重复性,可以使用如下设置:

from torch.backends import cudnn
cudnn.benchmark = False  # if benchmark=True, deterministic will be False
cudnn.deterministic = True

用gpu训练模型:
device = torch.device("cuda:id" if torch.cuda.is_available() else "cpu")
# single-gpu training
model.to(device)

# multi-gpu training
model = nn.DataParallel(model) 
model.to(device)

# data processing
data.to(device)
data.to(device=id)

momentum parameter:相关博客


torch.max

torch.max(a) # returns the maximum value of all elements
torch.max(a, 0) # returns the maximum value of each column and corresponding index
torch.max(a, 1) # returns the maximum value of each row and corresponding index


squeeze & unsqueeze

torch.squeeze(input, dim=None, out=None) → \to Tensor
→ \to dim:if given, the input will be squeezed only in this dimension
torch.unsqueeze(input, dim, out=None) → \to Tensor
→ \to Returns a new tensor with a dimension of size one inserted at the specified position


模型保存与加载
# save the whole network
torch.save(model, 'model.pth')
model = torch.load('model.pth')

# only save the parameters of network(recommend)
torch.save(model.state_dict(), 'params.pth')
model.load_state_dict(torch.load('params.pth'))

GPU和CPU模型相互加载


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值