Exemplo n.º 1
0
        /// <summary>
        /// UseKMeans is an extansion that call KMeans through LearningAPI
        /// </summary>
        /// <param name="api">the LearningAPI object</param>
        /// <param name="settings">the desired clustering settings</param>
        /// <returns></returns>
        public static LearningApi UseKMeans(this LearningApi api, ClusteringSettings settings, double[][] centroids = null, double[] maxDistance = null)
        {
            var alg = new KMeans(settings, centroids, maxDistance);

            api.AddModule(alg, "Rbm");
            return(api);
        }
Exemplo n.º 2
0
        /// <summary>
        /// UseKMeans is an extension that call KMeans through LearningAPI
        /// </summary>
        /// <param name="api">the LearningAPI object</param>
        /// <param name="settings">the desired clustering settings</param>
        /// <returns></returns>
        public static LearningApi UseKMeans(this LearningApi api, ClusteringSettings settings, double[] maxDistance = null)
        {
            var alg = new KMeansAlgorithm(settings.Clone(), maxDistance);

            api.AddModule(alg, "Rbm");
            return(api);
        }
Exemplo n.º 3
0
        public void TestWithNormalize_MinMax()
        {
            // Creates learning api object
            LearningApi api = new LearningApi(loadMetaData1());

            //Real dataset must be defined as object type, because data can be numeric, binary and classification
            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                return(getRealDataSample(@"C:\Data\First.csv"));
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            //
            api.UseMinMaxNormalizer();

            ////use denormalizer on normalized data
            //api.UseMinMaxDeNormalizer();

            //
            var result = api.Run() as double[][];

            Helpers.WriteToCSVFile(result);
        }
Exemplo n.º 4
0
        public static LearningApi Useimageedge(this LearningApi a)
        {
            Lap alg = new Lap();

            a.AddModule(alg, "Useimageedge");
            return(a);
        }
        public void RunPipelineTest()
        {
            // Creates learning api object
            LearningApi api = new LearningApi(TestHelpers.GetDescriptor());

            // Initialize data provider
            api.UseCsvDataProvider(m_iris_data_path, ',', 1);

            // Use mapper for data, which will extract (map) required columns
            api.UseDefaultDataMapper();

            // Use MinMax data normalizer
            //api.UseMinMaxNormalizer(m_stats.Select(x => x.Min).ToArray(), m_stats.Select(x => x.Max).ToArray());

            // We could also use some other normalizer like Gaus data normalizer
            //api.UseGaussNormalizer(m_stats.Select(x => x.Mean).ToArray(), m_stats.Select(x => x.StDev).ToArray());

            // Prepares the ML Algoritm and setup parameters
            api.UseBackPropagation(1, 0.2, 1.0, null);

            //start process of learning
            api.Run();

            //  api.Train();
            //   api.TrainSample();

            IScore status = api.GetScore();

            //api.Train(vector)
            return;
        }
    {    /// <summary>
         /// Creating Object of SobelConvolutionFilter in this method and adding it to api.
         /// </summary>
         /// <param name="api">this is a api used to add module to learningApi. It is used as reference of LearningApi</param>
         /// <returns>It return api of LearningaApi</returns>
        public static LearningApi UseSobelConvolutionFilter(this LearningApi api)
        {
            SobelConvolutionFilter sobelconvo = new SobelConvolutionFilter();

            api.AddModule(sobelconvo, "UseSobelConvolutionFilter");
            return(api);
        }
Exemplo n.º 7
0
        public void SobelFilterTestForGrayscaleConversionImage()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <double[, , ], double[, , ]>((input, ctx) =>
            {
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string path          = Path.Combine(baseDirectory, "TestInputimages\\TIM3.jpg"); //path to bin directory of project
                Bitmap bitmap        = new Bitmap(path);
                double[,,] data      = helper.ConvertBitmapToDouble(bitmap);                     // convert bitmap to double
                return(data);
            });
            api.AddModule(new SobelConvolutionFilter());
            double[,,] result = api.Run() as double[, , ];

            Bitmap bitresult = helper.ConvertDoubleToBitmap(result);// convert double to bitmap

            string baseDirectory2 = AppDomain.CurrentDomain.BaseDirectory;
            string outpath        = baseDirectory2 + "\\TestOutputImages\\";

            if (!Directory.Exists(outpath))
            {
                Directory.CreateDirectory(outpath);
            }

            bitresult.Save(outpath + "Output3.jpg");
        }
        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
        }
