/// <summary>Classify one sample in the machine using the perceptron algorithm (used after training).</summary> /// <param name="input">The sample to classify</param> public override double classify(double[] input) { if (input.Length > this.featureMask.Length) { throw new ArgumentOutOfRangeException("Input features is bigger than features in feature mask"); } input = VectorTools.prepend(input, this.bias); //Add the bias. double net = VectorTools.multiply(this.weight, input); int sgnOut = ActivationFunctions.signum(net); return(sgnOut); }
/// <summary>Train the machine using the perceptron algorithm.</summary> /// <param name="trainCount">Number of data set smaples to use in training.</param> public override void train(int trainCount) { int epochs = 0; bool weightChanged = true; int classIndex; double[] lineData; /*REAL WORK*/ while (weightChanged && epochs < NeuralNetwork.MAX_EPOCHS) { weightChanged = false; for (int i = 0; i < this.classMask.Length; i++) //Class index. { classIndex = (int)classMask[i] - 1; for (int j = 0; j < trainCount; j++) //Data index. { lineData = VectorTools.trim(this.data[classIndex][j], this.featureMask); lineData = VectorTools.prepend(lineData, this.bias); //Prepend the bias. double net = VectorTools.multiply(this.weight, lineData); int sgnOut = ActivationFunctions.signum(net); if (sgnOut != this.target[i]) { weightChanged = true; lineData = VectorTools.trim(this.data[classIndex][j], this.featureMask); lineData = VectorTools.prepend(lineData, this.bias); double error = this.target[i] - sgnOut; double[] mulOut = VectorTools.multiply(lineData, error * this.eta); this.weight = VectorTools.sum(this.weight, mulOut); } } } //End of inner for. epochs++; } //End of outer while. }
public Layer(List <Neuron> neurons, ActivationFunctions activationFunction) { this.Neurons = neurons; this.activationFunction = activationFunction; }
public Layer(int numNeurons, ActivationFunctions activationFunction) { this.activationFunction = activationFunction; Neurons = new List <Neuron>(numNeurons); }