美文网首页
深度学习_线性回归初探

深度学习_线性回归初探

作者: VictorHong | 来源:发表于2019-07-31 17:04 被阅读0次

    练习 线性回归从零开始

    import mxnet as mx
    from mxnet import nd
    import random
    from mxnet import autograd as ag
    

    生成数据

    num_inputs = 2
    num_examples = 2000
    
    true_w = [5.5,-2.6]
    true_b = 3.4
    
    features = nd.random.normal(shape=(num_examples,num_inputs))
    labels = features[:,0]*true_w[0] + features[:,1]*true_w[1] + true_b
    labels += nd.random.normal(scale=0.01,shape=labels.shape)
    
    
    features[0:10],labels[0:10]
    
    (
     [[ 1.1630785   0.4838046 ]
      [ 0.29956347  0.15302546]
      [-1.1688148   1.558071  ]
      [-0.5459446  -2.3556297 ]
      [ 0.54144025  2.6785064 ]
      [ 1.2546344  -0.54877406]
      [-0.68106437 -0.1353156 ]
      [ 0.37723133  0.41016456]
      [ 0.5712682  -2.7579627 ]
      [ 1.07628    -0.6141325 ]]
     <NDArray 10x2 @cpu(0)>, 
     [ 8.5570145e+00  4.6516924e+00 -7.0968418e+00  6.5224156e+00
      -5.8473641e-01  1.1730561e+01  1.0545446e-02  4.3994021e+00
       1.3717617e+01  1.0907241e+01]
     <NDArray 10 @cpu(0)>)
    

    获取小批量数据

    def data_iter(batch_szie,features,labels):
        num_examples = len(features)
        idx = list(range(num_examples))
        random.shuffle(idx)
        for i in range(0,num_examples,batch_szie):
            j = nd.array(idx[i:min(i+batch_szie,num_examples)])
            yield features.take(j),labels.take(j)
    
    for x,y in data_iter(10,features,labels):
        print(x,y)
        break
    
    [[ 1.5999995  -0.5617653 ]
     [ 0.75412536  0.12447102]
     [-0.69230694 -0.7290668 ]
     [-2.5128932  -0.26212415]
     [ 0.28285232  1.1859419 ]
     [ 0.34267116 -0.36542156]
     [ 0.91700655  1.0906272 ]
     [-0.4991787   0.23789974]
     [-1.1900624   0.02474929]
     [ 0.9383549  -0.75709605]]
    <NDArray 10x2 @cpu(0)> 
    [13.677179    7.2258916   1.4948013  -9.739497    1.8581214   6.23214
      5.5850635   0.03775719 -3.2022686  10.532288  ]
    <NDArray 10 @cpu(0)>
    

    初始化模型参数

    w = nd.random.normal(scale=0.01,shape=(num_inputs,1))
    b = nd.zeros(shape=1)
    
    w,b
    
    (
     [[ 0.00538086]
      [-0.0016038 ]]
     <NDArray 2x1 @cpu(0)>, 
     [0.]
     <NDArray 1 @cpu(0)>)
    
    w.attach_grad()
    b.attach_grad()
    

    定义模型

    def linreg(X,w,b):
        return nd.dot(X,w) + b
    

    定义损失函数

    def squared_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 - param.grad*lr/batch_size
    

    训练

    lr = 0.01
    epochs = 5
    net = linreg
    loss = squared_loss
    batch_size = 10
    
    for epoch in range(1,epochs+1):
        for X,y in data_iter(batch_size,features,labels):
            with ag.record():
                l = loss(net(X,w,b),y)
            l.backward()
            sgd([w,b],lr,batch_size)
        train_l = loss(net(features,w,b),labels)
        print("epoch %d ,loss %f" % (epoch,train_l.mean().asscalar()))
    
    epoch 1 ,loss 0.402907
    epoch 2 ,loss 0.006754
    epoch 3 ,loss 0.000162
    epoch 4 ,loss 0.000050
    epoch 5 ,loss 0.000048
    
    true_w,w
    
    ([5.5, -2.6], 
     [[ 5.499551 ]
      [-2.6001654]]
     <NDArray 2x1 @cpu(0)>)
    
    true_b,b
    
    (3.4, 
     [3.400331]
     <NDArray 1 @cpu(0)>)
    

    相关文章

      网友评论

          本文标题:深度学习_线性回归初探

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