Exemplo n.º 9
0
        /// <summary>
        /// Extension methods of LearningAPI for using MinMax denormalizer
        /// </summary>
        /// <param name="api"></param>
        /// <returns></returns>
        public static LearningApi UseMinMaxDeNormalizer(this LearningApi api)
        {
            var minMaxDeNorm = new MinMaxDeNormalizer();

            api.Modules.Add("Denormilizer", minMaxDeNorm);
            return(api);
        }
        public void Test_Save()
        {
            double[][] clusterCenters = new double[3][];
            clusterCenters[0] = new double[] { 5.0, 5.0 };
            clusterCenters[1] = new double[] { 15.0, 15.0 };
            clusterCenters[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: 2);

            // Creates learning api object
            LearningApi api = new LearningApi(loadDescriptor());

            // creates data
            var             rawData = Helpers.CreateSampleData(clusterCenters, 2, 10000, 0.5);
            KMeansAlgorithm kMeans  = new KMeansAlgorithm(settings);
            // train
            var response = kMeans.Run(rawData, api.Context);

            string fileName = "Test01.json";

            kMeans.Save(rootFolder + fileName);
        }
Exemplo n.º 11
0
        public bool InitNeuralBackPropagationTest()
        {
            //  InitIrisMapperInJsonFormat_helper();

            // 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();

            // Use MinMax data normalizer
            //api.UseMinMaxNormalizer();

            // We could also use some other normalizer like Gaus data normalizer
            //api.UseGaussNormalizer(m_stats.Select(x => x.Mean).ToArray(), m_stats.Select(x => x.StDev).ToArray());

            // Prepares the ML Algoritm and setup parameters
            api.UseBackPropagation(1, 0.2, 1.0, null);

            api.Run();

            IScore status = api.GetScore();

            //api.Train(vector)
            return(true);
        }
Exemplo n.º 12
0
        public void MinMaxNormalization_test1()
        {
            // Creates learning api object
            LearningApi api = new LearningApi(loadMetaData1());

            //Real dataset must be defined as object type, because data can be numeric, binary and classification
            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                return(GetRealDataSample());
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            //
            api.UseMinMaxNormalizer();

            //
            var result = api.Run() as double [][];


            //Test result for normalization
            var expected = GetNormalizedDataSample();

            for (int i = 0; i < expected.Length; i++)
            {
                for (int j = 0; j < expected[0].Length; j++)
                {
                    Assert.Equal(Math.Round(result[i][j], 5), expected[i][j]);
                }
            }

            //
            return;
        }
Exemplo n.º 13
0
        public void RBMDataSample1Test()
        {
            var dataPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\rbm_sample1.csv");

            LearningApi api = new LearningApi(this.getDescriptorForRbm_sample1());

            // Initialize data provider
            api.UseCsvDataProvider(dataPath, ',', false, 1);
            api.UseDefaultDataMapper();
            api.UseRbm(0.2, 1000, 6, 3);

            RbmResult score = api.Run() as RbmResult;

            double[][] testData = new double[4][];

            testData[0] = new double[] { 1, 1, 0, 0, 0, 0 };
            testData[1] = new double[] { 0, 0, 0, 0, 1, 1 };
            testData[2] = new double[] { 0, 1, 0, 0, 0, 0 };
            testData[3] = new double[] { 0, 0, 0, 0, 1, 1 };

            var result = api.Algorithm.Predict(testData, api.Context);

            // NOT FINISHED.
            //Assert.True(result[0] == 1);
            //Assert.True(result[1] == 0);
            //Assert.True(result[2] == 0);
            //Assert.True(result[3] == 0);
            //Assert.True(result[4] == 1);
            //Assert.True(result[5] == 0);
        }
Exemplo n.º 14
0
        public void OR_Test()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                ctx.DataDescriptor = get2DDescriptor();
                double[][] data    = new double[4][];


                data[0] = new double[] { 0, 0, 0, 0.0 };
                data[1] = new double[] { 0, 1, 1, 0.0 };
                data[2] = new double[] { 1, 0, 1, 0.0 };
                data[3] = new double[] { 1, 1, 1, 0.0 };

                return(data);
            });

            api.UseDeltaLearning(0.2, 1000);

            IScore score = api.Run() as IScore;

            double[][] testData = new double[3][];
            testData[0] = new double[] { 0, 0, 0.0 };
            testData[1] = new double[] { 1, 1, 0.0 };
            testData[2] = new double[] { 0, 1, 0.0 };

            var result = api.Algorithm.Predict(testData, api.Context) as DeltaLearningResult;

            Assert.True(result.PredictedResults[0] == 0);
            Assert.True(result.PredictedResults[1] == 1);
            Assert.True(result.PredictedResults[2] == 1);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Installs the KMeanFunctionRecognitionModule in the LearningApi pipeline.
        /// </summary>
        /// <param name="api"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static LearningApi UseKMeansFunctionRecognitionModule(this LearningApi api, ClusteringSettings settings)
        {
            var alg = new KMeansFunctionRecognitionAlgorithm(settings);

            api.AddModule(alg, "KMeanFunctionRecognitionModule");
            return(api);
        }
Exemplo n.º 16
0
        public void RBMBinaryDataCreation()
        {
            //size of image
            int size = 40;

            var context = getImgRecognitionDescriptor(size * size);

            LearningApi api = new LearningApi(context);

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                double[][] trainData = new double[22][];
                for (int j = 0; j < 1; j++)
                {
                    //Path of training images.
                    //return getImageData(size, $"{Directory.GetCurrentDirectory()}\\RestrictedBolzmannMachine2\\TrainingImages");
                    trainData = getImageData(size, imagePath + j);
                }
                return(trainData);
            });

            IScore score = api.Run() as IScore;

            //DigitDatasetCSVFile.Close();
            SmileyCSVFile.Close();
        }
Exemplo n.º 17
0
        /// <summary>
        /// Extension methods of LearningAPI for using Gauss denormalizer
        /// </summary>
        /// <param name="api"></param>
        /// <returns></returns>
        public static LearningApi UseGaussDeNormalizer(this LearningApi api)
        {
            var minMaxDeNorm = new GaussDeNormalizer();

            api.AddModule(minMaxDeNorm, "GaussDenormilizer");
            return(api);
        }
Exemplo n.º 18
0
        public void Feature_and_LabelIndex_Mapping_Test2()
        {
            var desc = loadMetaData_with_CategoricFeature();
            // Creates learning api object
            LearningApi api = new LearningApi(desc);

            //Real dataset must be defined as object type, because data can be numeric, binary and classification
            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                return(loadRealDataSample());
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            //
            var result = api.Run() as double[][];

            var featureNumber = result[0].Count() - 1;//- labelcolumn

            //string column is not counted because it is ignored column
            //there is one category column in features so the number of features is increased with (calssCount-1)
            // featureCount = columnCount- strinCoulumnCount-LabelColumn + calssCount-1
            var featureCount = 4 - 1 - 1 + (3 - 1);

            Assert.Equal(featureNumber, featureCount);
        }
Exemplo n.º 19
0
        /// <summary>
        /// perform the loading data from SCV file
        /// </summary>
        /// <param name="api">instance of the LearningAPI</param>
        /// <param name="fileName">csv file path</param>
        /// <param name="delimiter">csv delimiter</param>
        /// <param name="isHeader"> is header included in the data after skiped rows. </param>
        /// <param name="skipRows">firs several rows which should be skiped in parsing.</param>
        /// <returns></returns>
        public static LearningApi UseCsvDataProvider(this LearningApi api, string fileName, char delimiter, bool isHeader, int skipRows = 0)
        {
            var dp = new CsvDataProvider(fileName, delimiter, isHeader, skipRows);

            api.AddModule(dp, "CsvDataProvider");

            return(api);
        }
Exemplo n.º 20
0
        /// <summary>
        /// perform the loading data from SCV file
        /// </summary>
        /// <param name="api">instance of the LearningAPI</param>
        /// <param name="strContent">csv string</param>
        /// <param name="delimiter">csv delimiter</param>
        /// <param name="isHeader"> is header included in the data after skiped rows. </param>
        /// <param name="skipRows">firs several rows which should be skiped in parsing.</param>
        /// <returns></returns>
        public static LearningApi UseCsvDataParser(this LearningApi api, string strContent, char delimiter, bool isHeader, int skipRows = 0)
        {
            var dp = new CsvDataParser(strContent, delimiter, isHeader, skipRows);

            api.AddModule(dp, "CsvDataParser");

            return(api);
        }
        /// <summary>
        /// Extension methods of LearningAPI for using MinMax normalizer
        /// </summary>
        /// <param name="api">LearningAPI</param>
        /// <returns></returns>
        public static LearningApi UseMinMaxNormalizer(this LearningApi api)
        {
            var minMaxNorm = new MinMaxNormalizer();

            api.AddModule(minMaxNorm, "Normalizer");

            return(api);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Extension methods of LearningAPI for Gauss MinMax normalizer
        /// </summary>
        /// <param name="api">LearningAPI</param>
        /// <returns></returns>
        public static LearningApi UseGaussNormalizer(this LearningApi api)
        {
            var minMaxNorm = new GaussNormalizer();

            api.Modules.Add("GaussNormilizer", minMaxNorm);

            return(api);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Loads and initializes data mapper
        /// </summary>
        /// <param name="api">LearningAPI </param>
        /// <returns></returns>
        public static LearningApi UseDefaultDataMapper(this LearningApi api)
        {
            var dm = new DataMapper();

            api.Modules.Add("DataMapper", dm);

            return(api);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Loads and initializes data mapper
        /// </summary>
        /// <param name="api">LearningAPI </param>
        /// <returns></returns>
        public static LearningApi UseDefaultDataMapper(this LearningApi api)
        {
            var dm = new DataMapper();

            api.AddModule(dm, "DataMapper");

            return(api);
        }
Exemplo n.º 25
0
        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();
        }
Exemplo n.º 26
0
        public void calculateCorrelation_test1()
        {
            //
            LearningApi api = new LearningApi(null);

            // Initialize data provider
            api.UseCsvDataProvider(@"CorrelationMatrix/corellation_data.csv", ',', true, 0);


            //Custom action of dataset
            api.UseActionModule <object[][], double[][]>((input, ctx) =>
            {
                return(toColumnVector(input));
            });


            // api.UseMinMaxNormalizer();


            var data = api.Run() as double[][];

            var prov = api.GetModule <CsvDataProvider>("CsvDataProvider");

            var strData = new List <string>();
            var hed     = prov.Header.ToList();

            hed.Insert(0, "");
            strData.Add(string.Join(",", hed.ToArray()));
            for (int i = 0; i < data.Length; i++)
            {
                var lst = new List <string>();
                lst.Add(prov.Header[i]);
                for (int k = 0; k < i; k++)
                {
                    lst.Add(" ");
                }

                for (int j = i; j < data.Length; j++)
                {
                    var corValue = data[i].CorrCoeffOf(data[j]);
                    if (double.IsNaN(corValue))
                    {
                        continue;
                    }
                    lst.Add(corValue.ToString("n5", CultureInfo.InvariantCulture));
                }


                strData.Add(string.Join(",", lst));
            }

            Assert.True("Col1,1.00000,0.16892,0.99111,0.75077,-0.82354,-0.85164" == strData[1]);

            System.IO.File.WriteAllLines(@"CorrelationMatrix/strCorrlation.txt", strData);
            //
            return;
        }
Exemplo n.º 27
0
        /// <summary>
        /// The implementation of Logistics Regression machine learning algorithm
        /// </summary>
        /// <param name="api"></param>
        /// <param name="learningRate"></param>
        /// <returns></returns>
        public static LearningApi UseSurvivalAnalysis(this LearningApi api, int iterations)

        {
            var alg = new SurvivalAnalysis();

            alg.Iterations = iterations;
            api.AddModule(alg, "Logistic Regression");
            return(api);
        }
        /// <summary>
        /// The implementation of Logistics Regression machine learning algorithm
        /// </summary>
        /// <param name="api"></param>
        /// <param name="learningRate"></param>
        /// <returns></returns>
        public static LearningApi UseLogisticRegression(this LearningApi api, double learningRate, int iterations)

        {
            var alg = new LogisticRegression(learningRate);

            alg.Iterations = iterations;
            api.Modules.Add("Logistic Regression", alg);
            return(api);
        }
Exemplo n.º 29
0
        public static LearningApi UseBackPropagation(this LearningApi api,
                                                     int hiddenLayerCount, double momentum, double learningRate,
                                                     IActivationFunction activationFnc)

        {
            var alg = new BackPropagationNetwork(hiddenLayerCount, momentum, learningRate, activationFnc);

            api.AddModule(alg, "Algorithm");
            return(api);
        }
        /// <summary>
        /// This method is invoked from the unit test project to train the neural network on the training data
        /// </summary>
        /// <param name="api">Learning api</param>
        /// <param name="learningRate">learning rate of the network</param>
        /// <param name="iterations">Number of epochs</param>
        /// <param name="hiddenlayerneurons">Defines list of layers with number of hidden layer neurons at every layer.</param>
        /// <param name="activationFnc">activation function</param>
        /// <returns>LearningApi</returns>
        public static LearningApi UseMLPerceptron(this LearningApi api,
                                                  double learningRate, int iterations, int batchSize, int testCaseNumber, int[] hiddenlayerneurons = null,
                                                  IActivationFunction activationFnc = null)

        {
            var alg = new MLPerceptronAlgorithm(learningRate, iterations, batchSize, testCaseNumber, hiddenlayerneurons);

            api.AddModule(alg, "MLPerceptronAlgorithm");
            return(api);
        }