home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mega CD-ROM 1
/
megacd_rom_1.zip
/
megacd_rom_1
/
MAGAZINE
/
DDJMAG
/
DDJ8910.ZIP
/
SWAINE.LST
< prev
Wrap
File List
|
1989-09-07
|
7KB
|
128 lines
_Programming Paradigms_
by Michael Swaine
[LISTIN╟ ONE]
Program BackPropagationDemo;
Const NumOfRows = 2; (* Number of rows of cells. *)
NumOfCols = 2; (* Number of columns of cells. *)
LearningRate = 0.25; (* Learning rate. *)
Criteria = 0.005; (* Convergence criteria. *)
Zero = 0.05; (* Anything below 0.05 counts as zero. *)
One = 0.95; (* Anything above 0.95 counts as one. *)
Type CellRecord = Record
Output : Real; (* Output of the current cell. *)
Error : Real; (* Error signal for the current cell. *)
Weights: Array[0..NumOfCols] Of Real; (* Weights in cell. *)
End;
Var CellArray : Array[0..NumOfRows,0..NumOfCols] Of CellRecord; (* Cells. *)
Inputs : Array[1..NumOfCols] Of Real; (* Input signals. *)
DesiredOutputs: Array[1..NumOfCols] Of Real; (* Desired output signals. *)
Procedure CalculateInputsAndOutputs( Iteration: Integer );
Var I: Integer;
Begin (* Calculate the inputs and desired outputs for the current iteration. *)
(* The inputs cycle through the 4 patterns (0.05,0.05), (0.95,0.05), *)
(* (0.05,0.95), (0.95,0.95). The corresponding desired outputs are *)
(* (0.05,0.05), (0.05,0.95), (0.05,0.95), (0.95,0.05). The first *)
(* desired output is the logical AND of the inputs, and the second *)
(* desired output is the logical XOR. *)
If (Iteration Mod 2) = 1 Then Inputs[1] := One Else Inputs[1] := Zero;
If (Iteration Mod 4) > 1 Then Inputs[2] := One Else Inputs[2] := Zero;
If (Inputs[1] > 0.5) And (Inputs[2] > 0.5) Then DesiredOutputs[1] := One
Else DesiredOutputs[1] := Zero;
If (Inputs[1] > 0.5) Xor (Inputs[2] > 0.5) Then DesiredOutputs[2] := One
Else DesiredOutputs[2] := Zero;
End;
Procedure UpdateCellOnForwardPass( Row, Column: Integer );
Var J : Integer;
Sum: Real;
Begin (* Calculate the output of the cell at the specified row and column. *)
With CellArray[Row,Column] Do
Begin
Sum := 0.0; (* Clear weighted sum of inputs. *)
For J := 0 To NumOfCols Do (* Form weighted sum of inputs. *)
Sum := Sum + Weights[J]*CellArray[Row-1,J].Output;
Output := 1.0/(1.0+Exp(-Sum)); (* Calculate output of cell. This *)
(* is called a sigmoid function. *)
Error := 0.0; (* Clear error for backward pass. *)
End;
End;
Procedure UpdateCellOnBackwardPass( Row, Column: Integer );
Var J: Integer;
Begin (* Calculate error signals and update weights on the backward pass. *)èWith CellArray[Row,Column] Do
Begin
For J := 1 To NumOfCols Do (* Back propagate the error to the cells *)
CellArray[Row-1,J].Error := (* below the current cell. *)
CellArray[Row-1,J].Error+Error*Output*(1.0-Output)*Weights[J];
For J := 0 To NumOfCols Do (* Update the weights in the current cell. *)
Weights[J] :=
Weights[J] +
LearningRate*Error*Output*(1.0-Output)*CellArray[Row-1,J].Output;
End;
End;
Var I, J, K : Integer; (* I loops over rows, J loops over columns,*)
(* and K loops over weights. *)
ConvergedIterations: Integer; (* Network must remain converged for four *)
(* iterations (one for each input pattern).*)
Iteration : Integer; (* Total number of iterations so far. *)
ErrorSquared : Real; (* Error squared for current iteration. *)
Begin
ClrScr; (* Initialize the screen. *)
Writeln('Iteration Inputs Desired Outputs Actual Outputs');
Iteration := 0; (* Start at iteration 0. *)
ConvergedIterations := 0; (* The network hasn't converged yet. *)
For I := 1 To NumOfRows Do (* Initialize the weights to small random numbers.*)
For J := 1 To NumOfCols Do
For K := 0 To NumOfCols Do
CellArray[I,J].Weights[K] := 0.2*Random-0.1;
For I := 0 To NumOfRows Do (* Initialize outputs of dummy constant cells. *)
CellArray[I,0].Output := One;
Repeat
CalculateInputsAndOutputs(Iteration);
For J := 1 To NumOfCols Do (* Copy inputs to dummy input cells. *)
CellArray[0,J].Output := Inputs[J];
For I := 1 To NumOfRows Do (* Propagate inputs forward through network. *)
For J := 1 To NumOfCols Do
UpdateCellOnForwardPass(I,J);
For J := 1 To NumOfCols Do (* Calculate error signals. *)
CellArray[NumOfRows,J].Error :=
DesiredOutputs[J]-CellArray[NumOfRows,J].Output;
For I := NumOfRows Downto 1 Do (* Propagate errors backward through *)
For J := 1 To NumOfCols Do (* network, and update weights. *)
UpdateCellOnBackwardPass(I,J);
ErrorSquared := 0.0; (* Clear error squared. *)
For J := 1 To NumOfCols Do (* Calculate error squared. *)
ErrorSquared := ErrorSquared + Sqr(CellArray[NumOfRows,J].Error);
If ErrorSquared < Criteria Then (* If network has converged, increment *)
ConvergedIterations := ConvergedIterations + 1 (* convergence *)
Else ConvergedIterations := 0; (* count, else clear convergence count. *)
If (Iteration Mod 100) < 4 Then (* Every 100 iterations, write out *)
Begin (* information on the 4 patterns. *)
If (Iteration Mod 100) = 0 Then GotoXY(1,2);
Write(' ',Iteration:5,' '); (* Write iteration number. *)
For J := 1 To NumOfCols Do (* Write out input pattern. *)
Write(Inputs[J]:4:2,' ');è Write(' ');
For J := 1 To NumOfCols Do (* Write out desired outputs. *)
Write(DesiredOutputs[J]:4:2,' ');
Write(' ');
For J := 1 To NumOfCols Do (* Write out actual outputs. *)
Write(CellArray[NumOfRows,J].Output:4:2,' ');
Writeln;
End;
Iteration := Iteration + 1; (* Increment iteration count *)
Until (ConvergedIterations = 4) Or (Iteration = 32767);
(* Stop when the network has converged on all 4 input patterns, or when*)
(* we are about to get integer overflow. *)
If ConvergedIterations <> 4 (* Write a final message. *)
Then Writeln('Network didn''t converge')
Else Writeln('Network has converged to within criteria');
End.