%This computes weights for a simple neural network using backpropagation and tests it.
%The goal is to train the neural network to determine the presence of a "+" symbol.
clear;a=7;mu=0.2;sigma=0.1;
%Generate 10000 labelled training image sets as follows:
%Generate 3X3 images of noise, and unwrap to 9X1 vectors of noise. 
%Add "+" symbol at random to half of the 9X1 vectors of noise.
W=0.5+randn(6,10);W0=[0.5+randn(6,1);-3];
for K=1:10000;%training image #K:
if(rand(1,1)>0.5);d=1;else d=0;end;X=sigma*randn(9,1);
X=X+d*[0 1 0 1 1 1 0 1 0]';X(10)=1;%Add "+" symbol to noise half the time, at random.
%Run NN Forward:
V=W*X;Y=1./(1+exp(-a*V));Y(7)=1;v0=W0'*Y;y0=1/(1+exp(-a*v0));
%Backpropagation:
delta0=-a*(d-y0)*y0*(1-y0);DELTA=a*delta0*Y(1:6).*(1-Y(1:6)).*W0(1:6);
dEdW=DELTA*X';W=W-mu*dEdW;W0=W0-mu*delta0*y0;end;
%Test image set:
for K=1:100;%Generate 100 test images by adding "+" to noise half the time, at random.
if(rand(1,1)>0.5);d(K)=1;else d(K)=0;end;X=sigma*randn(10,1);
X=X+d(K)*[0 1 0 1 1 1 0 1 0 0]';X(10)=1;%training image #K
%Run NN Forward:
V=W*X;Y=1./(1+exp(-a*V));Y(7)=1;v0=W0'*Y;y0(K)=1/(1+exp(-a*v0));e(K)=abs(d(K)-y0(K));end;
figure,subplot(2,1,1),stem(e),subplot(2,1,2),stem(d)
