zoukankan      html  css  js  c++  java
  • 多项式拟合

    多项式拟合

    In [1]:
    %matplotlib inline
    import gluonbook as gb
    from mxnet import nd,autograd, gluon
    from mxnet.gluon import data as gdata,loss as gloss,nn
    
     

    生成数据集 $$y=1.2x - 3.4x^2 + 5.6x^3 + 5 + alpha$$

    In [2]:
    n_train = 100
    n_test = 100
    true_w = [1.2,-3.4,5.6]
    true_b = 5
    
    In [10]:
    features = nd.random.normal(shape=(n_train+n_test,1))
    features.shape
    
    Out[10]:
    (200, 1)
    In [11]:
    poly_features = nd.concat(features, nd.power(features, 2),nd.power(features, 3))
    poly_features.shape
    
    Out[11]:
    (200, 3)
    In [12]:
    labels = (true_w[0]*poly_features[:,0]+true_w[1]*poly_features[:,1]+true_w[2]*poly_features[:,2]+true_b)
    labels += nd.random.normal(scale=0.1,shape=labels.shape)
    
    In [13]:
    features[:2], poly_features[:2], labels[:2]
    
    Out[13]:
    (
     [[-0.6474401 ]
      [-0.55203336]]
     <NDArray 2x1 @cpu(0)>, 
     [[-0.6474401   0.41917866 -0.27139306]
      [-0.55203336  0.30474085 -0.1682271 ]]
     <NDArray 2x3 @cpu(0)>, 
     [1.1710231 2.493016 ]
     <NDArray 2 @cpu(0)>)
     

    定义,训练,测试模型

     

    平方损失函数

    In [14]:
    num_epochs = 100
    loss = gloss.L2Loss()
    
    In [17]:
    def fit_and_plot(train_features,test_features,train_labels,test_labels):
        net = nn.Sequential()
        net.add(nn.Dense(1))
        net.initialize()
        batch_size = min(10,train_labels.shape[0])
        
        train_iter = gdata.DataLoader(gdata.ArrayDataset(
            train_features,train_labels),batch_size,shuffle=True)
        trainer = gluon.Trainer(net.collect_params(),'sgd',{'learning_rate':0.01})
        
        train_ls = []
        test_ls = []
        
        for _ in range(num_epochs):
            for X, y in train_iter:
                with autograd.record():
                    l = loss(net(X),y)
                l.backward()
                trainer.step(batch_size)
                
            train_ls.append(loss(net(train_features),
                                train_labels).mean().asscalar())
            test_ls.append(loss(net(test_features),
                               test_labels).mean().asscalar())
        
        print('final epoch: train loss', train_ls[-1], 'test loss', test_ls[-1])
        print('weight:',net[0].weight.data().asnumpy())
        print('bias:',net[0].bias.data().asnumpy())
    
    In [18]:
    fit_and_plot(poly_features[:n_train, :], poly_features[n_train:, :],
                 labels[:n_train], labels[n_train:])
    
     
    final epoch: train loss 0.0054066028 test loss 0.007267852
    weight: [[ 1.1381239 -3.3803644  5.616167 ]]
    bias: [4.9635367]
    
    In [19]:
    poly_features.shape
    
    Out[19]:
    (200, 3)
     

    欠拟合

    In [21]:
    features.shape
    
    Out[21]:
    (200, 1)
    In [20]:
    fit_and_plot(features[:n_train, :], features[n_train:, :], labels[:n_train],
                 labels[n_train:])
    
     
    final epoch: train loss 91.158066 test loss 147.58788
    weight: [[18.628803]]
    bias: [1.0468582]
    
     

    过拟合

    In [22]:
    fit_and_plot(poly_features[0:2, :], poly_features[n_train:, :], labels[0:2],
                 labels[n_train:])
    
     
    final epoch: train loss 0.3388414 test loss 481.85403
    weight: [[-0.5109072   0.31804863 -0.13049412]]
    bias: [0.9656807]
  • 相关阅读:
    计算机科学中最重要的32个算法
    主流无线传输技术GPRS与CDMA之对比
    GPRS、GSM、WAP、CDMA 、EDGE和3G区别
    TCP与UDP的区别
    web自动化测试(java)---测试过程中遇到的错误合集
    web自动化测试(java)---环境搭建
    Java之基础学习(数据类型、运算符、分支语句和循环语句)
    OSI七层模型和tcp/ip四层模型对比
    测试工具之Match Tracer(正则表达式匹配工具)
    测试工具之RobotFramework关键字和快捷键
  • 原文地址:https://www.cnblogs.com/TreeDream/p/10022025.html
Copyright © 2011-2022 走看看