介绍

学习了拟合神经网络fitnet里面的数据结构,本节继续学习拟合神经网络fitnet里面的函数。

net对象如下所示:

>> net
 net =
     Neural Network
               name: 'Function Fitting Neural Network'
           userdata: (your custom info)
     dimensions:
          numInputs: 1
          numLayers: 2
         numOutputs: 1
     numInputDelays: 0
     numLayerDelays: 0
  numFeedbackDelays: 0
  numWeightElements: 226
         sampleTime: 1
     connections:
        biasConnect: [1; 1]
       inputConnect: [1; 0]
       layerConnect: [0 0; 1 0]
      outputConnect: [0 1]
     subobjects:
              input: Equivalent to inputs{1}
             output: Equivalent to outputs{2}
             inputs: {1x1 cell array of 1 input}
             layers: {2x1 cell array of 2 layers}
            outputs: {1x2 cell array of 1 output}
             biases: {2x1 cell array of 2 biases}
       inputWeights: {2x1 cell array of 1 weight}
       layerWeights: {2x2 cell array of 1 weight}
     functions:
           adaptFcn: 'adaptwb'
         adaptParam: (none)
           derivFcn: 'defaultderiv'
          divideFcn: 'dividerand'
        divideParam: .trainRatio, .valRatio, .testRatio
         divideMode: 'sample'
            initFcn: 'initlay'
         performFcn: 'mse'
       performParam: .regularization, .normalization
           plotFcns: {'plotperform', 'plottrainstate', 'ploterrhist',
                     'plotregression', 'plotfit'}
         plotParams: {1x5 cell array of 5 params}
           trainFcn: 'trainlm'
         trainParam: .showWindow, .showCommandLine, .show, .epochs,
                     .time, .goal, .min_grad, .max_fail, .mu, .mu_dec,
                     .mu_inc, .mu_max
     weight and bias values:
                 IW: {2x1 cell} containing 1 input weight matrix
                 LW: {2x2 cell} containing 1 layer weight matrix
                  b: {2x1 cell} containing 2 bias vectors
     methods:
              adapt: Learn while in continuous use
          configure: Configure inputs & outputs
             gensim: Generate Simulink model
               init: Initialize weights & biases
            perform: Calculate performance
                sim: Evaluate network outputs given inputs
              train: Train network with examples
               view: View diagram
        unconfigure: Unconfigure inputs & outputs
     evaluate:       outputs = net(inputs)

里面包含的运算相关的参数如下:

 (1)IW;

(2)LW;

(3)b;

(4)inputs.range;inputs.processedRange

(5)outputs.range;outputs.processedRange

如何根据这7类参数计算出最后的拟合结果?

netfit的计算公式原始数据

根据net.IW可得下式,记为

>> W1=net.IW{1}
 W1 =
    -0.0643   -0.5102   -0.3899   -1.1452   -0.6156   -0.5644   -0.8229   -0.4793   -1.4119   -0.0568   -0.4892    0.1450   -2.0693
    -3.3754    0.9086    0.4771    1.2751   -0.9957    0.7283   -0.2105   -0.9506    0.6034   -1.4836   -0.4590    0.8682    0.0842
    -0.9134    1.9986    0.3363    0.0187    1.4188   -0.0724    1.1463    1.5481   -1.1005    0.2486    0.2108   -0.6476    0.2342
     0.4928   -1.1089    1.3174   -0.0159    0.1640   -1.8452   -0.2444    0.2382    0.4159   -1.0352   -0.1922   -0.2572    0.2730
    -0.1881   -0.4629    0.5444    0.1811   -0.2755   -1.0071   -0.0371    0.9353    0.0249   -0.7149    0.6569    1.3472    0.8498
    -0.5650    1.4343    1.6925    0.6614    0.8298    0.4463   -0.0292    0.0611    0.1525    0.6717   -1.9798   -1.3753   -1.4448
    -0.8131    1.1747    0.9539   -0.0602   -1.3154    0.5710   -1.3058    1.2787    1.4570   -0.9123   -1.0669    1.0100   -0.4752
    -0.3102   -1.1759    0.1568   -0.2604   -0.4042   -0.6719    0.2898    2.9933    0.6541    0.6591   -0.3286   -1.5196    0.8313
    -0.2545    0.3364   -1.0118    0.8460    0.8119   -0.2874    0.3222   -1.9115    1.1700   -0.9626    0.9201    2.1497   -0.4317
     0.8291    0.4106    1.3754   -0.1689    0.3806    0.4695   -0.6680    3.0876   -0.6761   -0.9902   -0.5475   -0.9032   -0.0428
    -1.8959    2.3886   -1.1967   -0.6461   -1.0763   -0.2231    0.3428   -1.1600   -0.0316   -1.1602   -0.4950    1.0815    0.8906
    -0.1548   -0.6660    0.1265    0.7222    0.8261   -0.4984   -0.2974   -0.0020   -0.4614   -0.0332    0.1965   -0.4464   -0.9535
     0.3757    0.8101    1.0679    0.4015    0.4796   -0.2457    0.5457    1.5153   -0.3745    1.5960    1.5651    0.5996   -1.2532
    -1.0513    0.1569    0.1494   -0.5867   -1.9264    0.0396   -0.2479    0.6159   -1.0877    0.4553    1.2083    0.2206    3.3561
     0.1181   -0.2309   -0.3543    1.0262   -0.6904   -0.1576    0.8765    0.5159   -0.0843   -0.1114    0.8817   -0.2681    0.3237根据net.LW{2,1}可得下式,记为
