simple Feed forward (newff) network in MATLAB

2019-09-11 05:26发布

问题:

I used ffnew functions many times but when I am trying to create a simple feed forward network such that the input vector is P=[1;2;3;4] and the desired output is T=[1 ;0;0;1]. So i only have one sample input vector

The code is

net = newff(P,T,[4 1],{'tansig','tansig'});
net=train (net,P,T);

When I write the last line I got:

??? Error using ==> plus
Matrix dimensions must agree.

Error in ==> calcperf2 at 163
        N{i,ts} = N{i,ts} + Z{k};

Error in ==> trainlm at 253
[perf,El,trainV.Y,Ac,N,Zb,Zi,Zl] = calcperf2(net,X,trainV.Pd,trainV.Tl,trainV.Ai,Q,TS);

Error in ==> network.train at 216
  [net,tr] = feval(net.trainFcn,net,tr,trainV,valV,testV);

回答1:

Perhaps a simple example will help. Consider the famous XOR problem:

input = [0 0; 0 1; 1 0; 1 1]';               %'# each column is an input vector
ouputActual = [0 1 1 0];                     %#

net = newpr(input, ouputActual, 2);          %# 1 hidden layer with 2 neurons
net.divideFcn = '';                          %# use the entire input for training

net = init(net);                             %# init
[net,tr] = train(net, input, ouputActual);   %# train
outputPredicted = sim(net, input);           %# predict

[err,cm] = confusion(ouputActual,outputPredicted);

Note that I used NEWPR instead of NEWFF. The reason is that it uses a logistic function on the output (NEWFF does linear), which is more suited for classification tasks. If you use a 1-of-N target encoding, the output will be in the range [0,1] and can be interpreted as posterior probabilities for each class (NEWFF will not be restricted to [0,1])



回答2:

If you create your NN using MLP or RNN, you can change the function

a2 = round(f2(LW2 * a1 + b2)) or a2 = round(purelin(LW2 * a1 + b2))

then the output NN (a2) will be binary