## 提高浅层神经网络泛化能力，避免过拟合

### 重新训练神经网络

```[x, t] = bodyfat_dataset; Q = size(x, 2); Q1 = floor(Q * 0.90); Q2 = Q - Q1; ind = randperm(Q); ind1 = ind(1:Q1); ind2 = ind(Q1 + (1:Q2)); x1 = x(:, ind1); t1 = t(:, ind1); x2 = x(:, ind2); t2 = t(:, ind2);```

```net = feedforwardnet(10); numNN = 10; NN = cell(1, numNN); perfs = zeros(1, numNN); for i = 1:numNN fprintf('Training %d/%d\n', i, numNN); NN{i} = train(net, x1, t1); y2 = NN{i}(x2); perfs(i) = mse(net, t2, y2); end```

### 多个神经网络

```[x, t] = bodyfat_dataset; Q = size(x, 2); Q1 = floor(Q * 0.90); Q2 = Q - Q1; ind = randperm(Q); ind1 = ind(1:Q1); ind2 = ind(Q1 + (1:Q2)); x1 = x(:, ind1); t1 = t(:, ind1); x2 = x(:, ind2); t2 = t(:, ind2);```

```net = feedforwardnet(10); numNN = 10; nets = cell(1, numNN); for i = 1:numNN fprintf('Training %d/%d\n', i, numNN) nets{i} = train(net, x1, t1); end```

```perfs = zeros(1, numNN); y2Total = 0; for i = 1:numNN neti = nets{i}; y2 = neti(x2); perfs(i) = mse(neti, t2, y2); y2Total = y2Total + y2; end perfs y2AverageOutput = y2Total / numNN; perfAveragedOutputs = mse(nets{1}, t2, y2AverageOutput) ```

### 早停法

```net.divideFcn ```

```net.divideParam ```

### 索引数据划分 (divideind)

```p = [-1:0.01:1]; t = sin(2*pi*p)+0.1*randn(size(p)); ```

```trainInd = 1:3:201 valInd = 2:3:201; testInd = 3:3:201; [trainP,valP,testP] = divideind(p,trainInd,valInd,testInd); [trainT,valT,testT] = divideind(t,trainInd,valInd,testInd); ```

### 随机数据划分 (dividerand)

```[trainP,valP,testP,trainInd,valInd,testInd] = dividerand(p); ```

```[trainT,valT,testT] = divideind(t,trainInd,valInd,testInd); ```

### 分块数据划分 (divideblock)

```[trainP,valP,testP,trainInd,valInd,testInd] = divideblock(p); ```

```[trainT,valT,testT] = divideind(t,trainInd,valInd,testInd); ```

### 交错数据划分 (divideint)

```[trainP,valP,testP,trainInd,valInd,testInd] = divideint(p); ```

```[trainT,valT,testT] = divideind(t,trainInd,valInd,testInd); ```

### 正则化

#### 修正的性能函数

`$F=mse=\frac{1}{N}\sum _{i=1}^{N}{\left({e}_{i}\right)}^{2}=\frac{1}{N}\sum _{i=1}^{N}{\left({t}_{i}-{\alpha }_{i}\right)}^{2}$`

`$msw=\frac{1}{n}\sum _{j=1}^{n}{w}_{j}^{2}$`

```[x,t] = simplefit_dataset; net = feedforwardnet(10,'trainbfg'); net.divideFcn = ''; net.trainParam.epochs = 300; net.trainParam.goal = 1e-5; net.performParam.regularization = 0.5; net = train(net,x,t); ```

#### 自动正则化 (trainbr)

```x = -1:0.05:1; t = sin(2*pi*x) + 0.1*randn(size(x)); net = feedforwardnet(20,'trainbr'); net = train(net,x,t); ```

`trainbr` 算法通常在网络输入和目标缩放到大致在 [−1,1] 范围内时效果最好。此处的测试问题就是这种情况。如果您的输入和目标不在此范围内，可以使用函数 `mapminmax``mapstd` 来执行缩放，如选择神经网络输入输出处理函数中所述。默认情况下，使用 `feedforwardnet` 创建的网络包含 `mapminmax` 作为输入和输出处理函数。

### 早停法和正则化的摘要与讨论

BALL

672-10-1小球位置测量中的双传感器标定

SINE (5% N)

411-15-1高斯噪声水平为 5% 的单周期正弦波

SINE (2% N)

411-15-1高斯噪声水平为 2% 的单周期正弦波

ENGINE (ALL)

11992-30-2发动机传感器 - 完整数据集

ENGINE (1/4)

3002-30-2发动机传感器 - 数据集的 1/4

CHOLEST (ALL)

2645-15-3胆固醇测量 - 完整数据集

CHOLEST (1/2)

1325-15-3胆固醇测量 - 1/2 数据集

ES1.2e-11.3e-21.9e-21.2e-11.4e-11.7e-11.3e-1
BR1.3e-32.6e-34.7e-31.2e-19.3e-23.0e-26.3e-3
ES/BR925411.55.721

### 训练后分析 (regression)

```x = [-1:.05:1]; t = sin(2*pi*x)+0.1*randn(size(x)); net = feedforwardnet(10); net = train(net,x,t); y = net(x); [r,m,b] = regression(t,y) ```
```r = 0.9935 m = 0.9874 b = -0.0067 ```