⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backprop.pas

📁 ANN And Hopfield Neural Network
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  //Save results
  AssignFile(F, IntToStr(CurClassifier) + '-res.txt');
  Rewrite(F);
  Writeln(F, 'Centroids');
  for i := Low(Centroids) to NumCentroids - 1 do
    Writeln(F, FloatToStr(Centroids[i].Vector[0]) + ', ' +
               FloatToStr(Centroids[i].Vector[1]) + '          ' +
               IntToStr(Classifications[i].Total) + ' members'
            );

  Writeln(F, 'Analysis');
  for i := 0 to 3 do begin
    Writeln(F, Format('Class %1d: %8d  %8.2f  %8d  %8.2f  %8d',
       [i + 1,
        Classifications[i].Classified,
        Classifications[i].percentClassified,
        Classifications[i].Misclassified,
        Classifications[i].PercentMisclassified,
        Classifications[i].Total]));
  end;
  Writeln(F, FloatToStr(
        (Classifications[0].Classified +
         Classifications[1].Classified +
         Classifications[2].Classified +
         Classifications[3].Classified) /
         8.25) + '%');

  CloseFile(F);

end;

procedure TFormBackpropTest.ButtonSaveGraphClick(Sender: TObject);
var
  FormImage: TBitmap;
begin
  Chart1.CopyToClipboardBitmap;
end;

procedure TFormBackpropTest.ButtonPrintClick(Sender: TObject);
var
  P: TPrinter;
begin

end;

procedure TFormBackpropTest.PrintSet( var P: TPrinter;
                           Samples: array of TFeature;
                           ShowErrors: Boolean);
var
  i: Integer;
begin
  for i := Low(Samples) to High(Samples) do
    PrintSample(P, Samples[i], ShowErrors);

end;

procedure TFormBackpropTest.PrintSample(var P: TPrinter; Sample: TFeature; ShowError: Boolean);
var
  X, Y: Integer;
begin
  X := XOrig + Trunc(Sample.Vector[0] * XScale);
  Y := YOrig  + Trunc(Sample.Vector[1] * YScale);

  if ShowError then
    //if misclassified, mark it...
    if (Sample.OrigClass <> Sample.CompClass) and
       (Sample.CompClass >= 0 ) then begin
      P.Canvas.Pen.Color := Colors[Sample.OrigClass];
      P.Canvas.Rectangle(X-1, Y-1, X + 3, Y + 3);
    end;

  if Sample.CompClass >= 0 then
    P.Canvas.Pen.Color := Colors[Sample.CompClass]
  else
    P.Canvas.Pen.Color := Colors[Sample.OrigClass];

  P.Canvas.Ellipse(X, Y, X + 3, Y + 3);

end;

procedure TFormBackpropTest.ButtonCreateANNClick(Sender: TObject);
begin
  FeedForward := TFeedForward.Create([2,10,10,4], 1);
  DisplayNetwork;
  BuildDataSets;
  Randomize;
end;

procedure TFormBackpropTest.DisplayNet;
var
  X, Y,
  LayerNum, NodeNum, NextNodeNum: Integer;
begin
  for LayerNum := 0 to FeedForward.Layers.Count - 1 do begin
     X := 80 + LayerNum * 80;
    //iterate through the layer
    for NodeNum := 0 to TList(FeedForward.Layers.Items[LayerNum]).Count - 1 do begin
      Y := 80 + NodeNum * 30;
      Self.Canvas.Pen.Color := clRed;
      Self.Canvas.Ellipse(X, Y, X + 5, Y + 5);
    end;
  end;

end;

procedure TFormBackpropTest.FormDestroy(Sender: TObject);
begin
  FeedForward.Free;

end;

procedure TFormBackpropTest.DisplayNetwork;
var
  i, j, k: Integer;
  CurLayer: TNodeVector;
  CurNode: TNode;
  CurEdge: TEdge;
begin
  with FeedForward do begin
    for j := 0 to Layers.Count - 1 do begin
      CurLayer := TNodeVector(Layers.Items[j]);
      for i := 0 to CurLayer.Count - 1 do begin
        CurNode := TNode(CurLayer.Items[i]);
        if j < Layers.Count - 1 then  //if not the output layer
          for k := 0 to CurNode.EdgesOut.Count - 1 do begin
            CurEdge := TEdge(CurNode.EdgesOut.Items[k]);
          end

      end;
    end;
  end;
end;

procedure TFormBackpropTest.Train;
var
  i, j: Integer;
  C1, C2, C3, C4: Extended;
  Error: Extended;
begin
    j := Random(825);
    C1 := 0.001;
    C2 := 0.001;
    C3 := 0.001;
    C4 := 0.001;
    case TrainingSet[j].OrigClass of
      0: C1 := 0.999;
      1: C2 := 0.999;
      2: C3 := 0.999;
      3: C4 := 0.999;
    end;

    Error := FeedForward.Train([(TrainingSet[j].Vector[0]/100),
                       (TrainingSet[j].Vector[1]/100)],
                       [C1, C2, C3, C4]);

end;

function TFormBackpropTest.TrainEvenly(ErrorTolerance: Extended; Indx: Integer): Boolean;
var
  j: Integer;
  Error: array [0..3] of Extended;
begin
    j := Indx Mod (CLASS1COUNT div 2);
    Error[0] :=
    FeedForward.Train([(Class1[j].Vector[0])/100,
                       (Class1[j].Vector[1])/100],
                       [0.999, 0.001, 0.001, 0.001]);

    j := Indx Mod (CLASS2COUNT div 2);
    Error[1] :=
    FeedForward.Train([(Class2[j].Vector[0])/100,
                       (Class2[j].Vector[1])/100],
                       [0.001, 0.999, 0.001, 0.001]);

    j := Indx Mod (CLASS3COUNT div 2);
    Error[2] :=
    FeedForward.Train([(Class3[j].Vector[0])/100,
                       (Class3[j].Vector[1])/100],
                       [0.001, 0.001, 0.999, 0.001]);

    j := Indx Mod (CLASS4COUNT div 2);
    Error[3] :=
    FeedForward.Train([(Class4[j].Vector[0])/100,
                       (Class4[j].Vector[1])/100],
                       [0.001, 0.001, 0.001, 0.999]);

    Result := (Error[0] < ErrorTolerance) and
              (Error[1] < ErrorTolerance) and
              (Error[2] < ErrorTolerance) and
              (Error[3] < ErrorTolerance);

end;

procedure TFormBackpropTest.Recall;
var
  i, j: Integer;
  OutLayer: TNodeVector;
  MaxVal: Extended;
  MaxClass: Integer;
begin
  for i := Low(UnknownSet) to High(UnknownSet) do begin
    FeedForward.Feed([UnknownSet[i].Vector[0]/100, UnknownSet[i].Vector[1]/100]);
    OutLayer := FeedForward.Layers[FeedForward.Layers.Count - 1];

    //Find the maximun of the outputs of the network
    MaxVal := -99999999;
    for j := 0 to OutLayer.Count - 1 do begin
      if MaxVal < TNode(OutLayer.Items[j]).NOut then begin
        MaxVal := TNode(OutLayer.Items[j]).NOut;
        MaxClass := j;
      end;
    end;
    UnknownSet[i].CompClass := MaxClass;
  end;

  DisplayNetwork;

  DisplaySet(UnknownSet, True);

  DisplayResults(UnknownSet);
end;

procedure TFormBackpropTest.ButtonTrainOneClick(Sender: TObject);
begin
  TrainEvenly(StrToFloat(EditError.Text), Random(2000));
  DisplayNetwork;
end;

procedure TFormBackpropTest.ButtonRecallClick(Sender: TObject);
begin
  Recall;
end;

procedure TFormBackpropTest.ButtonTrainAllClick(Sender: TObject);
var
  i, Trial: Integer;
  E: Extended;
begin
  Trial := StrToInt(EditTrials.Text);
  ProgressBar1.Max := Trial;
  E := StrToFloat(EditError.Text);
  for i := 1 to Trial do begin
    Application.ProcessMessages;

    //if it has converged
    if TrainEvenly(E, i) then begin
      break;
    end;
    ProgressBar1.StepIt;
  end;
  LabelTrials.Caption := IntToStr(i);
  DisplayNetwork;
end;

procedure TFormBackpropTest.Button8Click(Sender: TObject);
begin
  FeedForward := TFeedForward.Create([5, 4], 1);
  DisplayNetwork;
  Randomize;
end;

procedure TFormBackpropTest.Button7Click(Sender: TObject);
var
  i, Trial: Integer;
  E: Extended;
begin
  Trial := StrToInt(EditTrials.Text);
  ProgressBar1.Max := Trial;
  E := StrToFloat(EditError.Text);
  for i := 1 to Trial do begin
    Application.ProcessMessages;

    //if it has converged
    if TrainAgent(E, i) then begin
      break;
    end;
    ProgressBar1.StepIt;
  end;
  LabelTrials.Caption := IntToStr(i);
  DisplayNetwork;
end;

function TFormBackpropTest.TrainAgent(ErrorTolerance: Extended; Indx: Integer): Boolean;
var
  j: Integer;
  Error: array [0..15] of Extended;
begin
    Error[0] :=
    FeedForward.Train([0.9, 0.1, 0.1, 0.1, 0.9],
                       [0.001, 0.001, 0.999, 0.999]);
    Error[1] :=
    FeedForward.Train([0.9, 0.9, 0.1, 0.1, 0.9],
                       [0.001, 0.001, 0.001, 0.999]);
    Error[2] :=
    FeedForward.Train([0.1, 0.9, 0.1, 0.1, 0.9],
                       [0.001, 0.999, 0.001, 0.999]);
    Error[3] :=
    FeedForward.Train([0.1, 0.9, 0.9, 0.1, 0.9],
                       [0.001, 0.999, 0.001, 0.001]);
    Error[4] :=
    FeedForward.Train([0.1, 0.1, 0.9, 0.1, 0.9],
                       [0.999, 0.999, 0.001, 0.001]);
    Error[5] :=
    FeedForward.Train([0.1, 0.1, 0.9, 0.9, 0.9],
                       [0.999, 0.001, 0.001, 0.001]);
    Error[6] :=
    FeedForward.Train([0.1, 0.1, 0.1, 0.9, 0.9],
                       [0.999, 0.001, 0.999, 0.001]);
    Error[7] :=
    FeedForward.Train([0.9, 0.1, 0.1, 0.9, 0.9],
                       [0.001, 0.001, 0.999, 0.001]);

    Error[8] :=
    FeedForward.Train([0.9, 0.1, 0.1, 0.1, 0.1],
                       [0.999, 0.999, 0.001, 0.001]);
    Error[9] :=
    FeedForward.Train([0.9, 0.9, 0.1, 0.1, 0.1],
                       [0.999, 0.001, 0.001, 0.001]);
    Error[10] :=
    FeedForward.Train([0.1, 0.9, 0.1, 0.1, 0.1],
                       [0.999, 0.001, 0.999, 0.001]);
    Error[11] :=
    FeedForward.Train([0.1, 0.9, 0.9, 0.1, 0.1],
                       [0.001, 0.001, 0.999, 0.001]);
    Error[12] :=
    FeedForward.Train([0.1, 0.1, 0.9, 0.1, 0.1],
                       [0.001, 0.001, 0.999, 0.999]);
    Error[13] :=
    FeedForward.Train([0.1, 0.1, 0.9, 0.9, 0.1],
                       [0.001, 0.001, 0.001, 0.999]);
    Error[14] :=
    FeedForward.Train([0.1, 0.1, 0.1, 0.9, 0.1],
                       [0.001, 0.999, 0.001, 0.999]);
    Error[15] :=
    FeedForward.Train([0.9, 0.1, 0.1, 0.9, 0.1],
                       [0.001, 0.999, 0.001, 0.001]);


    Result := (Error[0] < ErrorTolerance) and
              (Error[1] < ErrorTolerance) and
              (Error[2] < ErrorTolerance) and
              (Error[3] < ErrorTolerance) and
              (Error[4] < ErrorTolerance) and
              (Error[5] < ErrorTolerance) and
              (Error[6] < ErrorTolerance) and
              (Error[7] < ErrorTolerance) and
              (Error[8] < ErrorTolerance) and
              (Error[9] < ErrorTolerance) and
              (Error[10] < ErrorTolerance) and
              (Error[11] < ErrorTolerance) and
              (Error[12] < ErrorTolerance) and
              (Error[13] < ErrorTolerance) and
              (Error[14] < ErrorTolerance) and
              (Error[15] < ErrorTolerance);

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -