求助:如何运用MATLAB编写一个BP神经网络程序,要求是二维输入,一维输出(输出值只能是0或者1),多谢!

2024年11月18日 22:59
有1个网友回答
网友(1):

x=[0.45 42;0.32 42;0.47 51;0.52 50;0.88 6;0.92 3;0.01 21;0.06 4;0.58 48;0.78 44];
y=[1;0;1;1;0;0;0;0;1;1]
inputs = x';
targets = y';

hiddenLayerSize = 8;
net = patternnet(hiddenLayerSize);

net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};

net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

net.trainFcn = 'trainlm'; % Levenberg-Marquardt

net.performFcn = 'mse'; % Mean squared error

net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
'plotregression', 'plotfit'};

[net,tr] = train(net,inputs,targets);

outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)

trainTargets = targets .* tr.trainMask{1};
valTargets = targets .* tr.valMask{1};
testTargets = targets .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs)
valPerformance = perform(net,valTargets,outputs)
testPerformance = perform(net,testTargets,outputs)

view(net)

训练的模型保存在net这个结构体中,想通过输入得到输出用sim()函数