Udacity Pytorch

https://www.youtube.com/watch?v=MswxJw-8PvE&list=PLaiC38QTRBdwq9MzAIrlThEBI2vIeT8hn

1.

1.1 使用矩阵乘法

 torch.mm() or torch.matmul()

一般使用前者,因为后者支持broadcasting,如果输入不正确会出现奇怪结果.

You can do the multiplication and sum in the same operation using a matrix multiplication. In general, you'll want to use matrix multiplications since they are more efficient and accelerated using modern libraries and high-performance computing on GPUs.

注意计算的时候,bias不要放到矩阵乘法里面去

 

1.2 inner product, 向量内积

1.3 torch.mul(a, b) 元素对应相乘 (element wise)

1.4 将numpu转为tensor: torch.from_numpy(),如果要使其求导,转化为torch.float型

 

1.5 RuntimeError: grad can be implicitly created only for scalar outputs

说明此时求backward是变量不是一个标量。https://discuss.pytorch.org/t/loss-backward-raises-error-grad-can-be-implicitly-created-only-for-scalar-outputs/12152 

1.6 torch.randn

https://pytorch.org/docs/stable/torch.html#torch.randn

1.8

What is in-place operation 

https://discuss.pytorch.org/t/what-is-in-place-operation/16244

Initialize the parameters with "tensor" and update them with "tensor.data" instead of in-place operation (in the past, people used the "variable" instead of tensor. but now the "variable" had been deprecated).

https://discuss.pytorch.org/t/leaf-variable-was-used-in-an-inplace-operation/308/3 

19. what is leaf variable

https://discuss.pytorch.org/t/leaf-variable-was-used-in-an-inplace-operation/308/3 

20. If the loss cannot converge, the reason maybe the learning rate is too large.

21. What is the retain_graph do in "loss.backward(retain_graph=True)" ?

Right now, a real use case is multi-task learning where you have multiple loss which maybe be at different layers. 

https://stackoverflow.com/questions/46774641/what-does-the-parameter-retain-graph-mean-in-the-variables-backward-method

https://oldpan.me/archives/pytorch-retain_graph-work

22. If the forward pass exist any "Nan", the gradient of the backward is also nan. And somtimes even if the loss is not nan, but in the backward process there will be nan.

https://discuss.pytorch.org/t/loss-is-not-nan-but-the-gradients-are/21224

23. Be careful with the denominator, something like vanishing.

24. torch gradient computation is descent in default, not ascent.

25. python中’*’和’**’的使用分两个方面,一个是计算,另一个是参数传递过程中元素的打包和解包。 (方便操作,但不是必要)

https://blog.csdn.net/qq_29343201/article/details/51600340 

 

比如在DDPG中init参数,输入uniform的upper bound 和lower bound可以用得到(将tuple中的元素逐个输入)

2. 通常使用tensor.view( x,y )来改变tensor的形状

(少使用reshape或者resize_的原因是前者可能是copy而不是直接共享地址,后者当输入错误形状的时候不会报错,而是变成一个损失数据或者补充了假数据的矩阵)

3. numpy和pytorch数据之间的互相转换 (注意,转换后数据是共享的)

The memory is shared between the Numpy array and Torch tensor, so if you change the values in-place of one object, the other will change as well.

 

4. dim =0 按列求和,  dim =1 按行求和

5. flatten 扁平化处理

通过view来实现,除了指定的行数外,其余相乘化为一行(用-1表示).  

 

6. part 2 有教如何单独初始化w和b为指定数值 (但在第17有更加方便初始化weight和bias的方法)

 

7. 制作一个网络及其训练最简洁代码

左边loss的类型有:  CrossEntropy Loss, Mean Square Error, 

左边optimizer的更新方式有:  SGD, ADAM

 

8. 当不训练的时候, 即用来test的时候,关掉可导功能可以加速

 

 

9. 用"=="比较两个tensor的时候,如果维度不一致,会造成遍历比较.  可以用view(* arg)的方法取得一致的维度 (part 5)

10. 

由于equal符号==返回的是byteTensor类型,因此需要转化为float型才可以进行运算操作

 

11. 利用validation test和dropout来防止过拟合(part 5)

train的时候:  model_train()

 

test的时候:         with torch.no_grad():
                                        model.eval()

 

12. 记录训练好的神经网路的参数(part6)

网络的参数存在state_dict之中.

                  3-5行建立一个和传入的网络结构一致的网络

                  6行传入传入网络参数

 

13. 加载数据,  数据增强 (data augmentation)

通过旋转/截取/翻转训练集,使得训练集更加丰富,因此使得模型更加具有泛化能力.

此外,由于三色通道数值过大,可以化为-1 到1之间, 思路和bike sharing相似,在这里只需要用transforms.Normalize传入mean和std即可.

 

15. 加载images

如果在torchvision的dataset库里面已经有相应的images,可以像如下导入 (Cifar_cnn)

 

若要在本地导入,则(Transfer_Learning_Exercise):

 

16. 当要固定某层layer的参数,可以用requires_grad和在optim中设置来完成

requires_grad使得指定的参数不用求导

optim只更新(update)指定的参数

17. torch中各种初始化weight的方法:https://pytorch.org/docs/stable/nn.html#torch-nn-init  

 

18. tensor 的类型转换

 

numpy -> tensor -> tensor.float

tensor的其他数据类型

https://blog.csdn.net/g11d111/article/details/80896137

 

19. tensor.detach()

当我们训练完得到tensor类型的数据之后,我们会用detach再保存数据,因为tensor原本还包含着grad_graph图,会占用内存。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值