unit NeurNet; {Neural network, layered structure, feed-forward} Interface uses FileInp, Basic; const xMaxLayerCount = 3; xMaxNeuronsPerLayer = 15; type t_NeuralNetwork = ^tNeuralNetwork; tNeuralNetwork = object {network parameters} cNeuronCounts: array [1..xMaxLayerCount] of Integer; cSigmoidParameter: Real; {determines shape of the transfer function} cLayerCount: Integer; {} cWeights: array [1..xMaxLayerCount, 1..xMaxNeuronsPerLayer, 1..xMaxNeuronsPerLayer ] of Real; cActivities: array [1..xMaxLayerCount, 1..xMaxNeuronsPerLayer ] of Real; cThresholds: array [1..xMaxLayerCount, 1..xMaxNeuronsPerLayer ] of Real; {} function Weight( pLayer1, pN1, pN2: Integer ): Real; {weight asociated to w(pLayer1, pN1)-w(pLayer1+1, pN2) connection} function Threshold( pLayer, pN: Integer ): Real; {threshold coefficient of a given neuron} function TransferFunction( p: Real ): Real; function Activity( pLayer, pN: Integer ): Real; procedure SetActivity( pLayer, pN: Integer; pActivity: Real ); procedure ProcessNeuron( pLayer, pN: Integer ); {counts activity of a given neuron} procedure ProcessNetwork; {produces output of a network on a given input} {} function LayerCount: Integer; function NeuronCount( pLayer: Integer ): Integer; procedure SetWeight( pLayer1, pN1, pN2: Integer; pWeight: Real ); function ReadParams( psFileName: String ): Boolean; end; Implementation function tNeuralNetwork.Weight( pLayer1, pN1, pN2: Integer ): Real; begin Weight := cWeights[ pLayer1, pN1, pN2 ]; end; function tNeuralNetwork.Threshold( pLayer, pN: Integer ): Real; begin Threshold := cThresholds[ pLayer, pN ]; end; function tNeuralNetwork.TransferFunction( p: Real ): Real; begin {sigmoid} TransferFunction := 1/(1+exp(p*cSigmoidParameter)) end; function tNeuralNetwork.Activity( pLayer, pN: Integer ): Real; begin Activity := cActivities[ pLayer, pN ]; end; procedure tNeuralNetwork.SetActivity( pLayer, pN: Integer; pActivity: Real ); begin cActivities[ pLayer, pN ] := pActivity; end; procedure tNeuralNetwork.ProcessNeuron( pLayer, pN: Integer ); var ActivityIn: Real; i: Integer; begin if (pLayer > 1) and (pLayer <= LayerCount) then begin ActivityIn := 0; for i := 1 to NeuronCount( pLayer-1 ) do begin ActivityIn := ActivityIn + Weight( pLayer-1, i, pN )*Activity( pLayer-1, i ); end; ActivityIn := ActivityIn + Threshold( pLayer, pN ); SetActivity( pLayer, pN, TransferFunction( ActivityIn ) ); end; end; procedure tNeuralNetwork. ProcessNetwork; var i, j: Integer; begin for i := 2 to LayerCount do for j := 1 to NeuronCount( i ) do ProcessNeuron( i, j ); end; function tNeuralNetwork.LayerCount: Integer; begin LayerCount := cLayerCount; end; function tNeuralNetwork.NeuronCount( pLayer: Integer ): Integer; begin NeuronCount := cNeuronCounts[ pLayer ]; end; procedure tNeuralNetwork.SetWeight( pLayer1, pN1, pN2: Integer; pWeight: Real ); begin cWeights[ pLayer1, pN1, pN2 ] := pWeight; end; function tNeuralNetwork.ReadParams( psFileName: String ): Boolean; var f: tFile; begin if f.Open( psFileName ) and f.SeekSection( '[NEURALNETWORK]' ) then begin cNeuronCounts[1] := StrToInt( f.GetValue ); cNeuronCounts[2] := StrToInt( f.GetValue ); cNeuronCounts[3] := StrToInt( f.GetValue ); cSigmoidParameter := StrToReal( f.GetValue ); cLayerCount := StrToInt( f.GetValue ); ReadParams := True; end else ReadParams := False; f.close; end; begin end.