All Bundles

NeuralNetwork

Simple neural network. Input values should be scaled to between 0.0 to 1.0. The tuned network should return outputs between 0.0 to 1.0.

Operations

Code example:

network : NeuralNetwork;
filename := "data/model.dat";
inputs_targets := MatrixReader->LoadSplitMatrices(args[0], 1, 0.8); # 20% test data

# load model
if(args->Size() = 2) {
   network := NeuralNetwork->Load(filename);
   "Loaded model..."->PrintLine();

   "Testing model..."->PrintLine();
   tests := inputs_targets[2];
   answers := inputs_targets[3];

   failures := 0;
   each(i : answers) {
      answer := answers->Get(i)->ToBool();
      predict := network->Query(FloatMatrixRef->New(tests->Get(i)->Get()));
      if(predict <> answer) {
         failures += 1;
      };
   };

   correct := 100.0 * (1.0 - failures->As(Float) / tests->Size()->As(Float));
   System.IO.Console->Print("Tests: ")->Print(tests->Size())->Print(", correct: ")->SetFloatPrecision(5)->Print(correct)->PrintLine("%");
}
# train and store model
else if(args->Size() = 1) {
   "Training model..."->PrintLine();
   network := NeuralNetwork->Train(2, inputs_targets[0], 8, 1, inputs_targets[1], 0.01725, 256);
   if(inputs_targets <> Nil) {
      network->Store(filename);
      "Stored model..."->PrintLine();
   };
}

Confidence

Query the network's confidence for the give inputs

method : public : Confidence(inputs:FloatMatrixRef) ~ Float
Parameters
NameTypeDescription
inputsFloatMatrixRefquery inputs

Return
TypeDescription
Floatconfidence percentage

Load

Loads network inputs and outputs

function : Load(filename:String) ~ NeuralNetwork
Parameters
NameTypeDescription
filenameStringfile to store to

Return
TypeDescription
NeuralNetworktrue if successful, false otherwise

Query

Query the network

method : public : Query(inputs:FloatMatrixRef) ~ Bool
Parameters
NameTypeDescription
inputsFloatMatrixRefquery inputs

Return
TypeDescription
Booltrue if activated, false otherwise

SetActivation

Sets the activation threshold. Activation if output > threshold or < 1.0 - threshold

method : public : SetActivation(threshold:Float, attempts:Int) ~ Nil
Parameters
NameTypeDescription
thresholdFloatactivation threshold default is 0.85
attemptsIntnumber of activation attempts

Store

Saves final network inputs and outputs

method : public : Store(filename:String) ~ Bool
Parameters
NameTypeDescription
filenameStringfile to store to

Return
TypeDescription
Booltrue if successful, false otherwise

Train

Trains the network

function : Train(input_nodes:Int, inputs:Vector<FloatMatrixRef>, hidden_factor:Int, output_nodes:Int, targets:Vector<FloatMatrixRef>, learning_rate:Float, iterations:Int) ~ NeuralNetwork
Parameters
NameTypeDescription
input_nodesIntnumber of input nodes
inputsVector<FloatMatrixRef>: training inputs
hidden_factorIntsize of hidden layer, factor of input (i.e. input_nodes * hidden_factor)
output_nodesInttraining outputs
targetsVector<FloatMatrixRef>training targets
learning_rateFloatlearning rate
iterationsIntnumber of training iterations