权重衰退简洁实现

def train_concise(wd):
    net = nn.Sequential(nn.Linear(num_inputs, 1))
    
    for param in net.parameters():
        param.data.normal_()
    
    loss = nn.MSELoss()
    num_epochs, lr = 100, 0.003
    
    # 随机梯度下降
    trainer = torch.optim.SGD([{
        "params": net[0].weight,
        
        # weight_decay:就是那个超参数
        'weight_decay': wd}, {
            "params": net[0].bias}], lr=lr)
    
    animator = d2l.Animator(xlabel='epochs', ylabel='loss', yscale='log',
                            xlim=[5, num_epochs], legend=['train', 'test'])
    
    for epoch in range(num_epochs):
        for X, y in train_iter:
            with torch.enable_grad():
                trainer.zero_grad()
                l = loss(net(X), y)
            l.backward()
            trainer.step()
        if (epoch + 1) % 5 == 0:
            animator.add(epoch + 1, (d2l.evaluate_loss(net, train_iter, loss),
                                     d2l.evaluate_loss(net, test_iter, loss)))
    print('w的L2范数:', net[0].weight.norm().item())

  

原文地址:https://www.cnblogs.com/xiaoqing-ing/p/15072454.html