错误使用 network/train (line 340) Output data size does not match net.outputs{2}.size.

Today, I encountered such a problem when learning to use neural networks:

错误使用 network/train (line 340)
Output data size does not match net.outputs{2}.size.

Specific picture:

The cause
of the problem. After analyzing a wave, it was found that the reason is very simple, because the number of neurons in the output layer is not the same as that set in the newff function, and the result is wrong.
I use this code:

clear
clc
P=[3.2 3.2 3 3.2 3.2 3.4 3.2 3 3.2 3.2 3.2 3.9 3.1 3.2;
9.6 10.3 9 10.3 10.1 10 9.6 9 9.6 9.2 9.5 9 9.5 9.7;
3.45 3.75 3.5 3.65 3.5 3.4 3.55 3.5 3.55 3.5 3.4 3.1 3.6 3.45;
2.15 2.2 2.2 2.2 2 2.15 2.14 2.1 2.1 2.1 2.15 2 2.1 2.15;
140 120 140 150 80 130 130 100 130 140 115 80 90 130;
2.8 3.4 3.5 2.8 1.5 3.2 3.5 1.8 3.5 2.5 2.8 2.2 2.7 4.6;
11 10.9 11.4 10.8 11.3 11.5 11.8 11.3 11.8 11 11.9 13 11.1 10.85;
50 70 50 80 50 60 65 40 65 50 50 50 70 70];
T=[2.24 2.33 2.24 2.32 2.2 2.27 2.2 2.26 2.2 2.24 2.24 2.2 2.2 2.35];
[p1,minp,maxp,t1,mint,maxt]=premnmx(P,T);
%创建网络
net=newff(minmax(P),[8,2],{'tansig','purelin'},'trainlm');
%设置训练次数
net.trainParam.epochs = 5000;
%设置收敛误差
net.trainParam.goal=0.0000001;
%训练网络
[net,tr]=train(net,p1,t1);
%输入数据
a=[3.0;9.3;3.3;2.05;100;2.8;11.2;50];
%将输入数据归一化
a=premnmx(a);
%放入到网络输出数据
b=sim(net,a);
%将得到的数据反归一化得到预测数据
c=postmnmx(b,mint,maxt);

It can be seen that the training set is a 1 * 14 matrix, which is equivalent to saying that there is a neuron outputting 14 samples, Test set
but in the newff function, 2 neurons are set in the output layer,

so an error will be reported.

Solution:
modify the number of neurons in the output layer of the newff function

I
Neural network operation results
can also find a problem here by changing 2 to 1 here. I am always confused when I first learn. Why does nftool always show layer1 and layer2 ? I am not sure whether there is an output layer.
net = newff (P, T, S, TF, BTF, BLF, PF, IPF, OPF, DDF)
The S inside is actually [the number of hidden layers, the number of output layers]. For example, [8,6,2,1] means set the hidden layer to 3, the first layer has 8 neurons, the second layer has 6 neurons, the third layer has 2 neurons, and the output layer has 1 Neurons.
To put it simply, the last layer displayed by nftool is the output layer, and the others are hidden layers.

Beginner neural network, please point out if there is an error

Published 13 original articles · Like 32 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_43637490/article/details/100654203