Exemplo n.º 1
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.º 2
0
        public void SobelFilterTestForPngImageFileType()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <double[, , ], double[, , ]>((input, ctx) =>
            {
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string path          = Path.Combine(baseDirectory, "TestInputimages\\TIM4.png"); //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 + "Output4.png");
        }
Exemplo n.º 3
0
        public void NumericTransformation_test2()
        {
            // Creates learning api object
            LearningApi api = new LearningApi(loadMetaData_with_CategoricFeature());

            //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[][];


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

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

            //
            return;
        }
Exemplo n.º 4
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.º 5
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.º 6
0
        public void SimpleSequenceWithGapsTest()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                const int maxSamples = 1000000;
                ctx.DataDescriptor   = getDescriptor();
                double[][] data      = new double[maxSamples / 3][];

                //
                // We generate following input vectors:
                // IN Val - Expected OUT Val
                // Every 3th number is given

                for (int i = 0; i < maxSamples / 3; i++)
                {
                    data[i]    = new double[2];
                    data[i][0] = i * 3;
                    if ((i * 3) > (maxSamples / 2))
                    {
                        data[i][1] = 1;
                    }
                    else
                    {
                        data[i][1] = 0;
                    }
                }

                return(data);
            });

            api.UsePerceptron(0.05, 1000);

            IScore score = api.Run() as IScore;

            double[][] testData = new double[6][];
            testData[0] = new double[] { 2.0, 0.0 };
            testData[1] = new double[] { 1, 0.0 };
            testData[2] = new double[] { 3, 0.0 };
            testData[3] = new double[] { 3002, 0.0 };
            testData[4] = new double[] { 6002.0, 0.0 };
            testData[5] = new double[] { 9005, 0.0 };

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

            // TODO... THUS TEST iS NOT COMPLETED

            //Assert.True(result.PredictedValues[0] == 0);
            //Assert.True(result.PredictedValues[1] == 0);
            //Assert.True(result.PredictedValues[2] == 0);
            //Assert.True(result.PredictedValues[3] == 1);
            //Assert.True(result.PredictedValues[4] == 1);
            //Assert.True(result.PredictedValues[5] == 1);
        }
Exemplo n.º 7
0
        public void LogisticRegression_Test_iterations_20_learningrate_015()
        {
            var         desc = loadMetaData();
            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());
            });

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

            api.UseMinMaxNormalizer();

            //run logistic regression for 10 iteration with learningRate=0.15
            api.UseLogisticRegression(0.15, 20);

            api.Run();

            IScore score = api.GetScore();

            //Errors during each iteration
            Assert.Equal(Math.Round(score.Errors[0], 5), 0.24236);
            Assert.Equal(Math.Round(score.Errors[1], 5), 0.23707);
            Assert.Equal(Math.Round(score.Errors[2], 5), 0.23358);
            Assert.Equal(Math.Round(score.Errors[3], 5), 0.23001);
            Assert.Equal(Math.Round(score.Errors[4], 5), 0.22806);
            Assert.Equal(Math.Round(score.Errors[5], 5), 0.22506);
            Assert.Equal(Math.Round(score.Errors[6], 5), 0.22409);
            Assert.Equal(Math.Round(score.Errors[7], 5), 0.22134);
            Assert.Equal(Math.Round(score.Errors[8], 5), 0.22105);
            Assert.Equal(Math.Round(score.Errors[9], 5), 0.21840);
            Assert.Equal(Math.Round(score.Errors[10], 5), 0.21857);
            Assert.Equal(Math.Round(score.Errors[11], 5), 0.21595);
            Assert.Equal(Math.Round(score.Errors[12], 5), 0.21640);
            Assert.Equal(Math.Round(score.Errors[13], 5), 0.21381);
            Assert.Equal(Math.Round(score.Errors[14], 5), 0.21439);
            Assert.Equal(Math.Round(score.Errors[15], 5), 0.21189);
            Assert.Equal(Math.Round(score.Errors[16], 5), 0.21251);
            Assert.Equal(Math.Round(score.Errors[17], 5), 0.21015);
            Assert.Equal(Math.Round(score.Errors[18], 5), 0.21076);
            Assert.Equal(Math.Round(score.Errors[19], 5), 0.20860);


            //LG Model Best Found model in 20 iteration
            Assert.Equal(Math.Round(score.Weights[0], 5), 0.28363);
            Assert.Equal(Math.Round(score.Weights[1], 5), 0.37424);
            Assert.Equal(Math.Round(score.Weights[2], 5), 1.41890);
            Assert.Equal(Math.Round(score.Weights[3], 5), 1.01207);
            Assert.Equal(Math.Round(score.Weights[4], 5), -0.33841);
            Assert.Equal(Math.Round(score.Weights[5], 5), -0.33841);
            Assert.Equal(Math.Round(score.Weights[6], 5), -1.62489);
        }
