Рис. 7.
Приложение 2.
Программа, моделирующая однослойную сеть.
unit UPerc;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
Percept_Field, StdCtrls, Buttons, ExtCtrls;
const InputLayerUnits=35;
OutputLayerUnits=2;
eta=0.05;
epsilon=0.000001;
numberpatterns=36;
type
TFrmPerceptron = class(TForm)
Percept_FieldPerc: TPercept_Field;
GroupBoxTrain: TGroupBox;
GroupBoxInit: TGroupBox;
ComboBoxABC: TComboBox;
ComboBoxDigits: TComboBox;
BtnNext: TButton;
BitBtnClose: TBitBtn;
EditNumPat: TEdit;
LabelNumPat: TLabel;
GroupBoxRec: TGroupBox;
LabelInput: TLabel;
BtnOutput: TButton;
LabelOdd: TLabel;
RadioGroupTarget: TRadioGroup;
RadioButtonOdd: TRadioButton;
RadioButtonEven: TRadioButton;
LabelOr: TLabel;
LabelEven: TLabel;
procedure ComboBoxABCChange(Sender: TObject);
procedure ComboBoxDigitsChange(Sender: TObject);
procedure Percept_FieldPercMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure BitBtnCloseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnNextClick(Sender: TObject);
procedure BtnOutputClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmPerceptron: TFrmPerceptron;
w:array[1..OutputLayerUnits,1..InputLayerUnits] of real;
indexBtnNextClick:byte;
activation:array[1..OutputLayerUnits] of real;
OutputLayerOutput:array[1..OutputLayerUnits] of shortint;
target:array[1..numberpatterns,1..OutputLayerUnits] of shortint;
v:array[1..numberpatterns,1..InputLayerUnits] of shortint;
implementation
{$R *.DFM}
procedure TFrmPerceptron.Percept_FieldPercMouseDown(Sender: TObject;
var m,k:BYTE;
correctRect:shortint;
L,T,H,V:INTEGER;
begin
L:=0;
T:=0;
H:=Percept_FieldPerc.UnitHorizontal;
V:=Percept_FieldPerc.UnitVertical;
for m :=1 to Percept_FieldPerc.UnitRectVert do
for k :=1 to Percept_FieldPerc.UnitRectHorz do
if (XL) and (YT) then
correctRect:=k+Percept_FieldPerc.UnitRectHorz*(m-1);
if (Button=mbLeft) and
(Percept_FieldPerc.Brushes[correctRect]=Percept_FieldPerc.BackGroundBr
ush) then
Percept_FieldPerc.Brushes[correctRect]:=Percept_FieldPerc.RectBrush;
end
else
if (Button=mbRight) and
(Percept_FieldPerc.Brushes[correctRect]=Percept_FieldPerc.RectBrush)th
en
Percept_FieldPerc.Brushes[correctRect]:=Percept_FieldPerc.BackGroundBr
ush;
inc(L,Percept_FieldPerc.UnitHorizontal);
inc(H,Percept_FieldPerc.UnitHorizontal);
inc(T,Percept_FieldPerc.UnitVertical);
inc(V,Percept_FieldPerc.UnitVertical);
procedure TFrmPerceptron.BitBtnCloseClick(Sender: TObject);
Close;
procedure TFrmPerceptron.FormCreate(Sender: TObject);
var i,j:byte;
rand:real;
//numberpatterns:=2;//10;
EditNumPat.Text:=inttostr(numberpatterns);
BtnNext.Font.Color:=clRed;
indexBtnNextClick:=0;
LabelInput.Visible:=False;
// *********************************************
Randomize;// случайные веса (-0.5,0.5)
for i := 1 to OutputLayerUnits do
for j := 1 to InputLayerUnits do
rand:=Random-0.5;
w[i,j]:=rand;
procedure TFrmPerceptron.BtnNextClick(Sender: TObject);
var i,j,m:byte;
sum:real;
neterror,err:real;
error:array[1..OutputLayerUnits] of real;
stop:boolean;
krandom:integer;
indexBtnNextClick:=indexBtnNextClick+1;
for m:=1 to InputLayerUnits do begin
if (Percept_FieldPerc.Brushes[m]=Percept_FieldPerc.RectBrush)
then
v[indexBtnNextClick,m]:=1;
if
(Percept_FieldPerc.Brushes[m]=Percept_FieldPerc.BackGroundBrush) then
v[indexBtnNextClick,m]:=-1;
// ******************ODD or EVEN*********************
if RadioButtonOdd.Checked then
target[indexBtnNextClick,1]:=1;
target[indexBtnNextClick,2]:=-1;
if RadioButtonEven.Checked then
target[indexBtnNextClick,1]:=-1;
target[indexBtnNextClick,2]:=1;
// ***************************************************
if (indexBtnNextClick+1)=numberpatterns then
BtnNext.Caption:='last';
if (indexBtnNextClick)=numberpatterns then
BtnNext.Font.Color:=clWindowText;
BtnNext.Caption:='finished';
LabelInput.Font.Color:=clRed;
LabelInput.Visible:=True;
BtnNext.Caption:='next';
//*********************MAIN*******************************
repeat
stop:=false;
for m := 1 to numberpatterns do
sum:=0;
sum:=sum+w[i,j]*v[m,j];
activation[i]:=sum;
if sum>=0 then
OutputLayerOutput[i]:=1;
OutputLayerOutput[i]:=-1;
neterror:=0;
err:=target[m,i]-activation[i];
error[i]:=err;
neterror:=neterror+0.5*sqr(err);
if neterror=0 then
w[i,j]:=w[i,j]+eta*error[i]*v[m,j];
end;//if
until stop;//end;
end; // if
procedure TFrmPerceptron.BtnOutputClick(Sender: TObject);
var z:array[1..InputLayerUnits] of shortint;
m,i,j:byte;
Output:array[1..InputLayerUnits] of real;
z[m]:=1;
z[m]:=-1;
sum:=sum+w[i,j]*z[j];
Output[i]:=sum;
if (Output[1]>Output[2]) then
LabelOdd.Font.Color:=clRed;
LabelEven.Font.Color:=clWindowText;
else begin
if (Output[2]>Output[1]) then
LabelEven.Font.Color:=clRed;
LabelOdd.Font.Color:=clWindowText;
end.
Программа, моделирующая сеть обратного распространения
unit UBack;
Страницы: 1, 2, 3, 4, 5