www.gusucode.com > som-bp混合神经网络的matlab程序源码 > matlab_emulator/HybridNet/BP_ANN.m

    
% use the som to discern the 
%Initialize training samples


% TN = zeros(1, length(normalroi1));
% TF = ones(1, length(fattyroi1));
% T = [TN TF];
% 
% net = newff(minmax(I), [5 1], {'logsig', 'logsig'});
% net.trainParam.show = 100;
% net.trainParam.lr = 0.2;
% % net.trainParam.mr = 0.9;
% net.trainParam.epochs = 2000;
% net.trainParam.goal = 1e-5;
% 
% [net,tr]=train(net,I,T);
% SimResult = sim(net,I);

% --------------------- initial the BP train and test sample



BP_train_T = [ones(1,train_positive_num),zeros(1,train_negative_num)];         %  train object
BP_test_T = [ones(1,test_positive_num),zeros(1,test_negative_num)];         %  test object

MinMaxValue = [zeros(BP_input_num ,1),ones(BP_input_num ,1)];       
BP_net = newff(MinMaxValue,[5,5,5,1],{'logsig','logsig','logsig','logsig'});
BP_net.trainParam.epochs = BP_epochs;
BP_net.trainParam.goal = 1e-8;

% ---------------------  training -----------------------
[BP_net,BP_tr] = train(BP_net,BP_train_P,BP_train_T);


threshold = 0.5;
%------------------------- cal the train mse
A = sim(BP_net,BP_train_P);
E = BP_train_T -A;
Train_MSE = mse(E);

train_False_positive = 0;
for(j=1:train_positive_num)
    if(A(j)<=threshold)
        train_False_positive = train_False_positive + 1;
    end
end
train_False_negative = 0;
for(j=train_positive_num+1:train_positive_num + train_negative_num)
    if(A(j)>threshold)
        train_False_negative = train_False_negative + 1;
    end
end
BPresult_train = A;     
%------------------------ cal the test mse ------------------
A = sim(BP_net,BP_test_P);
E = BP_test_T - A;
Test_MSE = mse(E);

test_False_positive = 0;
for(j=1:test_positive_num)
    if(A(j)<=threshold)
        test_False_positive = test_False_positive + 1;
    end
end
test_False_negative = 0;
for(j=test_positive_num+1:test_positive_num + test_negative_num)
    if(A(j)>threshold)
        test_False_negative = test_False_negative + 1;
    end
end
BPresult_test = A;
%--------------------------- save the result -----------------
savePath1 = 'E:\SOMBP\software\HybridNet004\';
file = '.mat';
savePath2 = [savePath1 'BP' file];
save(savePath2,'BPresult_train','BPresult_test',......
               'train_False_positive','train_False_negative',......
               'test_False_positive','test_False_negative',......
               'BP_net','BP_tr'); 
           
% plot the result
figure;
title('BP train result');
for(i=1:train_positive_num)
    hold on
    plot(BPresult_train(i),0,'r*');
end
for(i=train_positive_num+1:train_positive_num+train_negative_num)
    hold on
    plot(BPresult_train(i),0,'bsquare');
end

figure;
title('BP test result');
for(i=1:test_positive_num)
    hold on
    plot(BPresult_test(i),0,'r*');
end
for(i=test_positive_num+1:test_positive_num+test_negative_num)
    hold on
    plot(BPresult_test(i),0,'bsquare');
end

 minmax(BPresult_test(1:test_positive_num))
 minmax(BPresult_test(test_positive_num+1:test_positive_num+test_negative_num))
 bp_result = [train_False_positive,train_False_negative,test_False_positive,test_False_negative]