layer_dimensions = [11 22 33 22 11]'; ld_size = size(layer_dimensions , 1); % what is deal [x rx dx rdx y ry dy rdy dphi] = deal( cell( ld_size , 1 ) ); [W rW dW rdW] = deal( cell( ld_size-1 , 1 ) ); numsamples = 100; %initialize weight and x,y, always allocate memory first for i = 1:ld_size x{i} = zeros( layer_dimensions( i ) , numsamples ); y{i} = x{i}; if i<ld_size W{i} = rand( layer_dimensions( i ) ,layer_dimensions( i+1 ) ); end end %initialize inputs = 0*ones(layer_dimensions(1),numsamples); y{1} = inputs; dphi{1} = zeros(size(y{1})); ry{1} = dphi{1}; numepoches = 100; numbatches = 10; for iii = 1:numepoches for ii = 1:numbatches % f0 pass for i = 2:ld_size x{i} = W{i-1}'*y{i-1}; y{i} = sigmoid(x{i}); dphi{i} = y{i}.*(1-y{i}); %for backpropagate end % r1 pass dy{ld_size} = y{ld_size} - y{1}; error=sum(sum(abs(dy{ld_size}))) dx{ld_size} = dphi{ld_size} .* dy{ld_size}; dW{ld_size-1} = y{ld_size-1} * dx{ld_size}'; for i = ld_size-1:-1:2 dy{i} = W{i} * dx{i+1}; W{i} = W{i} - 0.3 * dW{i}; dx{i} = dphi{i} .* dy{i}; dW{i-1} = y{i-1} * dx{i}'; end % f1 pass end end %test numtestsamples=3; for i = 1:ld_size x{i} = zeros( layer_dimensions( i ) , numtestsamples ); y{i} = x{i}; end inputs = zeros(layer_dimensions(1),numtestsamples); y{1} = inputs; dphi{1} = zeros(size(y{1})); ry{1} = dphi{1}; for i = 2:ld_size x{i} = W{i-1}'*y{i-1}; y{i} = sigmoid(x{i}); dphi{i} = y{i}.*(1-y{i}); %for backpropagate end y{ld_size}