% Lab 6, problem 1 % you must first load in apples and oranges data % initialize data array data=[apples oranges]; [N K]=size(data); % initialize teacher teacher=[ones(1,K/2) zeros(1,K/2)]; % learning rate eta=.001; % number of trials - you may want to make this longer num_trials=100; % initialize weights w=randn(2,1); w0=randn(1); % initialize data plot figure(1) plot(apples(1,:),apples(2,:),'+',oranges(1,:),oranges(2,:),'o') hold on x1=0:4; x2=-(w(1)*[0:4]+w0)/w(2); h=plot(x1,x2,'EraseMode','xor'); axis image, axis([0 4 0 4]) % loop over trials for t=1:num_trials dw=0; dw0=0; e2=0; % loop over training set for alpha=1:K u=w'*data(:,alpha)+w0; % net input y=sigmoid(u); % neuron output e=teacher(alpha)-y; % error e2=e2+e^2; % accumulate squared error delta=e*y*(1-y); % error * dy/du dw=dw+2*eta*delta*data(:,alpha); % accumulate dw dw0=dw0+2*eta*delta; % accumulate dw0 end % update weights w=w+dw; w0=w0+dw0; % save E for this trial E(t)=e2; % update display of separating hyperplane x2=-(w(1)*[0:4]+w0)/w(2); set(h,'YData',x2) drawnow end hold off figure(2) plot(E)