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); }
public void SaveModel() { m_LearningApi.Save("model"); }
public void UnitTestSdr(int iterations, double learningrate, int batchSize, int[] hiddenLayerNeurons, int iterationnumber) { LearningApi api = new LearningApi(); api.UseActionModule <object, double[][]>((notUsed, ctx) => { List <double[]> rows = new List <double[]>(); ctx.DataDescriptor = new DataDescriptor(); var trainingFiles = Directory.GetFiles($"{Directory.GetCurrentDirectory()}\\MLPerceptron\\TestFiles\\Sdr"); int rowCnt = 0; foreach (var file in trainingFiles) { using (var reader = new StreamReader(file)) { string line; while ((line = reader.ReadLine()) != null) { var tokens = line.Split(","); List <string> newTokens = new List <string>(); foreach (var token in tokens) { if (token != " ") { newTokens.Add(token); } } tokens = newTokens.ToArray(); if (rowCnt == 0) { ctx.DataDescriptor.Features = new LearningFoundation.DataMappers.Column[tokens.Length - 1]; for (int i = 1; i < tokens.Length; i++) { ctx.DataDescriptor.Features[i - 1] = new LearningFoundation.DataMappers.Column { Id = i, Index = i, Type = LearningFoundation.DataMappers.ColumnType.BINARY }; } ctx.DataDescriptor.LabelIndex = -1; } // We have 65 features and digit number in file. to encode digits 0-9. // Digits can be represented as 9 bits. double[] row = new double[tokens.Length - 1 + 10]; for (int i = 0; i < tokens.Length; i++) { row[i] = double.Parse(tokens[i], CultureInfo.InvariantCulture); } // // This code encodes 9 digit classes as last 9 bits of training vector. for (int k = 0; k < 10; k++) { if (double.Parse(tokens[0], CultureInfo.InvariantCulture) == k) { row[tokens.Length - 1 + k] = 1; } else { row[tokens.Length - 1 + k] = 0; } } rows.Add(row); } } rowCnt++; } return(rows.ToArray()); }); //int[] hiddenLayerNeurons = { 6 }; // Invoke the MLPerecptronAlgorithm with a specific learning rate, number of iterations api.UseMLPerceptron(learningrate, iterations, batchSize, iterationnumber, hiddenLayerNeurons); MLPerceptronAlgorithmScore score = api.Run() as MLPerceptronAlgorithmScore; api.Save("SdrMnistModel"); }