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
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
| DATA | Type | Default | Description |
|---|---|---|---|
oInputLayer | Object | TNeuralLayer for the input layer | |
aHiddenLayers | Array | {} | Array of TNeuralLayer objects (one or more hidden layers) |
oOutputLayer | Object | TNeuralLayer for the output layer | |
nLearningRate | Numeric | 0.1 | Learning rate for backpropagation weight updates |
TNeuralLayer
| DATA | Type | Default | Description |
|---|---|---|---|
aNeurons | Array | {} | Array of TNeuron objects in this layer |
TNeuron
| DATA | Type | Default | Description |
|---|---|---|---|
nBias | Numeric | hb_Random() | Bias term for this neuron |
aWeights | Array | Array of connection weights from the previous layer, initialized randomly | |
nValue | Numeric | Current activation value after forward propagation | |
nDeltaError | Numeric | Error gradient computed during backpropagation |
Methods
| Method | Description |
|---|---|
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
- Weights and biases are initialized to random values using
hb_Random(). - The sigmoid activation function maps values to the range (0, 1). The derivative is used during backpropagation for gradient computation.
- The
nLearningRatecontrols how much weights change during each update. Lower values (e.g. 0.01 - 0.1) provide more stable training, while higher values may converge faster but risk oscillation. - Multiple hidden layers can be created by passing an array with multiple values:
{ 4, 3 }creates two hidden layers with 4 and 3 neurons respectively. - This is a pure Harbour implementation with no external dependencies. The
Math_E()function is implemented via Harbour's C extension API. - For more advanced neural network capabilities, see the
Transformerclass insource/classes/transformer.prg.