gusucode.com > Matlab脸部识别程序源码 > code/code/perceptron_error.m

    function [cerror, mse] = perceptron_error(weights,data,targets)
  % function [cerror, mse] = perceptron_error(weights,data,targets)
  %
  %  Compute mis-classification error and mean squared error for
  %    a perceptron (linear) classifier
  %                            A. Student, ICS 175A
  %
  %  Inputs
  %     weights: 1 x (d+1) row vector of weights
  %     data: N x (d+1) matrix of training data    
  %     targets:  N x 1 vector of target values (+1 or -1)
  %                                   
  %  Outputs
  %     cerror: the percentage of examples misclassified (between 0 and 100)
  %     mse:  the mean-square error (sum of squared errors divided by N)
 
  N = size(data, 1);
  
  % error checking
  if nargin ~= 3
      error('The function takes three arguments (weights, data, targets)');
  end
  if size(weights,1) ~= 1
      error('The first argument (weights) should be a row vector');
  end
  if size(data,2) ~= size(weights,2)
      error('The first two arguments (weights and data) should have the same number of columns');
  end
  if size(data,1) ~= size(targets,1)
      error('The last two arguments (targets and data) should have the same number of rows');
  end

  
  % calculate the unthresholded output
  outputs = sig(weights * data')';
  
  % compare thresholded output to the target values to get the accuracy
  cerror = 100 * sum(sign(outputs) ~= targets)/N;
  
  % compare unthresholded output to the target values to get the mse
  mse = sum((outputs-targets).^2)/N;


function s = sig(x)
% Computes (rescaled) sigmoid function
  s = 2./(1+exp(-x)) - 1;

function ds = dsig(x)
% Computes derivative of (rescaled) sigmoid function
  ds = .5 .* (sig(x)+1) .* (1-sig(x));