Torch笔记

Torch笔记

  • 初始化很重要 切记 !不同的初始化产生的结果完全不同
  • relu函数可以解决sigmod函数梯度弥散的问题
  • tanh函数在卷积用的比较多
  • Leaky Relu 泄露的relu函数 使x<0时仍然具有梯度
  • SELU函数时两个函数的concat(不常用)
  • softplus同样是relu函数平滑的版本 在0处平滑(不常用)

如何防止Over fitting

  • 使用更多的数据

  • 控制模型复杂度

    • 使用更浅的模型

    • 正则化

      • L1 L2正则 lambda参数
      • 使用正则的前提是模型已经over fitting了
      optimizer = optim.SGD(net.parameters(),lr = learning_rate,weight_decay = 0.01)
      
  • Dropout

    torch.nn.Dropout(0.5) 将上一层数据减少一半传播
    
  • Data argumentation 数据增强

  • Early Stopping

优化器

  • momentum 动量 使用只需在优化器中添加momentum参数即可

    Adam优化器不需要添加动量 因为他自带

  • learnning rate decay 动态LR ReduceLROnPlateau(optimzer,'min')

代码笔记

device = torch.device('cuda:0')

net = MLP().to(device)  # 将网络切换到GPU上  原地更新

底层实现全连接

import torch
import troch.nn.Function as F
w1,b1 = torch.randn(200,784,requires_grad=True),torch.zeros(200,requires_grad=True)
w2,b2 = torch.randn(200,200,requires_grad=True),torch.zeros(200,requires_grad=True)
w3,b3 = torch.randn(200,784,requires_grad=True),torch.zeros(200,requires_grad=True)
def forward(x):
    x = x@w1.t()+b1
    x = F.relu(x)
    x = x@w2.t()+b2
    x = F.relu(x)
    x = x@w3.t()+b3
    x = F.relu(x)
    return x

    
posted @ 2019-08-15 22:26  FromZeroToOne  阅读(234)  评论(0编辑  收藏  举报