Exemplo n.º 8
0
        public void LogisticRegression_Test_Real_Example()
        {
            string m_binary_data_path = @"SampleData\binary\admit_binary.csv";

            var binary_path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), m_binary_data_path);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseCsvDataProvider(binary_path, ',', false, 1);

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

            api.UseMinMaxNormalizer();

            //run logistic regression for 10 iteration with learningRate=0.15
            api.UseLogisticRegression(0.00012, 200);


            var score = api.Run();

            ///**************PREDICTION AFTER MODEL IS CREATED*********////
            /////define data for testing (prediction)
            LearningApi apiPrediction = new LearningApi(loadMetaData1());

            //Real dataset must be defined as object type, because data can be numeric, binary and classification
            apiPrediction.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var data = new object[5][]
                {
                    new object[] { 660, 3.88, 2, 1 },
                    new object[] { 580, 3.36, 2, 0 },
                    new object[] { 640, 3.17, 2, 0 },
                    new object[] { 640, 3.51, 2, 0 },
                    new object[] { 800, 3.05, 2, 1 },
                };
                return(data);
            });

            // Use mapper for data, which will extract (map) required columns
            apiPrediction.UseDefaultDataMapper();
            apiPrediction.UseMinMaxNormalizer();
            var testData = apiPrediction.Run();

            //use previous trained model
            var result = api.Algorithm.Predict(testData as double[][], api.Context) as LogisticRegressionResult;

            //
            Assert.Equal(Math.Round(result.PredictedValues[0], 0), 0);
            Assert.Equal(Math.Round(result.PredictedValues[1], 0), 0);
            Assert.Equal(Math.Round(result.PredictedValues[2], 0), 0);
            Assert.Equal(Math.Round(result.PredictedValues[3], 0), 0);
            Assert.Equal(Math.Round(result.PredictedValues[3], 0), 0);
        }
Exemplo n.º 9
0
        public void ContinuousTrainData()
        {
            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 0.2, -4.0 };
            initialCentroids[1] = new double[] { 0.2, -6.0 };
            initialCentroids[2] = new double[] { 0.4, -4.0 };
            initialCentroids[3] = new double[] { 0.4, -6.0 };

            string[] attributes = new string[] { "x", "y" };

            int numAttributes = attributes.Length;  // 2 in this demo (x,y)
            int numClusters   = 4;
            int maxCount      = 300;

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var rawDatalist = getRealDataSample(@"C:\Data\Function1.csv").ToList();

                double[][] oldSamples;

                var nn = kmeanApi.GetPreviousSamples(sett, out oldSamples);

                if (oldSamples != null)
                {
                    foreach (var old in oldSamples)
                    {
                        var row = old.Cast <object>().ToArray();
                        rawDatalist.Add(row);
                    }
                }
                return(rawDatalist.ToArray());
            });

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

            api.UseGaussNormalizer();

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

            Helpers.WriteToCSVFile(rawData);

            ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
        }
Exemplo n.º 10
0
        private object[][] getData(int cnt)
        {
            string filePath   = $"{Directory.GetCurrentDirectory()}\\DataSet\\Book2.csv";
            var    isris_path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), filePath);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseCsvDataProvider(isris_path, ',', 0);

            return(api.Run() as object[][]);
        }
Exemplo n.º 11
0
        public void movieRecommendationTestCRbm(int iterations, double learningRate, int visNodes, int hidNodes)
        {
            Debug.WriteLine($"{iterations}-{visNodes}-{hidNodes}");

            LearningApi api = new LearningApi(getDescriptorForRbm(3898));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\movieDatasetTrain.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85 };
            api.UseCRbm(featureVector, learningRate, iterations, visNodes, hidNodes);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmScore score = api.Run() as RbmScore;

            watch.Stop();

            var hiddenNodes  = score.HiddenValues;
            var hiddenWeight = score.HiddenBisases;


            double[] learnedFeatures = new double[hidNodes];
            double[] hiddenWeights   = new double[hidNodes];
            for (int i = 0; i < hidNodes; i++)
            {
                learnedFeatures[i] = hiddenNodes[i];
                hiddenWeights[i]   = hiddenWeight[i];
            }

            StreamWriter tw = new StreamWriter($"PredictedDigit_I{iterations}_V{visNodes}_H{hidNodes}_learnedbias.txt");

            foreach (var item in score.HiddenBisases)
            {
                tw.WriteLine(item);
            }
            tw.Close();

            var testData = ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\movieDatasetTest.csv"));

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

            var predictedData = ((RbmResult)result).VisibleNodesPredictions;

            var predictedHiddenNodes = ((RbmResult)result).HiddenNodesPredictions;

            var acc = testData.GetHammingDistance(predictedData);

            WriteDeepResult(iterations, new int[] { visNodes, hidNodes }, learningRate, acc, watch.ElapsedMilliseconds * 1000, predictedHiddenNodes);

            WriteOutputMatrix(iterations, new int[] { visNodes, hidNodes }, learningRate, predictedData, testData);
        }
Exemplo n.º 12
0
        private object[][] getRealDataSample(string filePath)
        {
            //
            //iris data file
            var isris_path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), filePath);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseCsvDataProvider(isris_path, ',', 0);

            return(api.Run() as object[][]);
        }
Exemplo n.º 13
0
        public void DeltaRuleLearning_Test_iterations_1000_learningrate_02()
        {
            loadRealDataSample();              // System coefficients initialization.

            var         desc = loadMetaData(); // Description of the system.
            LearningApi api  = new LearningApi(desc);

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

            // run input = UseActionModule output
            //run Delta Rule for 1000 iterations with learningRate=0.2
            api.UseDeltaRuleLearning(0.2, 1000);
            var result = api.Run() as double[];

            Debug.WriteLine("************ Output Predictions***********");
            for (int i = 0; i < result.Length; i++)
            {
                if (result[i] != 0)
                {
                    Debug.WriteLine(result[i]);
                }
            }
            using (var fs = File.OpenRead(@"H_Test.csv"))
                using (var reader = new StreamReader(fs))
                {
                    while (!reader.EndOfStream)
                    {
                        var line   = reader.ReadLine();
                        var values = line.Split(',');
                        ti = new double[values.Length];
                        to = new double[values.Length];
                        for (int i = 0; i < values.Length; i++)
                        {
                            var val = values[i].Split(' ');
                            double.TryParse(val[0], out x);
                            double.TryParse(val[1], out y);

                            ti[i] = x;
                            to[i] = y;
                        }
                    }
                }
            for (int i = 0; i < to.Length; i++)
            {
                //Testing of Test data with Predicted System model
                Assert.Equal(Math.Round(result[i], 4), Math.Round(to[i], 4));
            }
        }
Exemplo n.º 14
0
        public void SimpleRBMDeepTest()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                const int maxSamples = 12;
                ctx.DataDescriptor   = getDescriptorForRbm_sample1();
                double[][] data      = new double[maxSamples][];

                data[0] = new double[] { 1, 1, 0, 0, 0, 0 };  // A
                data[1] = new double[] { 0, 0, 1, 1, 0, 0 };  // B
                data[2] = new double[] { 0, 0, 0, 0, 1, 1 };  // C

                data[3] = new double[] { 1, 1, 0, 0, 0, 1 };  // noisy A
                data[4] = new double[] { 0, 0, 1, 1, 0, 0 };  // BRt
                data[5] = new double[] { 0, 0, 0, 0, 1, 1 };  // C

                data[6] = new double[] { 1, 0, 0, 0, 0, 0 };  // weak A
                data[7] = new double[] { 0, 0, 1, 0, 0, 0 };  // weak B
                data[8] = new double[] { 0, 0, 0, 0, 1, 0 };  // weak C

                data[9]  = new double[] { 1, 1, 0, 1, 0, 0 }; // noisy A
                data[10] = new double[] { 1, 0, 1, 1, 0, 0 }; // noisy B
                data[11] = new double[] { 0, 0, 1, 0, 1, 1 }; // noisy C
                return(data);
            });


            api.UseRbm(0.01, 1000, 6, 4);

            RbmScore score = api.Run() as RbmScore;

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

            Assert.True(score.Loss < 1.0);

            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, 0 };

            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.º 15
0
        public void FullDataSetRBMTest()
        {
            const int bits = 10;

            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                var maxSamples  = (int)Math.Pow(2, bits);
                double[][] data = new double[maxSamples][];

                for (int i = 0; i < maxSamples; i++)
                {
                    data[i] = new double[bits];

                    var val = 1;
                    for (int j = 0; j < bits; j++)
                    {
                        if ((val & i) >= 1)
                        {
                            data[i][j] = 1;
                        }
                        val = val << 1;
                    }
                }

                ctx.DataDescriptor = getDescriptorForRbm_sample1();

                return(data);
            });

            api.UseRbm(0.01, 1000, bits, 7);

            RbmScore score = api.Run() as RbmScore;

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

            testData[0] = new double[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
            testData[1] = new double[] { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 };
            testData[2] = new double[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
            testData[3] = new double[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 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.º 16
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());

            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();
        }
Exemplo n.º 17
0
        public void ActionModuleTest()
        {
            LearningApi api = new LearningApi(null);

            api.UseActionModule <double[], double[]>((input, ctx) =>
            {
                return(new double[] { 1.1, 2.2, 3.3, 4.4 });
            });

            var result = api.Run();

            Assert.Equal(1.1, ((double[])result)[0]);
            Assert.Equal(4.4, ((double[])result)[3]);
        }
Exemplo n.º 18
0
        public void SimpleSequenceTest()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                const int maxSamples = 10;
                ctx.DataDescriptor   = getDescriptor();
                double[][] 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;
                }

                return(data);
            });

            api.UseDeltaLearning(0.2, 1000);

            IScore score = api.Run() as IScore;

            double[][] testData = new double[4][];
            testData[0] = new double[] { 2.0, 0.0 };
            testData[1] = new double[] { 4.0, 0.0 };
            testData[2] = new double[] { 6.0, 0.0 };
            testData[3] = new double[] { 8.0, 0.0 };


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


            Assert.True(result.PredictedResults[0] == 0);
            Assert.True(result.PredictedResults[1] == 0);
            Assert.True(result.PredictedResults[2] == 1);
            Assert.True(result.PredictedResults[3] == 1);
        }
Exemplo n.º 19
0
        public void Training()
        {
            int cnt = 0;

            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 0.4, 25.0 };
            initialCentroids[1] = new double[] { 0.4, 15.0 };
            initialCentroids[2] = new double[] { 0.6, 15.0 };
            initialCentroids[3] = new double[] { 0.6, 25.0 };

            string[] attributes = new string[] { "x", "y" };

            int numAttributes = attributes.Length;
            int numClusters   = 4;
            int maxCount      = 300;

            ClusteringSettings clusterSettings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, Replace: true);

            AnomalyDetectionAPI      kmeanApi = new AnomalyDetectionAPI(clusterSettings); //AnomalyDetectionAPI(clusterSettings), Constractor should not be null when run Training()
            AnomalyDetectionResponse response;

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

            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var rawDataArray = getData(cnt);

                return(rawDataArray);
            });

            api.UseDefaultDataMapper();
            api.UseGaussNormalizer();


            //
            for (int i = 0; i < 15; i++)
            {
                cnt = i;

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

                response = kmeanApi.Training(rawData, initialCentroids);

                Helpers.WriteToCSVFile(kmeanApi.GetCentroid(), $"Data\\Centroid{i}.csv");

                //response = kmeanApi.Save($"Function{i}.json");
            }
        }
Exemplo n.º 20
0
        public void smileyTestCRbm(int iterations, double learningRate, int visNodes, int hidNodes)
        {
            LearningApi api = new LearningApi(getDescriptorForRbm(1600));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\Smiley.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0.1, 0.2 };
            api.UseCRbm(featureVector, learningRate, iterations, visNodes, hidNodes);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmScore score = api.Run() as RbmScore;

            watch.Stop();

            var hiddenNodes  = score.HiddenValues;
            var hiddenWeight = score.HiddenBisases;


            double[] learnedFeatures = new double[hidNodes];
            double[] hiddenWeights   = new double[hidNodes];
            for (int i = 0; i < hidNodes; i++)
            {
                learnedFeatures[i] = hiddenNodes[i];
                hiddenWeights[i]   = hiddenWeight[i];
            }

            var testData = ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\SmileyTest.csv"));

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

            var predictedData = ((RbmResult)result).VisibleNodesPredictions;

            var predictedHiddenNodes = ((RbmResult)result).HiddenNodesPredictions;

            var acc = testData.GetHammingDistance(predictedData);

            var ValTest  = calcDelta(predictedData, testData);
            var lossTest = ValTest / (visNodes);

            Debug.WriteLine($"lossTest: {lossTest}");

            WriteDeepResult(iterations, new int[] { visNodes, hidNodes }, acc, watch.ElapsedMilliseconds * 1000, predictedHiddenNodes);

            WriteOutputMatrix(iterations, new int[] { visNodes, hidNodes }, predictedData, testData);
        }
Exemplo n.º 21
0
        public void ContinuousTrainData2()
        {
            int cnt = 0;

            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 40.0, 10.0 };
            initialCentroids[1] = new double[] { 20.0, 10.0 };
            initialCentroids[2] = new double[] { 40.0, 20.0 };
            initialCentroids[3] = new double[] { 20.0, 20.0 };

            string[] attributes = new string[] { "x", "y" };

            int numAttributes = attributes.Length;  // 2 in this demo (x,y)
            int numClusters   = 4;
            int maxCount      = 300;

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters, initialCentroids);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var rawDatalist = getData(cnt);

                return(rawDatalist);
            });

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

            api.UseGaussNormalizer();

            for (int i = 0; i < 15; i++)
            {
                cnt = i;

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

                ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

                AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
            }
        }
Exemplo n.º 22
0
        public void SimpleSequence2DTest()
        {
            LearningApi api = new LearningApi();

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

                for (int i = 0; i < maxSamples / 2; i++)
                {
                    data[2 * i]    = new double[3];
                    data[2 * i][0] = i;
                    data[2 * i][1] = 5.0;
                    data[2 * i][2] = 1.0;

                    data[2 * i + 1]    = new double[3];
                    data[2 * i + 1][0] = i;
                    data[2 * i + 1][1] = -5.0;
                    data[2 * i + 1][2] = 0.0;
                }
                return(data);
            });

            api.UseDeltaLearning(0.2, 1000);

            IScore score = api.Run() as IScore;

            double[][] testData = new double[6][];
            testData[0] = new double[] { 2.0, 5.0, 0.0 };
            testData[1] = new double[] { 2, -5.0, 0.0 };
            testData[2] = new double[] { 100, -5.0, 0.0 };
            testData[3] = new double[] { 100, -5.0, 0.0 };
            testData[4] = new double[] { 490, 5.0, 0.0 };
            testData[5] = new double[] { 490, -5.0, 0.0 };

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

            Assert.True(result.PredictedResults[0] == 1);
            Assert.True(result.PredictedResults[1] == 0);
            Assert.True(result.PredictedResults[2] == 0);
            Assert.True(result.PredictedResults[3] == 0);
            Assert.True(result.PredictedResults[4] == 1);
            Assert.True(result.PredictedResults[5] == 0);
        }
        public void GaussianAndMean()
        {
            LearningApi lApi = new LearningApi();

            lApi.UseActionModule <double[, , ], double[, , ]>((input, ctx) =>
            {
                Bitmap myBitmap = new Bitmap($"{appPath}/TestPicture/test.gif");

                double[,,] data = new double[myBitmap.Width, myBitmap.Height, 3];

                for (int x = 0; x < myBitmap.Width; x++)
                {
                    for (int y = 0; y < myBitmap.Height; y++)
                    {
                        Color pixelColor = myBitmap.GetPixel(x, y);

                        data[x, y, 0] = pixelColor.R;
                        data[x, y, 1] = pixelColor.G;
                        data[x, y, 2] = pixelColor.B;
                    }
                }
                return(data);
            });

            lApi.AddModule(new GaussianFilter());

            lApi.AddModule(new MeanFilter());

            double[,,] result = lApi.Run() as double[, , ];

            Assert.True(result != null);

            Bitmap blurBitmap = new Bitmap(result.GetLength(0), result.GetLength(1));

            for (int x = 0; x < result.GetLength(0); x++)
            {
                for (int y = 0; y < result.GetLength(1); y++)
                {
                    Color pixelColor = Color.FromArgb((int)result[x, y, 0], (int)result[x, y, 1], (int)result[x, y, 2]);

                    blurBitmap.SetPixel(x, y, pixelColor);
                }
            }

            SaveImage(blurBitmap, "GaussianAndMean.jpg");
        }
Exemplo n.º 24
0
        public void CRbm_ClassifierTest()
        {
            var dataPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\rbm_twoclass_sample.csv");

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

            // Initialize data provider
            api.UseCsvDataProvider(dataPath, ';', false, 1);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0.1, 0.2, 0.3 };
            api.UseCRbm(featureVector, 0.01, 1000, 10, 2);
            RbmResult score = api.Run() as RbmResult;

            double[][] testData = new double[5][];

            //
            // This test data contains two patterns. One is grouped at left and one at almost right.
            testData[0] = new double[] { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
            testData[1] = new double[] { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
            testData[2] = new double[] { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 };
            testData[3] = new double[] { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 };

            // This will be classified as third class.
            testData[4] = new double[] { 1, 1, 1, 0, 0, 1, 1, 1, 0, 0 };

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

            //
            // 2 * BIT1 + BIT2 of [0] and [1] should be same.
            // We don't know how RBM will classiffy data. We only expect that
            // same or similar pattern of data will be assigned to the same class.
            // Note, we have here two classes (two hiddne nodes).
            // First and second data sample are of same class. Third and fourth are also of same class.

            // Here we check first classs.
            Assert.True(2 * result.HiddenNodesPredictions[0][0] + result.HiddenNodesPredictions[0][1] ==
                        2 * result.HiddenNodesPredictions[1][0] + result.HiddenNodesPredictions[1][1]);

            // Here is test for second class.
            Assert.True(2 * result.HiddenNodesPredictions[2][0] + result.HiddenNodesPredictions[2][1] ==
                        2 * result.HiddenNodesPredictions[3][0] + result.HiddenNodesPredictions[3][1]);

            printVector("Weights", result.Weights);
        }
Exemplo n.º 25
0
        public void LogisticsRegression_Test_iterations_10_learningrate_013()
        {
            var         desc = loadMetaData();
            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());
            });

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

            api.UseMinMaxNormalizer();

            //run logistic regression for 10 iterations with learningRate=0.13
            api.UseLogisticRegression(0.13, 10);

            api.Run();

            IScore score = api.GetScore();

            //Errors during each iteration. IF the learningRate is suitable erros is descrising for every next iteration
            Assert.Equal(Math.Round(score.Errors[0], 5), 0.24278);
            Assert.Equal(Math.Round(score.Errors[1], 5), 0.23749);
            Assert.Equal(Math.Round(score.Errors[2], 5), 0.23359);
            Assert.Equal(Math.Round(score.Errors[3], 5), 0.23010);
            Assert.Equal(Math.Round(score.Errors[4], 5), 0.22740);
            Assert.Equal(Math.Round(score.Errors[5], 5), 0.22476);
            Assert.Equal(Math.Round(score.Errors[6], 5), 0.22271);
            Assert.Equal(Math.Round(score.Errors[7], 5), 0.22065);
            Assert.Equal(Math.Round(score.Errors[8], 5), 0.21902);
            Assert.Equal(Math.Round(score.Errors[9], 5), 0.21739);

            //LG Model Best Found model in 10 iteration
            Assert.Equal(Math.Round(score.Weights[0], 5), 0.06494);
            Assert.Equal(Math.Round(score.Weights[1], 5), 0.21584);
            Assert.Equal(Math.Round(score.Weights[2], 5), 0.89901);
            Assert.Equal(Math.Round(score.Weights[3], 5), 0.51497);
            Assert.Equal(Math.Round(score.Weights[4], 5), -0.30213);
            Assert.Equal(Math.Round(score.Weights[5], 5), -0.30213);
            Assert.Equal(Math.Round(score.Weights[6], 5), -0.85624);
        }
        public void CSVDataProviderTest_IrisData()
        {
            //
            //iris data file
            var isris_path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), m_iris_data_path);

            LearningApi api = new LearningApi(TestHelpers.GetDescriptor());

            api.UseCsvDataProvider(isris_path, ',', false, 1);

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

            //get expected result
            var expected = GetReal_Iris_DataSet();

            for (int i = 0; i < result.Length; i++)
            {
                for (int j = 0; j < result[0].Length; j++)
                {
                    var col = api.Context.DataDescriptor.Features[j];
                    if (col.Type == ColumnType.STRING)
                    {
                        continue;
                    }
                    else if (col.Type == ColumnType.NUMERIC)//numeric column
                    {
                        var val1 = double.Parse((string)result[i][j], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture);
                        var val2 = Convert.ToDouble(expected[i][j], CultureInfo.InvariantCulture);

                        Assert.Equal(val1, val2);
                    }
                    else if (col.Type == ColumnType.BINARY)//binary column
                    {
                        Assert.Equal(result[i][j].ToString(), expected[i][j].ToString());
                    }
                    else if (col.Type == ColumnType.CLASS)//class column
                    {
                        Assert.Equal(result[i][j].ToString(), expected[i][j].ToString());
                    }
                }
            }

            return;
        }
        public void CSVDataProviderTest_SecomData()
        {
            //
            //iris data file
            var isris_path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), m_secom_data_path);

            LearningApi api = new LearningApi(TestHelpers.GetDescriptor(m_secom_data_mapper_path));

            api.UseCsvDataProvider(isris_path, ',', false, 1);

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

            //get expected result
            var expected = GetReal_Secom_DataSet();

            for (int i = 0; i < result.Length; i++)
            {
                for (int j = 0; j < result[0].Length; j++)
                {
                    var col = api.Context.DataDescriptor.Features[j];
                    if (col.Type == ColumnType.STRING)
                    {
                        Assert.Equal(result[i][j], expected[i][j]);
                    }
                    else if (col.Type == ColumnType.NUMERIC)//numeric column
                    {
                        //var val1 = double.Parse(result[i][j].ToString());
                        //var val2 = double.Parse(expected[i][j].ToString());

                        Assert.Equal(result[i][j], expected[i][j]);
                    }
                    else if (col.Type == ColumnType.BINARY)//binary column
                    {
                        Assert.Equal(result[i][j].ToString(), expected[i][j].ToString());
                    }
                    else if (col.Type == ColumnType.CLASS)//class column
                    {
                        Assert.Equal(result[i][j].ToString(), expected[i][j].ToString());
                    }
                }
            }

            return;
        }
Exemplo n.º 28
0
        public void smileyTestDeepRbm(int iterations, double learningRate, int[] layers)
        {
            LearningApi api = new LearningApi(getDescriptorForRbm(1600));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\Smiley.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            api.UseDeepRbm(learningRate, iterations, layers);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmDeepScore score = api.Run() as RbmDeepScore;

            watch.Stop();

            var testData = ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\SmileyTest.csv"));

            var result               = api.Algorithm.Predict(testData, api.Context) as RbmDeepResult;
            var accList              = new double[result.Results.Count];
            var predictions          = new double[result.Results.Count][];
            var predictedHiddenNodes = new double[result.Results.Count][];
            var Time = watch.ElapsedMilliseconds / 1000;

            int i = 0;

            foreach (var item in result.Results)
            {
                predictions[i]          = item.First().VisibleNodesPredictions;
                predictedHiddenNodes[i] = item.Last().HiddenNodesPredictions;
                accList[i] = testData[i].GetHammingDistance(predictions[i]);
                i++;
            }
            var ValTest  = calcDelta(predictions, testData);
            var lossTest = ValTest / (layers.First());

            Debug.WriteLine($"lossTest: {lossTest}");

            WriteDeepResult(iterations, layers, accList, Time / 60.0, predictedHiddenNodes);
            /// write predicted hidden nodes.......
            WriteOutputMatrix(iterations, layers, predictions, testData);
        }
Exemplo n.º 29
0
        public void DigitRecognitionDeepTest(int iterations, double learningRate, int[] layers)
        {
            Debug.WriteLine($"{iterations}-{String.Join("", layers)}");

            LearningApi api = new LearningApi(getDescriptorForRbmTwoClassesClassifier(4096));

            // Initialize data provider
            // TODO: Describe Digit Dataset.
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\DigitDataset.csv"), ',', false, 0);
            api.UseDefaultDataMapper();

            api.UseDeepRbm(learningRate, iterations, layers);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmDeepScore score = api.Run() as RbmDeepScore;

            watch.Stop();

            var testData = RbmHandwrittenDigitUnitTests.ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\predictiondigitdata.csv"));

            var result               = api.Algorithm.Predict(testData, api.Context) as RbmDeepResult;
            var accList              = new double[result.Results.Count];
            var predictions          = new double[result.Results.Count][];
            var predictedHiddenNodes = new double[result.Results.Count][];
            var Time = watch.ElapsedMilliseconds / 1000;

            int i = 0;

            foreach (var item in result.Results)
            {
                predictions[i]          = item.First().VisibleNodesPredictions;
                predictedHiddenNodes[i] = item.Last().HiddenNodesPredictions;
                accList[i] = testData[i].GetHammingDistance(predictions[i]);
                i++;
            }

            RbmHandwrittenDigitUnitTests.WriteDeepResult(iterations, layers, accList, Time * 1000, predictedHiddenNodes);
            /// write predicted hidden nodes.......
            RbmHandwrittenDigitUnitTests.WriteOutputMatrix(iterations, layers, predictions, testData);
        }
        public void Test_OptimalNumberOfCLusters()
        {
            // directory to load
            string loadDirectory = rootFolder + "Functions\\";
            string FunctionName  = "SIN X"; //without extension
            string savePath      = rootFolder + "Optimal Clusters\\" + FunctionName + " Results.csv";

            double[][] function = Helpers.LoadFunctionData(loadDirectory + FunctionName + "\\" + FunctionName + ".csv");
            function = TestFunctionGenerators.normalizeData(function);

            int numAttributes = 2;   // 2 in this demo (height,weight)
            int numClusters   = 0;   // 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();

            api.UseActionModule <object, double[][]>((data, ctx) =>
            {
                return(KMeansAlgorithm.transposeFunction(function));
            });

            api.UseKMeans(settings);

            // train
            var resp = api.Run() as KMeansScore;

            Assert.True(resp.Model.NumberOfClusters > 1);

            double[][] OptimalClustersResults = new double[4][];
            OptimalClustersResults[0] = new double[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            OptimalClustersResults[1] = resp.Model.D;
            OptimalClustersResults[2] = resp.Model.DPrime;
            OptimalClustersResults[3] = resp.Model.Fmin;

            Helpers.Write2CSVFile(OptimalClustersResults, savePath);

            // implement
        }