public override IScore Run(double[][] featureValues, IContext ctx) { m_Dimensions = ctx.DataDescriptor.Features.Count(); int numOfInputVectors = featureValues.Length; if (m_Weights == null) { m_Weights = new double[m_Dimensions]; initializeWeights(); } //m_Errors = new double[numOfInputVectors]; double lastError = 0; double totalError = 0.0; var score = new PerceptronAlgorithmScore(); for (int i = 0; i < m_Iterations; i++) { double maxError = 0; totalError = 0; for (int inputVectIndx = 0; inputVectIndx < numOfInputVectors; inputVectIndx++) { // Calculate the output value with current weights. double calculatedOutput = calculateResult(featureValues[inputVectIndx], m_Dimensions); // Get expected output. double expectedOutput = featureValues[inputVectIndx][ctx.DataDescriptor.LabelIndex]; // Error is difference between calculated output and expected output. double error = expectedOutput - calculatedOutput; // Total error for all input vectors inside of a single iteration. totalError += Math.Abs(error); if (this.m_TraceTotalError) { if (maxError < totalError) { maxError = totalError; } else if (inputVectIndx == numOfInputVectors - 1) { lastError = totalError; } } if (error != 0) { // Y = W * X // error = expectedOutput - calculatedOutput // W = Y/X // // Updating of weights for (int dimensionIndx = 0; dimensionIndx < m_Dimensions; dimensionIndx++) { double delta = m_LearningRate * featureValues[inputVectIndx][dimensionIndx] * error; m_Weights[dimensionIndx] += delta; } } // // Updating of threshold this.m_Threshold += this.m_LearningRate * error; //if (totalError == 0 && i >= (m_Iterations * 0.1) // && inputVectIndx >= (numOfInputVectors * 0.1)) // We let it calculate at least 10% of max iterations // break; } if (this.m_TraceTotalError) { Debug.WriteLine($"Interation: {i}\tmaxError:{maxError}\tlastErr:{lastError}"); } if (totalError == 0 && i >= (m_Iterations * 0.1)) // We let it calculate at least 10% of max iterations { break; } //if (totalError == 0) // break; } score.Weights = this.m_Weights; //score.Errors = this.m_Errors;//numberofsample score.TotolEpochError = totalError;//all 0 ctx.Score = score; return(ctx.Score); }
//private bool m_PersistConvergenceData = false; public PerceptronAlgorithm(PerceptronAlgorithmScore score) { }