zoukankan      html  css  js  c++  java
  • 线性回归的从零开始实现

    http://zh.d2l.ai/chapter_deep-learning-basics/linear-regression.html

    d2lzh.py

    from IPython import display
    from matplotlib import pyplot as plt
    from mxnet import autograd,nd
    import random
    import os
    
    #用矢量图显示
    def use_svg_display():
        display.set_matplotlib_formats('svg')
    
    #设置图的尺寸
    def set_figsize(figsize=(3.5,2.5)):
        use_svg_display()
        plt.rcParams['figure.figsize'] = figsize
    
    #读取小批量数据
    def data_iter(batch_size,features,labels):
        num_examples = len(features)
        indices = list(range(num_examples))
        random.shuffle(indices)#样本读取顺序随机
        for i in range(0,num_examples,batch_size):
            #选取从i到i+batch_size-1的元素
            j = nd.array(indices[i:min(i+batch_size,num_examples)])
            #返回第j个元素和标签
            yield features.take(j),labels.take(j)
    
    
    #线性回归的矢量表达式
    def linreg(X,w,b):
        return nd.dot(X,w) + b
    
    #定义损失函数
    def squard_loss(y_hat,y):
        return (y_hat-y.reshape(y_hat.shape))**2/2
        
    #定义优化算法
    '''
    小批量随机梯度下降算法。它通过不断迭代模型参数来优
    化损失函数。这⾥⾃动求梯度模块计算得来的梯度是⼀个批量样本的梯度和。我们将它除以批量
    ⼤小来得到平均值。
    ''' 
    def sgd(params,lr,batch_size):
        for param in params:
            param[:] = param - lr * param.grad / batch_size
    
    #绘制
    def paint(features,labels):
        set_figsize()
        plt.scatter(features[:,1].asnumpy(),labels.asnumpy(),1)
        plt.show()

    linear.py

    from IPython import display
    from matplotlib import pyplot as plt
    from mxnet import autograd,nd
    import random
    import os
    from d2lzh import *
    
    #构造数据
    #数据的维度
    num_inputs = 2
    #样本数量
    num_examples = 1000
    #产生数据的权值
    true_w = [2,-3.4]
    true_b = 4.2
    #生成标准差为1的1000个样本
    features = nd.random.normal(scale=1,shape=(num_examples,num_inputs))
    #计算标签
    labels = true_w[0]*features[:,0] + true_w[1]*features[:,1] + true_b
    #加上随机噪声
    labels += nd.random.normal(scale=0.01,shape=labels.shape)
    
    #绘制数据
    paint(features,labels)
    
    
    #初始化模型参数
    #将权重初始化成均值为0、标准差为0.01的正态随机数,偏差则初始化成0
    w = nd.random.normal(scale=0.01,shape=(num_inputs,1))
    b = nd.zeros(shape=(1,))
    
    #之后的模型训练中,需要对这些参数求梯度来迭代参数的值,因此我们需要创建它们的梯度。
    w.attach_grad()
    b.attach_grad()
    
    #学习率
    lr = 0.03
    #迭代周期
    num_epochs = 3
    #线性回归
    net = linreg
    #误差函数
    loss = squard_loss
    #批量大小
    batch_size = 10
    
     # 训练模型⼀共需要num_epochs个迭代周期
    for epoch in range(num_epochs):
        # 在每⼀个迭代周期中,会使⽤训练数据集中所有样本⼀次(假设样本数能够被批量⼤⼩整除)。
        # X和y分别是⼩批量样本的特征和标签
        for X, y in data_iter(batch_size, features, labels):
            # X.attach_grad()
            with autograd.record():
                # l是有关⼩批量X和y的损失
                l = loss(net(X,w,b),y)
             # ⼩批量的损失对模型参数求梯度
            l.backward()
            '''
            #对每个样本求梯度之后的和
            print(w.grad)
            #对每个样本求梯度之后的和
            print(b.grad)
            #每个样本点在原权值下的梯度
            print(X.grad)
            os.system("pause")
            '''
            sgd([w,b],lr,batch_size)# 使⽤⼩批量随机梯度下降迭代模型参数
        train_l = loss(net(features, w, b), labels)
        print( 'epoch %d, loss %f' % (epoch + 1, train_l.mean().asnumpy()) )
    
    print(true_w)
    print(w)
  • 相关阅读:
    Cookie中用户登录信息的提示
    利用CentOS系统IPtables防火墙添加网站IP白名单
    php5.3升级到5.5
    nginx+apache 404错误页面
    启用nginx status状态详解
    nginx前端负载,后端apache获取真实IP设置
    CentOS查看系统信息-CentOS查看命令
    nginx负载 发向代理配置文件参考
    Linux添加用户(user)到用户组(group)
    Centos6.5快速配置可用网卡
  • 原文地址:https://www.cnblogs.com/xiaochi/p/12613402.html
Copyright © 2011-2022 走看看