• PyTorch之Optimizer


    #!/usr/bin/env python2
    # -*- coding: utf-8 -*-
    import torch
    import torch.utils.data as Data
    import torch.nn.functional as F
    from torch.autograd import Variable
    import matplotlib.pyplot as plt
    
    torch.manual_seed(1)    
    LR = 0.01
    BATCH_SIZE = 32
    EPOCH = 12
    
    # 创造的训练数据
    x = torch.unsqueeze(torch.linspace(-1, 1, 1000), dim=1)
    y = x.pow(2) + 0.1*torch.normal(torch.zeros(*x.size()))
    
    plt.scatter(x.numpy(), y.numpy())
    plt.show()
    
    # 使用上节内容提到的 data loader
    torch_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
    loader = Data.DataLoader(dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2,)
    
    # 默认的 network 形式
    class Net(torch.nn.Module):
        def __init__(self):
            super(Net, self).__init__()
            self.hidden = torch.nn.Linear(1, 20)   # hidden layer
            self.predict = torch.nn.Linear(20, 1)   # output layer
    
        def forward(self, x):
            x = F.relu(self.hidden(x))      # activation function for hidden layer
            x = self.predict(x)             # linear output
            return x
    
    # 为每个优化器创建一个 net
    net_SGD         = Net()
    net_Momentum    = Net()
    net_RMSprop     = Net()
    net_Adam        = Net()
    nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam]
    
    #不同的优化器
    opt_SGD         = torch.optim.SGD(net_SGD.parameters(), lr=LR)
    opt_Momentum    = torch.optim.SGD(net_Momentum.parameters(), lr=LR, momentum=0.8)
    opt_RMSprop     = torch.optim.RMSprop(net_RMSprop.parameters(), lr=LR, alpha=0.9)
    opt_Adam        = torch.optim.Adam(net_Adam.parameters(), lr=LR, betas=(0.9, 0.99))
    optimizers = [opt_SGD, opt_Momentum, opt_RMSprop, opt_Adam]
    
    loss_func = torch.nn.MSELoss()
    losses_his = [[], [], [], []]   # 记录 training 时不同神经网络的 loss
    
    for epoch in range(EPOCH):
        print('Epoch: ', epoch)
        for step, (batch_x, batch_y) in enumerate(loader):
            b_x = Variable(batch_x)  # 务必要用 Variable 包一下
            b_y = Variable(batch_y)
    
            # 对每个优化器, 优化属于他的神经网络
            for net, opt, l_his in zip(nets, optimizers, losses_his):
                output = net(b_x)              # get output for every net
                loss = loss_func(output, b_y)  # compute loss for every net
                opt.zero_grad()                # clear gradients for next train
                loss.backward()                # backpropagation, compute gradients
                opt.step()                     # apply gradients
                l_his.append(loss.data[0])     # loss recoder

     下图是各优化器的优化效率对比:

  • 相关阅读:
    每位设计师都应该拥有的50个CSS代码片段(下)
    每位设计师都应该拥有的50个CSS代码片段(上)
    canvas基础——绘制简单的图形
    HTML5——canvas基础
    link和@import的有何区别应如何选择
    火焰灯效果导航菜单
    简单的 Jquery 下拉菜单Demo
    git多人协作冲突解决方法
    搭建iis本地测试服务器
    HTML5 <template>
  • 原文地址:https://www.cnblogs.com/xmeo/p/7260545.html
Copyright © 2020-2023  润新知