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 Test_LoadSave() { string moduleName = "test-action"; double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error ClusteringSettings settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1); // Creates learning api object LearningApi api = new LearningApi(loadDescriptor()); // // Defines action method, which will generate training data. api.UseActionModule <object, double[][]>((data, ctx) => { var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); return(rawData); }, moduleName); api.UseKMeans(settings); var resp = api.Run() as KMeansScore; Assert.True(resp.Model.Clusters != null); Assert.True(resp.Model.Clusters.Length == clusterCentars.Length); var result = api.Algorithm.Predict(clusterCentars, api.Context) as KMeansResult; Assert.True(result.PredictedClusters[0] == 0); Assert.True(result.PredictedClusters[1] == 1); Assert.True(result.PredictedClusters[2] == 2); // This is where trained model is saved. api.Save(nameof(TestLoadSave)); // Loads the saved model. var loadedApi = LearningApi.Load(nameof(TestLoadSave)); // // Because we have used action method in the LearningApi, we will have to setup it again. // This is not required because API design limitation. It is restriction of .NET framework. It cannot persist code. loadedApi.ReplaceActionModule <object, double[][]>(moduleName, (data, ctx) => { var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); return(rawData); }); loadedApi.Run(); }
public void Test_LoadSave() { string moduleName = "test-action"; double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error ClusteringSettings settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1); // Creates learning api object LearningApi api = new LearningApi(loadDescriptor()); api.UseActionModule <object, double[][]>((data, ctx) => { var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); return(rawData); }, moduleName); api.UseKMeans(settings); var resp = api.Run() as KMeansScore; Assert.True(resp.Model.Clusters != null); Assert.True(resp.Model.Clusters.Length == clusterCentars.Length); var result = api.Algorithm.Predict(clusterCentars, api.Context) as KMeansResult; Assert.True(result.PredictedClusters[0] == 0); Assert.True(result.PredictedClusters[1] == 1); Assert.True(result.PredictedClusters[2] == 2); api.Save(nameof(LoadSaveTests)); var loadedApi = LearningApi.Load(nameof(LoadSaveTests)); loadedApi.ReplaceActionModule <object, double[][]>(moduleName, (data, ctx) => { var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); return(rawData); }); loadedApi.Run(); }
public void Save_Test() { // Creates learning api object LearningApi api = new LearningApi(TestHelpers.GetDescriptor()); // Initialize data provider api.UseCsvDataProvider(m_IrisDataPath, ',', false, 1); // Use mapper for data, which will extract (map) required columns api.UseDefaultDataMapper(); // Prepares the ML Algorithm and setup parameters api.UseBackPropagation(1, 0.2, 1.0, null); api.Save("model1"); var loadedApi = LearningApi.Load("model1"); Assert.True(((BackPropagationNetwork)loadedApi.Algorithm).learningRate == ((BackPropagationNetwork)api.Algorithm).learningRate); }