Neural network code description for general image recognition

Source: Internet
Author: User

The Network format is defined by reading a file. The file format is as follows:

Input Image length input image width hidden layer neuron count output neuron count
Number of different network structures
[Number of hidden layer neurons connected at different locations]
[Position table of input neurons connected by hidden layer neurons]

The following is an example:

24 28 52 1
3
16 32
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
9 9 9 9 9 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
13 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16
4 8
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2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

Type

Tsingleextendedarray = array of extended;
Tdoubleextendedarray = array of extended;

Tsamples = packed record
INS: tsingleextendedarray;
Outs: tsingleextendedarray;
End;

Type

Tgraphicbpnn = Class
Private
Procedure backpropagate (T: tsingleextendedarray; n, m: Extended );
Function Update (inputs: tsingleextendedarray): extended;
Public
Samplecounts, testcounts: longint;
Procedure addtotrain (INS, outs: tsingleextendedarray );
Procedure addtotest (INS, outs: tsingleextendedarray );
Procedure savetofile (filename: string );
Procedure loadfromfile (filename: string );
Procedure train (n, m: Extended );
Function Init (filename: string): Boolean;
Function predict (INS: tsingleextendedarray): extended;
Function Test: extended;
Destructor destroy; override;
Private
Ni, NH, No: longint;
AI, ah, AO, output_deltas, hidden_deltas: tsingleextendedarray;
WI, wo, CI, CO: tdoubleextendedarray;
Connections: array of Boolean;
Samples: array of tsamples;
Testset: array of tsamples;
End;

Implementation

Function tgraphicbpnn. INIT (filename: string): Boolean;
VaR
I, J, K, Fi, FJ: longint;
NIW, institutes, ropmax, ropnum, roptypes: longint;
Ropmap: array of longint;
Begin
Assignfile (input, filename );
Reset (input );
Readln (input, NIW, institutes, NH, no );
Ni: = NIW * NI;
Setlength (AI, Ni );
Setlength (ah, NH );
Setlength (Ao, no );
For I: = 0 to ni-1 do Ai [I]: = 1;
For I: = 0 to NH-1 do Ah [I]: = 1;
For I: = 0 to no-1 do AO [I]: = 1;

Setlength (WI, Ni, NH );
Setlength (WO, NH, no );
Setlength (CI, Ni, NH );
Setlength (CO, NH, no );
Setlength (connections, Ni, NH );

For I: = 0 to ni-1 do
For J: = 0 to NH-1 do
Connections [I, j]: = false;

Readln (roptypes); FJ: = 0;
For K: = 1 to roptypes do begin
Readln (ropmax, ropnum );
Setlength (ropmap, Ni );
FI: = 0;
For I: = 1 to institutes do begin
For J: = 1 to NIW do begin
Read (ropmap [fi]);
INC (FI );
End;
Readln;
End;
FI: = 0;
For I: = 1 to ropnum do begin
INC (FI );
If fi> ropmax then FI: = 1;
For J: = 0 to ni-1 do
If ropmap [J] = Fi then connections [J, FJ]: = true;
INC (fj );
End;
End;

Setlength (output_deltas, no );
Setlength (hidden_deltas, NH );

Randomize;
For I: = 0 to ni-1 do
For J: = 0 to NH-1 do begin
CI [I, j]: = 0;
Wi [I, j]: = random (40000)/10000-2;
End;

For I: = 0 to NH-1 do
For J: = 0 to no-1 do begin
CO [I, j]: = 0;
WO [I, j]: = random (40000)/10000-2;
End;

Setlength (samples, $100); setlength (testset, $100 );
Samplecounts: = 0; testcounts: = 0;
Closefile (input );
End;

Procedure tgraphicbpnn. backpropagate (T: tsingleextendedarray; n, m: Extended );
VaR
I, J, K: longint;
Sum, change: extended;
Begin
For I: = 0 to no-1 do
Output_deltas [I]: = AO [I] * (1-ao [I]) * (T [I]-ao [I]);

For J: = 0 to NH-1 do begin
Sum: = 0;
For K: = 0 to no-1 do
Sum: = sum + output_deltas [k] * WO [j, k];
Hidden_deltas [J]: = Ah [J] * (1-Ah [J]) * sum;
End;

For J: = 0 to NH-1 do
For K: = 0 to no-1 do begin
Change: = output_deltas [k] * Ah [J];
WO [j, k]: = Wo [j, k] + N * change + M * CO [j, k];
CO [j, k]: = change;
End;

For I: = 0 to ni-1 do
For J: = 0 to NH-1 do
If connections [I, j] Then begin
Change: = hidden_deltas [J] * Ai [I];
Wi [I, j]: = wi [I, j] + N * change + M * CI [I, j];
CI [I, j]: = change;
End;

End;

Function tgraphicbpnn. Update (inputs: tsingleextendedarray): extended;
VaR
I, J, K: longint;
Sum: extended;
Begin
For I: = 0 to ni-1 do
Ai [I]: = inputs [I];
For J: = 0 to NH-1 do begin
Sum: = 0;
For I: = 0 to ni-1 do
If connections [I, j] Then
Sum: = sum + AI [I] * WI [I, j];
Ah [J]: = 1/(1 + exp (-sum ));
End;
For K: = 0 to no-1 do begin
Sum: = 0;
For J: = 0 to NH-1 do
Sum: = sum + Ah [J] * WO [j, k];
AO [k]: = 1/(1 + exp (-sum ));
End;
Update: = AO [0];
End;

Procedure tgraphicbpnn. Train (n, m: Extended );
VaR I: longint;
Begin
For I: = 0 to samplecounts-1 do begin
Update (samples [I]. INS );
Backpropagate (samples [I]. outs, n, m );
End;
End;

Procedure tgraphicbpnn. addtotrain (INS, outs: tsingleextendedarray );
VaR I: longint;
Begin
If samplecounts> high (samples) Then setlength (samples, samplecounts ++ $100 );
Setlength (samples [samplecounts]. INS, Ni );
Setlength (samples [samplecounts]. outs, no );
For I: = 0 to ni-1 do samples [samplecounts]. INS [I]: = INS [I];
For I: = 0 to no-1 do samples [samplecounts]. outs [I]: = outs [I];
INC (samplecounts );
End;

Procedure tgraphicbpnn. addtotest (INS, outs: tsingleextendedarray );
VaR I: longint;
Begin
If testcounts> high (testset) Then setlength (testset, testcounts + $100 );
Setlength (testset [testcounts]. INS, Ni );
Setlength (testset [testcounts]. outs, no );
For I: = 0 to ni-1 do testset [testcounts]. INS [I]: = INS [I];
For I: = 0 to no-1 do testset [testcounts]. outs [I]: = outs [I];
INC (testcounts );
End;

Procedure tgraphicbpnn. savetofile (filename: string );
VaR
I, J, K: longint;
Savestream: tmemorystream;
Begin
Savestream: = tmemorystream. Create;
Savestream. Seek (0, 0 );
For I: = 0 to ni-1 do
For J: = 0 to NH-1 do begin
Savestream. Write (WI [I, j], sizeof (WI [I, j]);
Savestream. Write (CI [I, j], sizeof (CI [I, j]);
End;
For J: = 0 to NH-1 do
For K: = 0 to no-1 do begin
Savestream. Write (WO [J, K], sizeof (WO [j, k]);
Savestream. Write (CO [J, K], sizeof (CO [j, k]);
End;
Savestream. savetofile (filename );
Savestream. Free;
End;

Procedure tgraphicbpnn. loadfromfile (filename: string );
VaR
I, J, K: longint;
Readstream: tmemorystream;
Begin
Readstream: = tmemorystream. Create;
Readstream. loadfromfile (filename );
Readstream. Seek (0, 0 );
For I: = 0 to ni-1 do
For J: = 0 to NH-1 do begin
Readstream. Read (WI [I, j], sizeof (WI [I, j]);
Readstream. Read (CI [I, j], sizeof (CI [I, j]);
End;
For J: = 0 to NH-1 do
For K: = 0 to no-1 do begin
Readstream. Read (WO [J, K], sizeof (WO [j, k]);
Readstream. Read (CO [J, K], sizeof (CO [j, k]);
End;
Readstream. Free;
End;

Function tgraphicbpnn. predict (INS: tsingleextendedarray): extended;
Begin
Try
Predict: = Update (INS );
Except
Predict: = 0;
End;
End;

Function tgraphicbpnn. Test: extended;
VaR
Preret: extended;
I, counts, RET: longint;
Begin
Counts: = 0;
For I: = 0 to testcounts-1 do begin
Preret: = predict (testset [I]. INS );
If preret> 0.5 then RET: = 1 else RET: = 0;
If ret = testset [I]. outs [0] Then Inc (counts );
End;
Result: = counts/testcounts;
End;

Destructor tgraphicbpnn. Destroy;
Begin
Setlength (AI, 0 );
Setlength (ah, 0 );
Setlength (Ao, 0 );
Setlength (output_deltas, 0 );
Setlength (hidden_deltas, 0 );
Setlength (WI, 0, 0 );
Setlength (WO, 0, 0 );
Setlength (CI, 0, 0 );
Setlength (CO, 0, 0 );
Setlength (connections, 0, 0 );
Setlength (samples, 0 );
Inherited;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.