pytorch实战第二章:回归问题

2.1神经元模型

  1. 提出了模拟生物神经元机制的人工神经网络的数学模型
  2. 进一步发展成感知机(Perceptron)模型
  3. 均方误差:MSE

2.2优化方法

  1. 这种通过严格的公式推导出的精确解也称为解析解
  2. 数值解:无法得到解析解.通过数值优化去优化出一个近似解
  3. 梯度下降法:Gradient Descent

2.3线性模型实战

import numpy as np
## 1. 采用数据
import numpy.random

data = []
for i in range(100):
    x = np.random.uniform(-10., 10.)  # 作用于从一个均匀分布的区域中随机采样。
    # 采样高斯噪音
    eps = numpy.random.normal(0., 0.01)
    y = 1.477 * x + 0.089 + eps
    data.append([x, y])
data = np.array(data)


## 2.计算误差
def mse(b, w, points):
    total_error = 0

    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        total_error += (y - (w * x + b)) ** 2
    return total_error / len(points)


## 3.计算梯度
def step_gradient(b_current, w_current, points, lr):
    '''
    计算误差函数在所有点上的导数,并更新w,b
    '''
    b_gradient = 0
    w_gradient = 0
    M = len(points)

    for i in range(M):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += (2 / M) * ((w_current * x + b_current) - y)
        w_gradient += (2 / M) * x * ((w_current * x + b_current) - y)
    new_b = b_current - lr * b_gradient
    new_w = w_current - lr * w_gradient
    return [new_b, new_w]


# 3.梯度更新

def gradient_descent(points, starting_b, starting_w, lr, num_iterations):
    b = starting_b
    w = starting_w
    losses = []
    for step in range(num_iterations):
        b, w = step_gradient(b, w, np.array(points), lr)
        loss = mse(b, w, points)
        # if step%50 == 0:
        #     print(f"iteration:{step},loss:{loss},w:{w},b:{b}")
        losses.append(loss)
    return [b, w, losses]


if __name__ == '__main__':
    lr = 0.01
    inital_b = 0
    inital_w = 0
    num_iteration = 1000
    [b, w, losses] = gradient_descent(data, inital_b, inital_w, lr, num_iteration)
    loss = mse(b, w, data)
    print(f'loss: {loss} w:{w},b:{b}')
    import matplotlib

    matplotlib.use('TkAgg')
    import matplotlib.pyplot as plt

    plt.plot(range(num_iteration), losses)
    plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值