public void Test_FunctionRecognitionModuleSave(int MinNoiseForPrediction, int MaxNoiseForPrediction) { #region Train and Save var batch = 100; var funcData = FunctionGenerator.CreateFunction(500, 2, 2 * Math.PI / 100); LearningApi api = new LearningApi(); api.UseActionModule <object, double[][]>((notUsed, ctx) => { var similarFuncData = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), 7, 10); double[][] formattedData = formatData(similarFuncData); return(formattedData); }); double[][] initCentroids = new double[4][]; initCentroids[0] = new double[] { 1.53, 0.63 }; initCentroids[1] = new double[] { 4.68, -0.63 }; initCentroids[2] = new double[] { 7.85, 0.62 }; initCentroids[3] = new double[] { 10.99, -0.64 }; ClusteringSettings settings = new ClusteringSettings(0, numClusters: 4, numDims: 2, KmeansAlgorithm: 2, initialCentroids: initCentroids, tolerance: 0) { KmeansMaxIterations = 1000 }; api.UseKMeansFunctionRecognitionModule(settings); KMeansFunctionRecognitonScore res; while (batch-- > 0) { res = api.RunBatch() as KMeansFunctionRecognitonScore; } api.Save("sinusmodel"); #endregion #region Load And Predict var api2 = LearningApi.Load("sinusmodel"); var noisedFunc = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), MinNoiseForPrediction, MaxNoiseForPrediction); double[][] data = formatData(noisedFunc); var predictionResult = api2.Algorithm.Predict(data, null) as KMeansFunctionRecognitionResult; // TRUE positives if (MaxNoiseForPrediction <= 10) { Assert.True(predictionResult.Loss == 1.0); } // TRUE negatives else if (MaxNoiseForPrediction >= 25) { Assert.False(predictionResult.Loss == 1.0); } #endregion }
public void Train(List <Point> points) { //double[][] initCentroids = new double[2][]; //initCentroids[0] = new double[] { points.First().X, points.First().Y }; //initCentroids[1] = new double[] { points.Last().X, points.Last().Y }; //m_Settings.InitialCentroids = initCentroids; m_CurrentData = getDataFromPoints(points); KMeansFunctionRecognitonScore res = m_LearningApi.RunBatch() as KMeansFunctionRecognitonScore; }
/// <summary> /// Trains the K-Means algorithm using coordinates. /// </summary> /// <param name="points">Coordinate Points</param> public void Train(List <Coordinates> points) { m_CurrentData = GetDataFromPoints(points); KMeansFunctionRecognitonScore res = m_LearningApi.RunBatch() as KMeansFunctionRecognitonScore; }
public void BatchingTest() { LearningApi api = new LearningApi(); api.UseActionModule <object, double[][]>((notUsed, ctx) => { const int batchSize = 500; const int maxSamples = 10000; ctx.DataDescriptor = getDescriptor(); if (data == null) { data = new double[maxSamples][]; // // We generate following input vectors: // IN Val - Expected OUT Val // 1 - 0 // 2 - 0, // ... // maxSamples / 2 - 1, // maxSamples / 2 + 1 - 1, // maxSamples / 2 + 2 - 1, for (int i = 0; i < maxSamples; i++) { data[i] = new double[2]; data[i][0] = i; data[i][1] = (i > (maxSamples / 2)) ? 1 : 0; } } if (currentBatch < maxSamples / batchSize) { List <double[]> batch = new List <double[]>(); batch.AddRange(data.Skip(currentBatch * batchSize).Take(batchSize)); ctx.IsMoreDataAvailable = true; currentBatch++; return(batch.ToArray()); } else { ctx.IsMoreDataAvailable = false; return(null); } }); api.UsePerceptron(0.02, 10000, traceTotalError: true); IScore score = api.RunBatch() as IScore; double[][] testData = new double[4][]; testData[0] = new double[] { 2.0, 0.0 }; testData[1] = new double[] { 2000.0, 0.0 }; testData[2] = new double[] { 6000.0, 0.0 }; testData[3] = new double[] { 5001, 0.0 }; var result = api.Algorithm.Predict(testData, api.Context) as PerceptronResult; Assert.True(result.PredictedValues[0] == 0); Assert.True(result.PredictedValues[1] == 0); Assert.True(result.PredictedValues[2] == 1); Assert.True(result.PredictedValues[3] == 1); }
public void Test_FunctionRecognitionModule_CustomFunc() { #region Training int points = 22; var batch = 100; /// If 2 dimensions are used, then data is formatted as: /// ret[dim1] = {x1,x2,..xN}, /// ret[dim2] = {y1,y2,..,yN}, /// Every dimension is returned as a row. Poinst of dimension are cells. var funcData = FunctionGenerator.CreateFunction(points, 2, 1, customFunc1); LearningApi api = new LearningApi(); api.UseActionModule <object, double[][]>((notUsed, ctx) => { var similarFuncData = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), 7, 10); // Formats the data to mulitidimensional array. double[][] formattedData = formatData(similarFuncData); return(formattedData); }); double[][] initCentroids = new double[2][]; initCentroids[0] = new double[] { 5.0, 10.0 }; initCentroids[1] = new double[] { 15.0, 20.0 }; ClusteringSettings settings = new ClusteringSettings(0, numClusters: 2, numDims: 2, KmeansAlgorithm: 2, initialCentroids: initCentroids, tolerance: 0) { KmeansMaxIterations = 1000 }; api.UseKMeansFunctionRecognitionModule(settings); KMeansFunctionRecognitonScore res; while (batch-- > 0) { res = api.RunBatch() as KMeansFunctionRecognitonScore; } #endregion #region Prediction int[] noises = new int[] { 5, 7, 9, 10, 3, 15, 20, 25, 30, 35, 40, 22, 15, 17, 12, 48, 61 }; int numOfAnomalliesDetected = 0; foreach (var noiseForPrediction in noises) { var noisedFunc = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), noiseForPrediction - 2, noiseForPrediction); m_CustFncIndx = 0; double[][] data = formatData(noisedFunc); var predictionResult = api.Algorithm.Predict(data, null) as KMeansFunctionRecognitionResult; //// TRUE positives if (noiseForPrediction <= 10) { Assert.True(predictionResult.Loss == 1.0); Debug.WriteLine($"Recognized: noise: {noiseForPrediction} - {predictionResult.Result} - {predictionResult.Loss}"); } // TRUE negatives else if (noiseForPrediction >= 10) { if (predictionResult.Result == false) { numOfAnomalliesDetected++; // Result can be statistically true positive or true negative. This is because similar functions are genarated in +/- range to specified noise. // If model is trained to 10% and sinilar function is created to 25%, it means that values of similar function // fit range from 0-25% of referenced value. If it is below 10% (used for training in this test) then resut will be true positive. Debug.WriteLine($"Anomally detected: Noise: {noiseForPrediction} - {predictionResult.Result} - {predictionResult.Loss}"); } } } // This is a statistical value. Test might theoretically fail. Assert.True(numOfAnomalliesDetected > 2, $"Num of anomallies detected = {numOfAnomalliesDetected}. Expected at least 2."); #endregion }
public void Test_FunctionRecognitionModule(int points, int MinNoiseForPrediction, int MaxNoiseForPrediction) { #region Training var batch = 100; /// If 2 dimensions are used, then data is formatted as: /// ret[dim1] = {x1,x2,..xN}, /// ret[dim2] = {y1,y2,..,yN}, /// Every dimension is returned as a row. Poinst of dimension are cells. var funcData = FunctionGenerator.CreateFunction(points, 2, 2 * Math.PI / 100); LearningApi api = new LearningApi(); api.UseActionModule <object, double[][]>((notUsed, ctx) => { var similarFuncData = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), 7, 10); // Formats the data to mlitidimensional array. double[][] formattedData = formatData(similarFuncData); return(formattedData); }); double[][] initCentroids = new double[4][]; initCentroids[0] = new double[] { 1.53, 0.63 }; initCentroids[1] = new double[] { 4.68, -0.63 }; initCentroids[2] = new double[] { 7.85, 0.62 }; initCentroids[3] = new double[] { 10.99, -0.64 }; ClusteringSettings settings = new ClusteringSettings(0, numClusters: 4, numDims: 2, KmeansAlgorithm: 2, initialCentroids: initCentroids, tolerance: 0, funcRecogMethod: 2) { KmeansMaxIterations = 1000 }; api.UseKMeansFunctionRecognitionModule(settings); KMeansFunctionRecognitonScore res; while (batch-- > 0) { res = api.RunBatch() as KMeansFunctionRecognitonScore; } #endregion #region Prediction var noisedFunc = FunctionGenerator.CreateSimilarFromReferenceFunc(funcData.ToArray(), MinNoiseForPrediction, MaxNoiseForPrediction); double[][] data = formatData(noisedFunc); var predictionResult = api.Algorithm.Predict(data, null) as KMeansFunctionRecognitionResult; // TRUE positives if (MaxNoiseForPrediction <= 10) { Assert.True(predictionResult.Loss == 1.0); } // TRUE negatives else if (MaxNoiseForPrediction >= 25) { Assert.False(predictionResult.Loss == 1.0); } #endregion }