【PyTorch】学习笔记

1. 查看PyTorch版本

import torch
print(torch.__version__)

2. 模型参数量

model = FPN()
num_params = sum(p.numel() for p in model.parameters())
print("num of params: {:.2f}k".format(num_params/1000.0))
# torch.numel()返回tensor的元素数目,即number of elements
# Returns the total number of elements in the input tensor.

3. 打印模型

model = FPN()
num_params = sum(p.numel() for p in model.parameters())
print("num of params: {:.2f}k".format(num_params/1000.0))
print("===========================")
#for p in model.parameters():
#    print(p.name)
print(model)

4. with torch.no_grad()

with torch.no_grad():
    # code no gradient Here!

5. ReLU激活函数

6. torch.nn.Sequential

7. 模型保存&&加载

https://zhuanlan.zhihu.com/p/76604532

8. 调整学习率 How to adjust learning rate


其实Optimizer也可以打印学习率

9. nn.ModuleList

10. Tensor.detach()

Returns a new Tensor, detached from the current graph. # 返回一个新的Tensor,从现有的图中分离开来
The result will never require gradient.  # 返回的Tensor不需要梯度信息

11. 二维卷积torch.nn.Conv2d

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0,
dilation=1, groups=1, bias=True, padding_mode='zeros')

12. torch.nn.Unfold()

13. 打印网络架构

14. 固定模型的部分参数

15. torch.unbind

Removes a tensor dimension
返回一个tuple

16. torch.clamp

关于batch_size和num_epochs设置的问题

https://stackoverflow.com/questions/35050753/how-big-should-batch-size-and-number-of-epochs-be-when-fitting-a-model-in-keras

17. PyTorch数据类型转换

PyTorch中数据类型详见👉 torch-tensor
自从xx版本以后,没有Boolean类型的数据变量,转为torch.uint8类型,mask的时候会涉及到

18. 防止Loss为NaN

  • sqrt(s^2+0.001^2) Robust 防止s-->0 时无法反向传播
  • 1/(x+1e-8) 在分母处加一个很小的数值

19. torch.from_numpy

记得copy(), 否则会报错

20. 节省显存

  • Replace BN+ReLU with Inplace-ABN
  • 另外BN与GN的关系和区别???
posted @ 2020-09-29 09:39  达可奈特  阅读(363)  评论(0编辑  收藏  举报