% TODO: create datasets x1 and x2 %% TODO: initialize the weights. This should be a vector of 3 weights, since we're using 2D data w = ... % Set this to one in order to check if the gradients are correct. % Set to zero if you're confident that they're correct and you want to skip the check checkGradient = 1; %%%% YOUR MAIN JOB IS TO IMPLEMENT THE FUNCTION logisticNLP THAT TAKES %%%% DATA x1 AND x2 FROM THE TWO TRAINING CLASSES, AND THE WEIGHT VECTOR w, AND EVALUATES %%%% THE NEGATIVE LOG-LIKELIHOOD %%%% %%%% YOU SHOULDN'T NEED TO MODIFY ANYTHING FROM HERE UNTIL THE END OF THE INNER LOOP %%%% BUT YOU SHOULD LOOK IT OVER TO MAKE SURE YOU UNDERSTAND IT % Initial negative log-likelihood and gradient [ll,g] = logisticNLP(x1,x2,w); % Use finite differences to compute the gradient and compare with the analytic value if checkGradient g2 = zeros(size(w)); h = .001; for i=1:length(w) w2 = w; w2(i)=w2(i)+h; g2(i) = (logisticNLP(x1,x2,w2) - ll)/h; end disp('Analytic gradient:') disp(g) disp('Numerical gradient:') disp(g2) disp('These two should be nearly identical (i.e., within 1%)') pause end % Initial step-size lambda = .001; % Gradient descent inner-loop l2 = []; for t=1:1000 l2 = [l2;ll]; %keep track of negative log-likelihoods in case you want to plot them lambda = lambda * 2; e = logisticNLP(x1,x2,w-lambda*g); if norm(g) < eps warning('gradient is zero'); break; end if isnan(e) || isinf(e) error('nan/inf error'); end while (e >= ll) && (lambda > 0) lambda = lambda / 2; e = logisticNLP(x1,x2,w-lambda*g); end if (lambda <= eps) warning('Infinitesimal lambda') fprintf(2,'lambda = %f\n',lambda); break; end w = w - lambda*g; [ll,g] = logisticNLP(x1,x2,w); fprintf(2,'step = %d, lambda = %f, ll=%f\n\r',t,lambda,ll); end %%%%%%%% GENERATE PLOTS AND/OR SAVE RESULTS HERE