美文网首页
pytorch线性回归(构建模型流程)

pytorch线性回归(构建模型流程)

作者: 英文名字叫dawntown | 来源:发表于2020-02-12 01:22 被阅读0次

    先贴个笔记:
    均方损失函数,注意\frac{1}{2}
    L(\mathbf{w}, b) =\frac{1}{n}\sum_{i=1}^n l^{(i)}(\mathbf{w}, b) =\frac{1}{n} \sum_{i=1}^n \frac{1}{2}\left(\mathbf{w}^\top \mathbf{x}^{(i)} + b - y^{(i)}\right)^2.
    SGD更新参数
    (\mathbf{w},b) \leftarrow (\mathbf{w},b) - \frac{\eta}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} \partial_{(\mathbf{w},b)} l^{(i)}(\mathbf{w},b)
    注意\frac{1}{2}

    1.构建模型

    设计网络的结构,pytorch有方便的模块和函数可供使用,例如nn.Modulenn.Sequentialnn.Linear

    # 使用类构建
    class LinearNet(nn.Module):
        def __init__(self, n_feature):
            super(LinearNet, self).__init__()      # call father function to init 
            self.linear = nn.Linear(n_feature, 1)  # function prototype: `torch.nn.Linear(in_features, out_features, bias=True)`
            '''
            # method 1
            self.linear = nn.Sequential(
                            nn.Linear(n_feature, 1)
                            # nn.Linear(...)
                    )
    
            # method 2
            self.linear = nn.Sequential()
            self.linear.add_model('linear', nn.Linear(n_feature, 1))
             # self.linear.add_model(...)
    
            # method3
            from collections import OrderedDict # 生成有序词典
            self.linear = nn.Sequential(OrderedDict([
                    ('linear', nn.Linear(n_feature, 1)),
                    # (...)
                ]))
            '''
        def forward(self, x):
            y = self.linear(x)
            return y
    

    其中的__init__实现是可变的,可以通过nn.Sequential加入多层神经元,例如以上注释掉的部分

    1.1初始化模型参数

    from torch.nn import init
    
    init.normal_(net[0].weight, mean=0.0, std=0.01)
    init.constant_(net[0].bias, val=0.0)
    

    1.2定义损失函数和优化函数

    loss = nn.MSELoss()
    
    import torch.optim as optim
    optimizer = optim.SGD(net.parameters(), lr=0.03)
    

    2.训练

    大体步骤是{预测、评判、清空前一步梯度、计算本步梯度、修正权重}固定步骤

    num_epochs = 3
    for epoch in range(1, num_epochs + 1):
        for X, y in data_iter:
            output = net(X)
            # l/o/l/o
            l = loss(output, y.view(-1, 1))
            optimizer.zero_grad() # reset gradient, equal to net.zero_grad()
            l.backward()
            optimizer.step()
        print('epoch %d, loss: %f' % (epoch, l.item()))
    

    查看权重

    # result comparision
    dense = net[0]
    print(true_w, dense.weight.data)
    print(true_b, dense.bias.data)
    

    相关文章

      网友评论

          本文标题:pytorch线性回归(构建模型流程)

          本文链接:https://www.haomeiwen.com/subject/atxqfhtx.html