📄 hopfieldtest.pas
字号:
//Copyright Ramesh Sabeti - sabeti@reazon.com
unit HopfieldTest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ANN,
StdCtrls, ExtCtrls;
type
TFormHopfieldTest = class(TForm)
Button1: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
RGAction: TRadioGroup;
Button12: TButton;
EditNoise: TEdit;
Label1: TLabel;
Button2: TButton;
EditIteration: TEdit;
Label2: TLabel;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure DisplayOutput(Pattern: TCharMatrix; IsOriginal: Boolean);
function Error(NoiseLevel: Integer; Iterations: Integer): Extended;
function Recall(SampleNum: Integer): Extended;
end;
var
FormHopfieldTest: TFormHopfieldTest;
{ A: TCharMatrix =(1 ,1 ,1);
B: TCharMatrix =(1 ,1 ,-1);
C: TCharMatrix =(1 ,-1 ,1);
D: TCharMatrix =(1 ,-1 ,-1);
E: TCharMatrix =(-1 ,1 ,1);
F: TCharMatrix =(-1 ,1 ,-1);
G: TCharMatrix =(-1 ,-1 ,1);
H: TCharMatrix =(-1 ,-1 ,-1);
}
OrigPixel, OutPixel: array [0..99] of TShape;
A: TCharMatrix =(-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
-1 ,+1 ,+1 ,+1 ,+1 ,+1 ,+1 ,-1 ,-1 ,-1 ,
-1 ,+1 ,+1 ,+1 ,+1 ,+1 ,+1 ,-1 ,-1 ,-1 ,
-1 ,+1 ,+1 ,-1 ,-1 ,+1 ,+1 ,-1 ,-1 ,-1 ,
-1 ,+1 ,+1 ,+1 ,+1 ,+1 ,+1 ,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
-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,+1 ,+1 ,+1 ,+1 ,-1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,+1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1
);
*)
HNN: THopfield;
Samples: array [0..7] of TCharMatrix;
OutFile: TextFile;
implementation
{$R *.DFM}
procedure TFormHopfieldTest.FormCreate(Sender: TObject);
var
x, Row, Cl, Col : Integer;
begin
AssignFile(OutFile, 'Hopfield.txt');
Rewrite(OutFile);
Show;
Randomize;
Samples [0] := A;
Samples [1] := O;
Samples [2] := E;
Samples [3] := F;
Samples [4] := G;
Samples [5] := H;
Samples [6] := I;
Samples [7] := J;
HNN := THopfield.Create;
// for x := 0 to 1 do
// HNN.Learn(Samples[x]);
Row := 10;
Col := 15;
for x := 0 to 99 do begin
Cl := x mod (NUM_PIXELS div 10);
if Cl = 0 then
Inc(Row);
Cl := Col + Cl;
OrigPixel[x] := TShape.Create(Self);
OrigPixel[x].Top := Row * 5;
OrigPixel[x].Left := Cl * 5;
OrigPixel[x].Height := 5;
OrigPixel[x].Width := 5;
OrigPixel[x].Parent := Self;
OrigPixel[x].Hide;
OutPixel[x] := TShape.Create(Self);
OutPixel[x].Top := Row * 5;
OutPixel[x].Left := 100 + Cl * 5;
OutPixel[x].Height := 5;
OutPixel[x].Width := 5;
OutPixel[x].Parent := Self;
OutPixel[x].Hide;
end;
end;
procedure TFormHopfieldTest.DisplayOutput(Pattern: TCharMatrix; IsOriginal: Boolean);
var
c, i: Integer;
begin
for i := Low(Pattern) to High(Pattern) do
if IsOriginal then
OrigPixel[i].Visible := Pattern[i] > 0
else
OutPixel[i].Visible := Pattern[i] > 0;
(* for i := Low(Output) to High(Output) do begin
C := i mod (NUM_PIXELS div 10);
if C = 0 then
Inc(Row);
C := Col + C;
if Output[i] > 0 then
Self.Canvas.Rectangle(C * 5, Row * 5, (C * 5) + 5, (Row * 5) + 5)
// else
// Self.Canvas.Rectangle(Col * 5, Row * 5, (Col * 5) + 2, (Row * 5) + 2)
end;
*)
Refresh;
end;
procedure TFormHopfieldTest.Button1Click(Sender: TObject);
begin
Recall(TButton(Sender).Tag);
end;
procedure TFormHopfieldTest.Button12Click(Sender: TObject);
begin
HNN.Clear;
end;
procedure TFormHopfieldTest.Button2Click(Sender: TObject);
var
x, Noise: Integer;
E: Extended;
begin
HNN.Clear;
RGAction.ItemIndex := 0; //put it in learn mode
//Teach the network
for x := Low(samples) to High(Samples) do begin
HNN.Learn(Samples[x]);
end;
RGAction.ItemIndex := 1; //put it in learn mode
//retrieve and report error
for x := 0 to 4 do begin
Noise := x * 5;
E := Error(Noise, StrToInt(EditIteration.Text));
Writeln(OutFile, Format('Error at %3d percent noise level = %6.2f',
[Noise, E]));
end;
CloseFile(OutFile);
end;
function TFormHopfieldTest.Error(NoiseLevel: Integer; Iterations: Integer): Extended;
var
x, y : Integer;
begin
EditNoise.Text := IntToStr(NoiseLevel);
Result := 0;
for y := 1 to Iterations do
for x := Low(Samples) to High(Samples) do begin
Result := Result + Recall(x);
end;
Result := Result / Iterations; //Return the percent error value
end;
function TFormHopfieldTest.Recall(SampleNum: Integer): Extended;
var
Pattern, Output: TCharMatrix;
i, j, Indx: Integer;
begin
Pattern := Samples[SampleNum];
if RGAction.ItemIndex = 1 then begin //if during recall phase
for i := 0 to StrToInt(EditNoise.Text) - 1 do begin
j := Random(NUM_PIXELS);
Pattern[j] := Pattern[j] * -1; // flip the pixel
end;
end;
DisplayOutput(Pattern, True);
if RGAction.ItemIndex = 0 then
HNN.Learn(Samples[SampleNum])
else begin
Output := HNN.Retrieve(Pattern);
DisplayOutput(Output, False);
end;
Result := 0;
for i := Low(Pattern) to High(Pattern) do
if Samples[SampleNum][i] <> Output[i] then
Result := Result + 1;
Result := Result / (High(Pattern) + 1); //return the percent error value
(* Writeln(OutFile);
Writeln(OutFile);
Write(OutFile, 'Input Output Noise Level: ' +
Edit1.Text + ' Char: ' + TButton(Sender).Caption);
if RGAction.ItemIndex = 0 then
WriteLn(OutFile, ' Phase: Learn')
else
WriteLn(OutFile, ' Phase: Recall');
for i := 0 to 9 do begin
for j := 0 to 9 do begin
indx := i * 10 + j;
if Pattern[indx] = 1 then
Write(OutFile, '@')
else
Write(OutFile, ' ');
end;
Write(OutFile, ' ');
for j := 0 to 9 do begin
indx := i * 10 + j;
if Output[indx] = 1 then
Write(OutFile, '@')
else
Write(OutFile, ' ');
end;
Writeln(OutFile);
end;
*)
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -