%% An Example Write Up in Matlab % % % In this problem, we want to build a linear neural network that will % learn how to distinguish T's from G's and F's. First we will define the % data and visualize it. % %% Section Title % % Descriptive Text % We can even include LaTeX code (we'll be exporting to Latex): % % % \begin{tabular}{|c|c|} % \hline % Letter & Output\\ % \hline % T & $-60$\\ % G & $0$\\ % F & $60$\\ % \end{tabular} % T1=[1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1]; T2=[-1 1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1]; G1=[1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1 -1]; G2=[-1 1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1]; F1=[1 1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1]; F2=[-1 1 1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1]; gg=colormap(gray); gg=gg(end:-1:1,:); subplot(2,3,1) imagesc(T1) colormap(gg) subplot(2,3,2) imagesc(G1) subplot(2,3,3) imagesc(F1) subplot(2,3,4) imagesc(T2) subplot(2,3,5) imagesc(G2) subplot(2,3,6) imagesc(F2) %% Initialize Parameters % X=[T1(:) T2(:) G1(:) G2(:) F1(:) F2(:)]; %Domain T=[60 60 0 0 -60 -60]; %Range alpha=0.03; %Learning Rate W=randn(1,16); %Weights b=0; %biases TotalErr=0; %Keep a running track of the error NumPoints=6; %Number of training points %% Main Code % % In Hebb's rule, we update W and b by running through the data multiple % times. In this case, we will run through all 6 points a total of 60 % times - (each of the 60 trials is called an epoch). % for k=1:60 % 60 times through the data idx=randperm(6); % 6 points presented at random for j=1:NumPoints % Hebbian rule loop ThisOut=W*X(:,idx(j))+b; % Compute y ThisErr=T(idx(j))-ThisOut; % Compute t-y W=W+alpha*ThisErr*X(:,idx(j))'; %Hebb's Rule for W b=b+alpha*ThisErr; %Hebb's Rule for b end EpochErr(k)=norm((W*X+b*ones(1,6))-T); %This is the error between the % y's and the t's end %% Look at the output: % % Since we have one dimensional output and only 6 points, we can actually % look at our network output and compare it to the desired values: % T W*X+b*ones(1,6) %% Visualize the error. % % The plot below has the epoch along the horizontal axis, and error along % the vertical axis (as defined in the vector EpochErr). figure(2) plot(EpochErr);