【pytorch01】简单回归问题

1.梯度下降(Gradient Descent)

梯度下降
y = x 2 ∗ s i n ( x ) y=x^{2}*sin(x) y=x2sin(x)
y ′ = 2 ∗ x ∗ s i n ( x ) + x 2 ∗ c o s ( x ) y'=2*x*sin(x) + x^{2}*cos(x) y=2xsin(x)+x2cos(x)
求最小值要求导

梯度下降定义:梯度下降要迭代计算,每一次得到一个导数以后,用原来的x减去该x处导数的值,得到一个新的x的值就是这样一个迭代的过程

x t = x t − 1 − η ∂ y ∂ x t − 1 x_{t}=x_{t-1}-η\frac{\partial{y}}{\partial x_{t-1}} xt=xt1ηxt1y

η就是learning rate(学习率),可以通过调整学习率够使目标函数在合适的时间内收敛到局部最小值。

  • y = w ∗ x + b y=w*x+b y=wx+b
    • 1.567 = w ∗ 1 + b 1.567 = w * 1 + b 1.567=w1+b
    • 3.043 = w ∗ 2 + b 3.043 = w * 2 + b 3.043=w2+b

w = 1.477
b = 0.089
通过消元法,此时w和b是一个准确解,被称之为Closed Form Solution

其实现实生活中可以精确求解的东西不多,我们现实生活中拿到的数据都是有一定偏差的,因此对于实际的问题,与其说求一个Closed Form Solution(封闭解),不如求得一个近似解,这个近似解在经验上可行,这样就可以达到我们的目的

用高斯噪声(均值为0.01,方差为1)模仿偏差(现实生活中拿到的数据都是带有一定噪声的)
y = w ∗ x + b + ϵ y=w *x+b + \epsilon y=wx+b+ϵ
ϵ ∼ N ( 0.01 , 1 ) \epsilon\sim N(0.01,1) ϵN(0.01,1)
1.567 = w ⋆ 1 + b + e p s 3.043 = w ⋆ 2 + b + e p s 4.519 = w ⋆ 3 + b + e p s . . . 1.567=w^{\star}1+b+eps\\3.043=w^{\star}2+b+eps\\4.519=w^{\star}3+b+eps\\... 1.567=w1+b+eps3.043=w2+b+eps4.519=w3+b+eps...
观测一组数据,通过观测这一组数据来求解,这一组数据中整体表现比较好的解,虽然不是Closed Form Solution,但是证明了有良好的表现,可以达到需求。

y = x 2 ∗ s i n ( x ) y=x^{2}*sin(x) y=x2sin(x)使用梯度下降算法是求这个函数的最小值

但是对于 y = w ∗ x + b y=w*x+b y=wx+b这个方程来说并不是要求y的最小值,而是要求真实的y和 w ∗ x + b w*x+b wx+b的差最小,因为希望 w ∗ x + b w*x+b wx+b更加接近真实的y的值

可以通过求 l o s s = ( w ∗ x + b − y ) 2 loss=(w*x+b -y)^2 loss=(wx+by)2的极小值,可以达到接近的目的,获取此时的w和b的值

图片

2.实战

l o s s = ( W X + b − y ) 2 loss=(WX+b-y)^2 loss=(WX+by)2

# 返回average loss
def compute_error_for_line_given_points(w,b,points):
    lossTotal = 0
    for i in range(len(points)):
        x = points[i,0]
        y = points[i,1]
        lossTotal += (y - (w * x + b))** 2
    return lossTotal / float(len(points))

w ′ = w − l r ∗ ∇ l o s s ∇ w w'=w-lr*\frac{\nabla loss}{\nabla w} w=wlrwloss

# 要求loss的极小值,对w和b分别梯度下降
def step_gradient(b_current,w_current,points,learningRate):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1]
        # loss函数分别对w和b求导
        # 多了N的原因是因为对所有点的导数累加起来,这样就不用做average了
        # 此时获得的w和b是所有点average之后的梯度
        w_gradient += -(2/N) * x * (y - (w_current * x + b_current))
        b_gradient += -(2/N) * (y - (w_current * x + b_current))
    new_b = b_current - (learningRate * b_gradient)
    new_w = w_current - (learningRate * w_gradient)
    return [new_w,new_b]

经过多次梯度下降得到最优解

def gradient_descent_runner(points,starting_w,starting_b,
                            learning_rate,num_iterations):
    w = starting_w
    b = starting_b
    for i in range(num_iterations):
        w,b = step_gradient(w,b,np.array(points),learning_rate)
    return [w,b]
def run():
    points = np.genfromtxt("data.csv",delimiter=",")
    print(points[:10])
    learning_rate = 0.0001
    initial_w = 0
    initial_b = 0
    num_iterations = 1000
    print("Starting gradient descent at w = {0},b = {1},error = {2}"
          .format(initial_w,initial_b,compute_error_for_line_given_points(initial_w,initial_b,points)))

    print("Running...")
    [w,b] = gradient_descent_runner(points,initial_w,initial_b,learning_rate,num_iterations)
    print("After {0} iterations w = {1},b = {2},error = {3}"
          .format(num_iterations,w, b,
                  compute_error_for_line_given_points(w, b, points)))

if __name__ == '__main__':
    run()

结果
最终的数据与Closed Form Solution非常接近

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为您解答关于使用PyTorch进行线性回归问题。 在PyTorch中进行线性回归可以分为以下几个步骤: 1. 准备数据:首先,您需要准备数据,包括训练数据和验证数据。在本例中,我们将使用一个简单的数据集,其中包含一些房屋的面积和价格,以用于预测房价。 2. 定义模型:接下来,您需要定义一个模型。在本例中,我们将使用一个简单的线性模型,它包含一个线性层,该层将输入的特征映射到输出的标签。 3. 定义损失函数:您需要定义一个损失函数,以便在训练模型时计算损失。在本例中,我们将使用均方误差(MSE)损失函数。 4. 训练模型:接下来,您需要定义一个优化器,并使用训练数据对模型进行训练。在本例中,我们将使用随机梯度下降(SGD)优化器,并迭代多次来更新模型的参数。 5. 验证模型:最后,您需要使用验证数据来评估模型的性能。在本例中,我们将计算模型在验证数据上的损失,并将其与训练数据的损失进行比较,以确定模型是否过拟合。 以下是一个简单的代码示例,演示了如何使用PyTorch进行线性回归: ```python import torch import torch.nn as nn # 准备数据 x_train = torch.tensor([[1.0], [2.0], [3.0], [4.0]]) y_train = torch.tensor([[2.0], [4.0], [6.0], [8.0]]) # 定义模型 model = nn.Linear(1, 1) # 定义损失函数 criterion = nn.MSELoss() # 定义优化器 optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # 训练模型 num_epochs = 1000 for epoch in range(num_epochs): # 向前传递 outputs = model(x_train) loss = criterion(outputs, y_train) # 向后传递和优化 optimizer.zero_grad() loss.backward() optimizer.step() if (epoch+1) % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # 验证模型 x_val = torch.tensor([[5.0]]) y_val = model(x_val) print('预测的房价: {:.2f}'.format(y_val.item())) ``` 希望这可以帮助您理解如何使用PyTorch进行线性回归

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值