总结在实际工作中遇到的关于PyTorch框架的一些API以及框架疑问,供未来查表
文章目录
- 1. resume与finetune的区别?
- 2. train_loader.sampler.set_epoch(epoch) 的意义?
- 3. torch.nn.parallel.DistributedDataParallel 分布式训练以及local_rank的意义?
- 4. torch.cuda.manual_seed与torch.cuda.manual_seed_all的区别?
- 5. torch.cuda.empty_cache()的作用
- 6. 解释load_state_dict
- 7. model.train()和model.eval()用法和区别
- 8. model.eval()和torch.no_grad()的区别
- 9. lr = self.optimizer.param_groups[0]['lr']
- 10. self.optimizer.zero_grad()
- 11. torch.cuda.empty_cache()
- 12. nn.BCELoss()
- 13. AverageMeter()的作用与用法
- 14. detach()的作用与用法
1. resume与finetune的区别?
2. train_loader.sampler.set_epoch(epoch) 的意义?
3. torch.nn.parallel.DistributedDataParallel 分布式训练以及local_rank的意义?
4. torch.cuda.manual_seed与torch.cuda.manual_seed_all的区别?
5. torch.cuda.empty_cache()的作用
6. 解释load_state_dict
7. model.train()和model.eval()用法和区别
8. model.eval()和torch.no_grad()的区别
https://blog.csdn.net/qq_38410428/article/details/101102075
9. lr = self.optimizer.param_groups[0][‘lr’]
10. self.optimizer.zero_grad()
11. torch.cuda.empty_cache()
12. nn.BCELoss()
12.1 官方定义
https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html#torch.nn.BCELoss
12.2 参数说明
12.3 示例
>>> m = nn.Sigmoid()
>>> loss = nn.BCELoss()
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> output = loss(m(input), target)
>>> output.backward()
12.4 注意事项
- 先初始化该类,然后再进行调用
loss = nn.BCELoss()(pred,gt)
或者
criterion = nn.BCELoss()
loss = criterion(pred,gt)
下面这种写法,是错误的:loss = nn.BCELoss()(pred,gt)
会报RuntimeError: Boolean value of Tensor with more than one value is ambiguous的错误,因为nn.BCELoss是一个class
- 常与sigmoid结合使用
- 注意默认reduction为mean
- BCE对log取infinite值(log0),有一个截断操作,让其>=-100
pred = torch.Tensor([0])
batch = torch.Tensor([1])
loss(pred,batch)
#输出100