>> W2=net.LW{2,1}
 W2 =
    -0.2171   -0.0488   -0.4684   -0.7564    0.2761    0.4006    0.0158    0.1042    0.2304    0.3622   -0.2882    0.1682   -0.7007    0.0786    0.5335根据net.b{1}可得下式,记为
>> b1=net.b{1}
 b1 =
     2.1510
     1.9738
     1.5234
    -1.9311
     0.5286
     2.5662
    -0.4979
     0.1441
     0.1731
    -0.2444
    -1.6653
     1.2786
    -1.7759
    -2.2227
    -2.0938根据net.b{2}可得下式,记为
>> b2=net.b{2}
 b2 =
    -0.7651 根据net.inputs{1}.range可得下式,记为
和
 >> in_xref = net.inputs{1}.range
 in_xref =
    22.0000   81.0000
   118.5000  363.1500
    29.5000   77.7500
    31.1000   51.2000
    79.3000  136.2000
    69.4000  148.1000
    85.0000  147.7000
    47.2000   87.3000
    33.0000   49.1000
    19.1000   33.9000
    24.8000   45.0000
    21.0000   34.9000
    15.8000   21.4000
 >> in_xref_min = in_xref(:,1)
 in_xref_min =
    22.0000
   118.5000
    29.5000
    31.1000
    79.3000
    69.4000
    85.0000
    47.2000
    33.0000
    19.1000
    24.8000
    21.0000
    15.8000
 >> in_xref_max = in_xref(:,2)
 in_xref_max =
    81.0000
   363.1500
    77.7500
    51.2000
   136.2000
   148.1000
   147.7000
    87.3000
    49.1000
    33.9000
    45.0000
    34.9000
    21.4000根据net.inputs{1}.processedRange可得下式,记为
和
>> in_yref = net.inputs{1}.processedRange
 in_yref =
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
    -1.0000    1.0000
 >> in_yref_min = in_yref(:,1)
 in_yref_min =
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
     -1
 >> in_yref_max = in_yref(:,2)
 in_yref_max =
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000
     1.0000根据net.outputs{2}.range可得下式,记为
和
>> out_xlim = net.outputs{2}.range
 out_xlim =
          0   47.5000
 >> out_xlim_min = out_xlim(1)
 out_xlim_min =
      0
 >> out_xlim_max = out_xlim(2)
 out_xlim_max =
    47.5000根据net.outputs{2}.processedRange可得下式,记为
和
>> out_ylim = net.outputs{2}.processedRange
 out_ylim =
     -1     1
 >> out_ylim_min=out_ylim(1)
 out_ylim_min =
     -1
 >> out_ylim_max=out_ylim(2)
 out_ylim_max =
      1

拆分后,一共有如下12个参数: 

如何根据这12个参数得到最终的拟合值?

netfit对应的simulink模型

根据fitnet对象包含的方法gensim: Generate Simulink model,可以生成对应的simulink模型。

如下所示 

matlab fitcknn解析 fitlm matlab_matlab

其中:

(1)x1为输入参数,在体脂估计算例中为13×1的矩阵;

(2)Function Fitting Neural Network为拟合神经网络;

(3)y1为输出,在体脂估计算例中为1×1矩阵。 

在本层,可得:

 Function Fitting Neural Network模型展开

展开如下:

matlab fitcknn解析 fitlm matlab_开发语言_02

 包含两排,

第一排为输入

matlab fitcknn解析 fitlm matlab_matlab fitcknn解析_03

到中间变量

matlab fitcknn解析 fitlm matlab_matlab_04

,可以得到:

matlab fitcknn解析 fitlm matlab_matlab_05

继续展开ProcessInput1,如下所示,得到: 

  

matlab fitcknn解析 fitlm matlab_开发语言_06

 Layer1展开如下所示,得到:

matlab fitcknn解析 fitlm matlab_matlab_07

 

 其中tansig展开如下所示,得到:

 

matlab fitcknn解析 fitlm matlab_matlab_08

 综上,

第二排为中间变量

matlab fitcknn解析 fitlm matlab_matlab_04

到输出

matlab fitcknn解析 fitlm matlab_拟合_10

,可以得到:

展开Layer2如下所示,可以得到:

 

matlab fitcknn解析 fitlm matlab_拟合_11

其中展开purelin得到:

 

matlab fitcknn解析 fitlm matlab_开发语言_12

展开 ProcessOutput1得到:

matlab fitcknn解析 fitlm matlab_机器学习_13

其中展开mapminmax_reverse得到(下图中x,y为局部标号):

 

matlab fitcknn解析 fitlm matlab_matlab_14

 最终公式

最终计算公式为:

p=(x1-in_xref_min).*(in_yref_max-in_yref_min)./(in_xref_max-in_xref_min)+in_yref_min;
a1=2./(1+exp(-2*(W1*p+b1)))-1;
L2_out = W2*a1+b2;
y1=(L2_out-out_ylim_min)*(out_xlim_max-out_xlim_min)/(out_ylim_max-out_ylim_min)+out_xlim_min

带入x1=zeros(13,1) ,得到:

>> x1=zeros(13,1);
 p=(x1-in_xref_min).*(in_yref_max-in_yref_min)./(in_xref_max-in_xref_min)+in_yref_min;
 a1=2./(1+exp(-2*(W1*p+b1)))-1;
 L2_out = W2*a1+b2;
 y1=(L2_out-out_ylim_min)*(out_xlim_max-out_xlim_min)/(out_ylim_max-out_ylim_min)+out_xlim_min
 y1 =
  -21.854734309717628

 直接采用拟合神经网络训练结果网络net进行计算,

y1=net(x1)

 得到的结果为:

>> y1=net(zeros(13,1))
 y1 =
  -21.854734309717625

两者在浮点计算意义下结果完全一致,表明了分析过程的正确性。