http://blog.csdn.net/hustjoyboy/article/details/50721535Delphi version of "how to implement deep neural network algorithms with 70 lines of Java code"2016-02-23 10:58 225 people read comments (0) favorite reports
Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.
=====ann.pas source program ===================================
{
by Forrest Gump 2016.2.23
Reference from this document
How to implement deep neural network algorithm with 70 lines of Java code
http://geek.csdn.net/news/detail/56086
Code in the original Author: Fourinone
The code in the original is written in Java and is now ported to Delphi for Reference.
Slight modification: weight does not include output layer
}
Unit ann;
Interface
Type
Tdbarr=array of double;
Tbpdeep=class
Private
Layer:array of array of double;
Layererr:array of array of double;
Layer_weight:array of array of array of double;
Layer_weight_delta:array of array of array of double;
mobp,rate:double;
Procedure Updateweight (tar:array of double);
Public
Constructor Create (layernum:array of integer;rate,mobp:double);
function Computeout (input:array of double): tdbarr;
Procedure Train (input,tar:array of double);
End
Implementation
Constructor Tbpdeep.create (layernum:array of integer;rate,mobp:double);
Var
a,i,j,k:integer;
Begin
self.rate:=rate;
self.mobp:=mobp;
A:=length (layernum);
If A<2 then exit;
SetLength (layer,a);
SetLength (layererr,a);
SetLength (layer_weight,a-1);
SetLength (layer_weight_delta,a-1);
Randomize;
For K:=0 to A-1 do
Begin
SetLength (layer[k],layernum[k]);
SetLength (layererr[k],layernum[k]);
If K+1<a Then
Begin
SetLength (layer_weight[k],layernum[k]+1,layernum[k+1]);
SetLength (layer_weight_delta[k],layernum[k]+1,layernum[k+1]);
For j:=0 to Layernum[k]-1 do
For i:=0 to Layernum[k+1]-1 do
Layer_weight[k][j][i]:=random;//random Initialization weights
End
End
End
function Tbpdeep.computeout (input:array of double): tdbarr;
Var
i,j,k:integer;
z:double;
Begin
For k:=1 to High (layer) do
For j:=0 to High (layer[k]) do
Begin
Z:=layer_weight[k-1][length (layer[k-1])][j];
For i:=0 to High (layer[k-1]) do
Begin
If K=1 then layer[k-1][i]:=input[i];
z:=z+layer_weight[k-1][i][j]*layer[k-1][i];
End
Layer[k][j]:=1/(1+exp (-z));
End
Result:=tdbarr (layer[high (layer)]);
End
Procedure Tbpdeep.updateweight (tar:array of double);
Var
i,j,k:integer;
z:double;
Begin
K:=high (layer);
For j:=0 to High (layererr[k]) do layererr[k][j]:=layer[k][j]* (1-layer[k][j]) * (tar[j]-layer[k][j]);
While k>0 do
Begin
Dec (k);
For j:=0 to High (layererr[k]) do
Begin
z:=0;
For i:=0 to High (layererr[k+1]) do
Begin
If Z+k>0 then z:=layererr[k+1][i]*layer_weight[k][j][i]
else z:=0;
Layer_weight_delta[k][j][i]:=mobp*layer_weight_delta[k][j][i]+rate*layererr[k+1][i]*layer[k][j];//hidden Layer Momentum Adjustment
Layer_weight[k][j][i]:=layer_weight[k][j][i]+layer_weight_delta[k][j][i];//hidden Layer Weight Adjustment
If J=high (layererr[k]) Then
Begin
Layer_weight_delta[k][j+1][i]:=mobp*layer_weight_delta[k][j+1][i]+rate*layererr[k+1][i];//intercept Momentum Adjustment
Layer_weight[k][j+1][i]:=layer_weight[k][j+1][i]+layer_weight_delta[k][j+1][i];//intercept Weight Adjustment
End
End
layererr[k][j]:=z*layer[k][j]* (1-layer[k][j]);//record Error
End
End
End
Procedure Tbpdeep.train (input,tar:array of double);
Begin
Computeout (input);
Updateweight (tar);
End
End.
========================================
Test code:
Procedure Tform1.bttrainclick (sender:tobject);//training
Var
i,k:integer;
Const
Data:array[0..3] of array[0..1] of double= ((+), (2,2), (), (2,1));
Tar:array[0..3] of array[0..1] of double= (1,0), (0,1), (0,1), (1,0));
Begin
For k:=0 to 499 do
For i:=0 to High (data) does
Bp.train (data[i], tar[i]);
End
Procedure Tform1.btcomputeclick (sender:tobject);//identification
Var
rst:tdbarr;
X:array[0..1] of double;
Begin
X[0]:=strtofloat (xx. Text);
X[1]:=strtofloat (yy. Text);
Rst:=bp.computeout (x);
memo1. Lines.append (floattostr (rst[0]) + "+floattostr (rst[1]));
End
Procedure Tform1.formcreate (sender:tobject);//initialization
Begin
Bp:=tbpdeep.create ([2,10,2],0.15,0.8);
End
=====================================
How to implement the Delphi version of the Deep Neural network algorithm with 70 lines of Java code