TNeuralNetwork

Source: source/classes/tneuralnet.prg

TNeuralNetwork is FiveWin's feedforward neural network implemented in pure Harbour. It provides a multi-layer perceptron (MLP) with backpropagation training. The network consists of three layer types: an input layer, one or more hidden layers, and an output layer. Each neuron uses the sigmoid activation function. The class is ideal for simple pattern recognition tasks such as the XOR problem.

Architecture

flowchart LR subgraph "TNeuralNetwork" A[oInputLayer
TNeuralLayer] B[aHiddenLayers
Array of TNeuralLayer] C[oOutputLayer
TNeuralLayer] end subgraph "TNeuralLayer" D[aNeurons
Array of TNeuron] end subgraph "TNeuron" E[nBias] F[aWeights] G[nValue] H[nDeltaError] end A --> D B --> D C --> D D --> E D --> F D --> G D --> H

Key DATA Members

TNeuralNetwork

DATATypeDefaultDescription
oInputLayerObjectTNeuralLayer for the input layer
aHiddenLayersArray{}Array of TNeuralLayer objects (one or more hidden layers)
oOutputLayerObjectTNeuralLayer for the output layer
nLearningRateNumeric0.1Learning rate for backpropagation weight updates

TNeuralLayer

DATATypeDefaultDescription
aNeuronsArray{}Array of TNeuron objects in this layer

TNeuron

DATATypeDefaultDescription
nBiasNumerichb_Random()Bias term for this neuron
aWeightsArrayArray of connection weights from the previous layer, initialized randomly
nValueNumericCurrent activation value after forward propagation
nDeltaErrorNumericError gradient computed during backpropagation

Methods

MethodDescription
New( nInputs, aHiddenLayers, nOutputs )Create a neural network with nInputs input neurons, an array of hidden layer sizes (e.g. { 4, 3 } for two hidden layers), and nOutputs output neurons.
Learn( aInputs, aOutputs, lDebug )Train the network on one sample: runs forward propagation, then backpropagation to adjust weights and biases.
Propagation( aInputs )Run forward propagation: compute activation values from input through hidden layers to output. Uses the sigmoid activation function.
BackPropagation( aInputs, aOutputs )Run backward propagation: compute error gradients and update all weights and biases based on the learning rate.

Example: XOR Training

#include "FiveWin.ch"

function Main()

   local oNN, n

   // Network: 2 inputs, hidden layer with 2 neurons, 1 output
   oNN := TNeuralNetwork():New( 2, { 2 }, 1 )

   // Train for 10000 iterations
   for n = 1 to 10000
      oNN:Learn( { 0, 0 }, { 0 } )
      oNN:Learn( { 1, 0 }, { 1 } )
      oNN:Learn( { 0, 1 }, { 1 } )
      oNN:Learn( { 1, 1 }, { 0 } )
   next

   // Test the trained network
   ? "XOR Truth Table:"
   ? "0 XOR 0 =", oNN:Propagation( { 0, 0 } )[ 1 ]  // ~0.0
   ? "1 XOR 0 =", oNN:Propagation( { 1, 0 } )[ 1 ]  // ~1.0
   ? "0 XOR 1 =", oNN:Propagation( { 0, 1 } )[ 1 ]  // ~1.0
   ? "1 XOR 1 =", oNN:Propagation( { 1, 1 } )[ 1 ]  // ~0.0

return nil

Example: Training with Debug Output

#include "FiveWin.ch"

function Main()

   local oNN := TNeuralNetwork():New( 2, { 4 }, 1 )

   // Debug mode prints inputs, outputs, expected values, and errors
   oNN:Learn( { 1, 0 }, { 1 }, .T. )

return nil

Notes

See Also