📄 testmlpeunit.pas
字号:
if NKind=2 then
begin
MLPECreateB2(NIn, NHid1, NHid2, NOut, A1, A2, EC, Ensemble);
end
else
begin
if NKind=3 then
begin
MLPECreateR2(NIn, NHid1, NHid2, NOut, A1, A2, EC, Ensemble);
end;
end;
end;
end;
end;
(*************************************************************************
Unsets network (initialize it to smallest network possible
*************************************************************************)
procedure UnsetEnsemble(var Ensemble : MLPEnsemble);
begin
MLPECreate0(1, 1, 1, Ensemble);
end;
(*************************************************************************
Iformational functions test
*************************************************************************)
procedure TestInformational(NKind : Integer;
NIn : Integer;
NHid1 : Integer;
NHid2 : Integer;
NOut : Integer;
EC : Integer;
PassCount : Integer;
var Err : Boolean);
var
Ensemble : MLPEnsemble;
N1 : Integer;
N2 : Integer;
begin
CreateEnsemble(Ensemble, NKind, -1.0, 1.0, NIn, NHid1, NHid2, NOut, EC);
MLPEProperties(Ensemble, N1, N2);
Err := Err or (N1<>NIn) or (N2<>NOut);
end;
(*************************************************************************
Processing functions test
*************************************************************************)
procedure TestProcessing(NKind : Integer;
NIn : Integer;
NHid1 : Integer;
NHid2 : Integer;
NOut : Integer;
EC : Integer;
PassCount : Integer;
var Err : Boolean);
var
Ensemble : MLPEnsemble;
Ensemble2 : MLPEnsemble;
ZeroNet : Boolean;
A1 : Double;
A2 : Double;
Pass : Integer;
I : Integer;
AllSame : Boolean;
RLen : Integer;
X1 : TReal1DArray;
X2 : TReal1DArray;
Y1 : TReal1DArray;
Y2 : TReal1DArray;
RA : TReal1DArray;
RA2 : TReal1DArray;
V : Double;
begin
//
// Prepare network
//
A1 := 0;
A2 := 0;
if NKind=2 then
begin
A1 := 1000*RandomReal-500;
A2 := 2*RandomReal-1;
end;
if NKind=3 then
begin
A1 := 1000*RandomReal-500;
A2 := A1+(2*RandomInteger(2)-1)*(0.1+0.9*RandomReal);
end;
//
// Initialize arrays
//
SetLength(X1, NIn-1+1);
SetLength(X2, NIn-1+1);
SetLength(Y1, NOut-1+1);
SetLength(Y2, NOut-1+1);
//
// Main cycle
//
Pass:=1;
while Pass<=PassCount do
begin
CreateEnsemble(Ensemble, NKind, A1, A2, NIn, NHid1, NHid2, NOut, EC);
//
// Same inputs leads to same outputs
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
X2[I] := X1[I];
Inc(I);
end;
I:=0;
while I<=NOut-1 do
begin
Y1[I] := 2*RandomReal-1;
Y2[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
MLPEProcess(Ensemble, X2, Y2);
AllSame := True;
I:=0;
while I<=NOut-1 do
begin
AllSame := AllSame and (Y1[I]=Y2[I]);
Inc(I);
end;
Err := Err or not AllSame;
//
// Same inputs on original network leads to same outputs
// on copy created using MLPCopy
//
UnsetEnsemble(Ensemble2);
MLPECopy(Ensemble, Ensemble2);
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
X2[I] := X1[I];
Inc(I);
end;
I:=0;
while I<=NOut-1 do
begin
Y1[I] := 2*RandomReal-1;
Y2[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
MLPEProcess(Ensemble2, X2, Y2);
AllSame := True;
I:=0;
while I<=NOut-1 do
begin
AllSame := AllSame and (Y1[I]=Y2[I]);
Inc(I);
end;
Err := Err or not AllSame;
//
// Same inputs on original network leads to same outputs
// on copy created using MLPSerialize
//
UnsetEnsemble(Ensemble2);
MLPESerialize(Ensemble, RA, RLen);
SetLength(RA2, RLen-1+1);
I:=0;
while I<=RLen-1 do
begin
RA2[I] := RA[I];
Inc(I);
end;
MLPEUnserialize(RA2, Ensemble2);
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
X2[I] := X1[I];
Inc(I);
end;
I:=0;
while I<=NOut-1 do
begin
Y1[I] := 2*RandomReal-1;
Y2[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
MLPEProcess(Ensemble2, X2, Y2);
AllSame := True;
I:=0;
while I<=NOut-1 do
begin
AllSame := AllSame and (Y1[I]=Y2[I]);
Inc(I);
end;
Err := Err or not AllSame;
//
// Different inputs leads to different outputs (non-zero network)
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
X2[I] := 2*RandomReal-1;
Inc(I);
end;
I:=0;
while I<=NOut-1 do
begin
Y1[I] := 2*RandomReal-1;
Y2[I] := Y1[I];
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
MLPEProcess(Ensemble, X2, Y2);
AllSame := True;
I:=0;
while I<=NOut-1 do
begin
AllSame := AllSame and (Y1[I]=Y2[I]);
Inc(I);
end;
Err := Err or AllSame;
//
// Randomization changes outputs (when inputs are unchanged, non-zero network)
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
X2[I] := 2*RandomReal-1;
Inc(I);
end;
I:=0;
while I<=NOut-1 do
begin
Y1[I] := 2*RandomReal-1;
Y2[I] := Y1[I];
Inc(I);
end;
MLPECopy(Ensemble, Ensemble2);
MLPERandomize(Ensemble2);
MLPEProcess(Ensemble, X1, Y1);
MLPEProcess(Ensemble2, X1, Y2);
AllSame := True;
I:=0;
while I<=NOut-1 do
begin
AllSame := AllSame and (Y1[I]=Y2[I]);
Inc(I);
end;
Err := Err or AllSame;
//
// Normalization properties
//
if NKind=1 then
begin
//
// Classifier network outputs are normalized
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
V := 0;
I:=0;
while I<=NOut-1 do
begin
V := V+Y1[I];
Err := Err or (Y1[I]<0);
Inc(I);
end;
Err := Err or (AbsReal(V-1)>1000*MachineEpsilon);
end;
if NKind=2 then
begin
//
// B-type network outputs are bounded from above/below
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
I:=0;
while I<=NOut-1 do
begin
if A2>=0 then
begin
Err := Err or (Y1[I]<A1);
end
else
begin
Err := Err or (Y1[I]>A1);
end;
Inc(I);
end;
end;
if NKind=3 then
begin
//
// R-type network outputs are within [A1,A2] (or [A2,A1])
//
I:=0;
while I<=NIn-1 do
begin
X1[I] := 2*RandomReal-1;
Inc(I);
end;
MLPEProcess(Ensemble, X1, Y1);
I:=0;
while I<=NOut-1 do
begin
Err := Err or (Y1[I]<Min(A1, A2)) or (Y1[I]>Max(A1, A2));
Inc(I);
end;
end;
Inc(Pass);
end;
end;
(*************************************************************************
Silent unit test
*************************************************************************)
function testmlpeunit_test_silent():Boolean;
begin
Result := TestMLPE(True);
end;
(*************************************************************************
Unit test
*************************************************************************)
function testmlpeunit_test():Boolean;
begin
Result := TestMLPE(False);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -