Advertisement
dondonondon

Proses Train

Nov 21st, 2020
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.40 KB | None | 0 0
  1. procedure fnProsesTrain;
  2. var ii, jj, kk, nn, i, xx: integer;
  3.     CT, Y2, net, D2, deltaB2, MSEchek, tempY2: Double;
  4.     error, sumError, PE, sumPE, MAPE, aa, bb: Double;
  5.     CP, Y1, D1, deltaW1, deltaW2, deltaB1, D1_net, tempNet: array of array of Double;
  6.     Baris, Kolom: Word;
  7.     themp: array of array of Double;
  8.     jmlParam : Integer;
  9. begin
  10.   sTrain := True;
  11.   sSave := False;
  12.   try
  13.     targetErr   := StrToFloatDef(FTrain.edTargetError.Text, 0.0000001);
  14.     alfa        := StrToFloatDef(FTrain.edLearningRate.Text, 0);
  15.     maxEpoch    := StrToIntDef(FTrain.edEpoch.Text, 0);
  16.     kolTarget   := nodeInput + 1;
  17.  
  18.     fnSetBobot;
  19.  
  20.     sumError    := 0;
  21.     MSEchek     := 1;
  22.     Epoch       := 0;
  23.  
  24.     SetLength(MSE, 1, maxEpoch + 1);
  25.     SetLength(Y2akhir, tLatih, 2);
  26.     SetLength(tempArrYAkhir, tLatih, 2);
  27.  
  28.     TThread.Synchronize(nil, procedure begin
  29.       FTrain.pbTrain.Max := maxEpoch;
  30.       FTrain.pbTrain.Min := 0;
  31.       FTrain.pbTrain.Value := 0;
  32.       FTrain.tiPrintData.Enabled := True;
  33.       FTrain.tiAutoSave.Enabled := True;
  34.     end);
  35.  
  36.     while (Epoch < maxEpoch) do begin
  37.       if (MSEchek > targetErr) then begin
  38.         Inc(Epoch);
  39.         errTrain := 0;
  40.         for kk := 0 to tLatih - 1 do begin
  41.           SetLength(CP, 1, nodeInput);
  42.           for Kolom := 0 to nodeInput - 1 do
  43.             CP[0, Kolom]:= DtLatihNorm[kk, Kolom];
  44.  
  45.           CT := DtLatihNorm[kk, kolTarget - 1];
  46.  
  47.           SetLength(themp, 1, nodeInput);
  48.           SetLength(Y1, 1, nodeHidden);
  49.  
  50.           for ii:= 0 to nodeHidden - 1 do begin
  51.             net:= 0;
  52.             for kolom:= 0 to nodeInput - 1 do begin
  53.               themp[0, Kolom] := CP[0, Kolom] * w1[ii, Kolom];
  54.               net := net + themp[0, Kolom];
  55.             end;
  56.  
  57.             net:= net + b1[ii, 0];
  58.             Y1[0, ii] := Sigmoid(net);
  59.           end;
  60.  
  61.           SetLength(themp, 1, nodeHidden);
  62.           for ii:= 0 to nodeOutput - 1 do begin
  63.             net:= 0;
  64.             for Kolom := 0 to nodeHidden - 1 do begin
  65.               themp[0, Kolom] := Y1[0, Kolom] * w2[ii, Kolom];
  66.               net := net + themp[0, Kolom];
  67.             end;
  68.             net:= net + b2;
  69.             Y2 := Sigmoid(net);
  70.           end;
  71.  
  72.           Y2akhir[kk, 0] := CT;
  73.           Y2akhir[kk, 1] := Y2;
  74.  
  75.           //menghitung error training
  76.           error:= CT - Y2;
  77.  
  78.           //menghitung rata-rata error dari semua node per epoch
  79.           sumError := sum(error);
  80.           errTrain:= errTrain + sqr(sumError/1);
  81.  
  82.           //menghitung faktor koreksi pada tiap neuron di output    //turunan
  83.           D2:= error * Y2 * (1 - Y2);  //sigmoid
  84.  
  85.           //menghitung delta w2
  86.           SetLength(deltaW2, nodeOutput, nodeHidden);
  87.           for jj:= 0 to nodeHidden-1 do begin
  88.             for nn:= 0 to nodeOutput-1 do begin
  89.               deltaW2[nn, jj]:= alfa * (D2 * Y1[0,jj]);
  90.             end;
  91.           end;
  92.  
  93.           //menghitung delta b2
  94.           for nn:= 0 to nodeOutput-1 do begin
  95.             deltaB2:= alfa * D2;
  96.           end;
  97.  
  98.           //menghitung faktor koreksi pada tiap neuron di hidden
  99.           SetLength(D1_net, 1, nodeHidden);
  100.           SetLength(D1, 1, nodeHidden);
  101.           for jj:= 0 to nodeHidden-1 do begin
  102.             D1_net[0, jj]:= D2 * w2[0,jj];
  103.           end;
  104.  
  105.           for Kolom:= 0 to nodeHidden-1 do                   //turunan
  106.             D1[0, kolom]:= D1_net[0, Kolom] * Y1[0, Kolom] * (1-Y1[0, Kolom]);
  107.  
  108.           //menghitung delta w1
  109.           SetLength(deltaW1, nodeHidden, nodeInput);
  110.           for jj:= 0 to nodeInput-1 do begin
  111.             for nn:=0 to nodeHidden-1 do begin
  112.               deltaW1[nn, jj]:= alfa * (D1[0, nn] * CP[0, jj]);
  113.             end;
  114.           end;
  115.  
  116.           //menghitung delta b1
  117.           SetLength(deltaB1, nodeHidden, 1);
  118.           for nn:= 0 to nodeHidden-1 do begin
  119.             deltaB1[nn, 0] := alfa * D1[0, nn];
  120.           end;
  121.  
  122.           //update semua bobot dan bias
  123.           for Baris:= 0 to nodeHidden-1 do
  124.             for Kolom:= 0 to nodeInput-1 do
  125.               w1[Baris, Kolom] := w1[Baris, Kolom] + deltaW1[Baris, Kolom];
  126.  
  127.           for Baris:= 0 to nodeOutput-1 do
  128.             for Kolom:= 0 to nodeHidden-1 do
  129.               w2[Baris, Kolom] := w2[Baris, Kolom] + deltaW2[Baris, Kolom];
  130.  
  131.           for Baris:= 0 to nodeHidden-1 do
  132.             for Kolom:= 0 to nodeOutput-1 do
  133.               b1[Baris, Kolom] := b1[Baris, Kolom] + deltaB1[Baris, Kolom];
  134.  
  135.           b2:= b2 + deltaB2;
  136.         end;
  137.  
  138.         //SetLength(MSE, 1, maxEpoch + 1);
  139.         MSE[0, Epoch-1]:= (errTrain / tLatih);
  140.         MSEchek:= errTrain / tLatih;
  141.         MSEterakhir:= MSE[0, Epoch-1];
  142.  
  143.         if sAutoSave then
  144.           fnSimpanParameter;
  145.  
  146.         if not sTrain then
  147.           Break;
  148.  
  149.       end else begin
  150.         Break;
  151.       end;
  152.     end;
  153.   finally
  154.     sTrain := False;
  155.     TThread.Synchronize(nil, procedure begin
  156.       fnHasilTrain;
  157.       fnProsesTest;
  158.       fnHasilTest;
  159.  
  160.       if FTrain.cbSimpanBerkala.IsChecked then
  161.         fnSimpanParameter;
  162.  
  163.       FTrain.btnSimpan.Enabled := True;
  164.       FTrain.btnProses.Enabled := True;
  165.       FTrain.btnProsesData.Enabled := True;
  166.       FTrain.fnSetEnabledInput(True);
  167.       FTrain.tiPrintData.Enabled := False;
  168.       FTrain.tiAutoSave.Enabled := False;
  169.  
  170.       FTrain.pbTrain.Value := Epoch;
  171.       FTrain.lblEpoch.Text := Epoch.ToString + ' / ' + maxEpoch.ToString;
  172.     end);
  173.   end;
  174. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement