accord-net / framework

Machine learning, computer vision, statistics and general scientific computing for .NET
http://accord-framework.net
GNU Lesser General Public License v2.1
4.49k stars 1.99k forks source link

System.AggregateException thrown in C45Learning.Run #113

Closed liadmat closed 8 years ago

liadmat commented 9 years ago

In v2.15, when you have 2 inputs that are exactly the same but for some reason have different outputs, you get an unhandled exception:

double[][] inputs = new double[][] {
    new double[] { 0 },
    new double[] { 0 }
};

int[] outputs = new int[] {
    1,
    0
};

DecisionVariable[] variables = { new DecisionVariable("x", DecisionVariableKind.Continuous) };

DecisionTree decisionTree = new DecisionTree(variables, 2);
C45Learning c45Learning = new C45Learning(decisionTree);
c45Learning.Run(inputs, outputs); // System.AggregateException thrown here
A first chance exception of type 'System.AggregateException' occurred in mscorlib.dll
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action`1 body)
   at Accord.MachineLearning.DecisionTrees.Learning.C45Learning.split(DecisionNode root, Double[][] input, Int32[] output, Int32 height)
   at Accord.MachineLearning.DecisionTrees.Learning.C45Learning.Run(Double[][] inputs, Int32[] outputs)

It also happens with more than 2 inputs (for example, you can make the DecisionTree sample app crash by changing examples.xls). ID3 algorithm isn't affected.

Thanks, Liad

kozzion commented 8 years ago

I have the same problem but no 2 inputs are the same (22 instances, 450 features per input no duplicate instances or even feature values).

Thanks, kozzion

erictg commented 8 years ago

I have the same problem, 450 instances with 49 features. I think this might be a problem where no 2 features can have the same value with different answers.

cesarsouza commented 8 years ago

Hello all,

I have just pushed a commit addressing the original issue, related to variables with same input value but different outputs. If you are still experiencing problems with unhandled exceptions in C4.5 learning, could you please share an example of the data you are using so I could reproduce the issue?

Regards, Cesar

g-yz commented 7 years ago

Hello, I do not know what is wrong, I keep getting error: First exception of type 'System.AggregateException' in mscorlib.dll 'ConsoleApplication1.vshost.exe' (CLR v4.0.30319: ConsoleApplication1.vshost.exe): 'C: \ Windows \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.DebuggerVisualizers \ 12.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.DebuggerVisualizers.dll' loaded . The PDB file can not be found or can not be opened.

`using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

using Accord.Statistics.Distributions.Fitting; using Accord.Statistics.Distributions.Multivariate;

using Accord.Statistics.Models.Fields; using Accord.Statistics.Models.Fields.Functions; using Accord.Statistics.Models.Fields.Learning;

using Accord.Statistics.Models.Markov; using Accord.Statistics.Models.Markov.Learning; using Accord.Statistics.Models.Markov.Topology; using Accord.Math; using Accord.Statistics.Distributions.Univariate;

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { double[][] hello = { new double[] {-4.19, 116.17, 115.93, 0, -0.1668, 0, -38.1241, 0, -2.6264, 30, 0, 0, 38.5214, 1.9938, -0.0658, 0, 0, 0, -9.3119, -52.3753, -71.9811, -9.9242, 2.6063, 14.5686, 21.2838, 0, -28.6057, -14.1763, -0.5994, 5.8565, 0, 0, 0, 0, 0, 0, -0.4007, 47.5658, 63.3098, 0, 0, -21.9515, -9.0779, 0, -9.3351, -6.2466, -0.005, -0.1514, 0.1878, 0.2307, 1.3336, 0, 0, 0, -8.0304, -0.0449, -2.613, -0.5257, -0.4277, -2.3007, 0, 0, 0}, new double[] {-3.75, 116.01, 115.95, 0, 0.1556, 0, -38.0077, 0, -2.9091, 30, 0, 0, 38.6679, 2.0705, -0.243, 0, 0, 0, -10.1249, -52.5701, -72.96, -9.0958, 2.3362, 14.2844, 22.0125, 0, -28.875, -14.1321, -0.5756, 5.5827, 0, 0, 0, 0, 0, 0, -0.5115, 47.8681, 63.1402, 0, 0, -22.1091, -8.4312, 0, -8.1592, -6.2566, -0.0201, -0.6139, 0.1405, 0.2761, 1.5881, 0, 0, 0, -8.0785, -0.0471, -2.8105, -0.3342, -0.4365, -2.3926, 0, 0, 0}, new double[] {-3.35, 115.66, 116, 0, 0.4967, 0, -37.9239, 0, -3.0898, 30, 0, 0, 38.7877, 2.0563, -0.3979, 0, 0, 0, -10.7685, -52.69, -73.766, -7.675, 1.8786, 13.6722, 18.5423, 0, -27.2261, -14.0448, -0.5427, 5.1887, 0, 0, 0, 0, 0, 0, -0.488, 48.0964, 62.8251, 0, 0, -22.0748, -8.8559, 0, -5.4322, -6.2245, -0.0346, -1.0497, 0.0067, 0.2869, 1.628, 0, 0, 0, -8.1804, -0.0497, -3.1314, -0.0163, -0.4348, -2.4603, 0, 0, 0}, new double[] {-3.02, 115.97, 116.03, 0, 0.7903, 0, -37.8929, 0, -3.3172, 30, 0, 0, 38.8604, 2.058, -0.4729, 0, 0, 0, -11.005, -52.619, -74.1992, -7.1933, 1.7475, 13.5834, 15.9675, 0, -26.1534, -13.8407, -0.5809, 5.3669, 0, 0, 0, 0, 0, 0, -0.5661, 48.3297, 62.6837, 0, 0, -21.5302, -9.4083, 0, -4.0718, -6.138, -0.0457, -1.3565, -0.2273, 0.257, 1.4243, 0, 0, 0, -8.4168, -0.0472, -3.4182, 0.5696, -0.4416, -2.6566, 0, 0, 0}, new double[] {-2.52, 116.03, 116.15, 0, 1.1039, 0, -37.9193, 0, -3.5958, 30, 0, 0, 38.8858, 2.1112, -0.4529, 0, 0, 0, -10.8813, -52.3768, -74.3113, -7.4923, 1.8662, 13.9076, 11.5848, 0, -19.8312, -13.3293, -0.5784, 4.9335, 0, 0, 0, 0, 0, 0, -1.0806, 48.7982, 63.0211, 0, 0, -20.1967, -10.0364, 0, -2.5429, -5.9903, -0.0544, -1.5551, -0.5761, 0.1793, 0.9601, 0, 0, 0, -9.0744, -0.0287, -3.5554, 2.098, -0.4609, -3.3173, 0, 0, 0}, new double[] {-2.13, 115.94, 116.28, 0, 1.4303, 0, -37.9853, 0, -3.8627, 30, 0, 0, 38.8695, 2.1845, -0.3698, 0, 0, 0, -10.3964, -51.9214, -74.1498, -7.6544, 1.9514, 14.2178, 7.7959, 0, -16.5115, -12.9736, -0.5988, 4.847, 0, 0, 0, 0, 0, 0, -0.7495, 48.9629, 62.2545, 0, 0, -23.1314, -7.0218, 0, 9.8186, -5.7449, -0.0545, -1.4672, -1.0995, -0.0087, -0.0442, 0, 0, 0, -10.2592, 0.008, -3.5544, 4.6907, -0.418, -4.4885, 0, 0, 0}, };

        double[][] car =
        {
            new double[] {3.34, 120.95, 121.49, 0, 12.835, 0, -39.5961, 0, -3.3524, 30, 0, 0, 39.0855, -1.7615, -1.0994, 0, 0, 0, -2.9296, -43.7755, -72.6799, -6.1651, 1.6746, 15.1383, -4.4379, 0, -3.2874, -9.5306, -0.0665, 0.3597, 0, 0, 0, 0, 0, 0, -1.1883, 52.7732, 58.721, 0, 0, -22.9554, -10.9315, 0, 7.1242, -5.2156, -0.0587, -1.4054, 3.2035, -0.5546, -4.6434, 0, 0, 0, -34.6775, 1.3816, -6.3099, 4.1877, 0.0982, 0.9647, 0, 0, 0}, 
            new double[] {3.43, 121.09, 121.81, 0, 14.8935, 0, -39.8019, 0, -3.4811, 30, 0, 0, 38.9793, -2.0239, -1.1578, 0, 0, 0, -2.4568, -43.1947, -72.65, -6.7903, 1.8992, 15.5534, -8.0008, 0, -2.8967, -9.3107, -0.0002, 0.0008, 0, 0, 0, 0, 0, 0, -1.0828, 53.1477, 58.1475, 0, 0, -21.4166, -5.4626, 0, 0.7315, -5.3496, -0.0519, -1.2783, 4.149, -0.4498, -4.3811, 0, 0, 0, -34.1681, 1.5897, -7.4153, 3.8206, 0.0959, 0.8856, 0, 0, 0}, 
            new double[] {3.58, 121.29, 121.95, 0, 16.6276, 0, -40.0249, 0, -3.5678, 30, 0, 0, 38.8323, -2.3071, -1.2281, 0, 0, 0, -1.9556, -42.6241, -72.5523, -7.3674, 2.1121, 15.9094, -13.4258, 0, 0.2782, -9.101, 0.0623, -0.3234, 0, 0, 0, 0, 0, 0, -1.1671, 53.6977, 57.7053, 0, 0, -22.2611, -9.5817, 0, 8.113, -5.3266, -0.0535, -1.3116, 5.4302, -0.2948, -3.6828, 0, 0, 0, -33.6451, 1.7534, -8.3625, 3.5828, 0.0963, 0.8564, 0, 0, 0}, 
            new double[] {3.59, 121.03, 122.12, 0, 17.7862, 0, -40.2542, 0, -3.6038, 30, 0, 0, 38.6685, -2.6206, -1.2698, 0, 0, 0, -1.2875, -41.9322, -72.3179, -8.0128, 2.3744, 16.3993, -14.9618, 0, 1.0348, -8.9315, 0.118, -0.6032, 0, 0, 0, 0, 0, 0, -1.3042, 54.2558, 57.3463, 0, 0, -21.5367, -12.8572, 0, 7.4241, -5.3102, -0.0556, -1.3589, 6.6784, -0.1894, -3.2605, 0, 0, 0, -33.2679, 1.8453, -8.9451, 3.7494, 0.1195, 1.0913, 0, 0, 0},
        };

        double[][] wardrobe =
        {
            new double[] {-1.03, 116.45, 111.32, 0, -8.0847, 0, -35.698, 0, -3.4739, 30, 0, 0, 38.6216, 1.1354, 0.6444, 0, 0, 0, -7.5, -51.8337, -69.8728, -9.8788, 2.7319, 15.3067, -12.3265, 0, -13.3988, -13.8453, -1.066, 9.7889, 0, 0, 0, 0, 0, 0, -1.3663, 47.464, 65.1281, 0, 0, -21.1892, 4.0188, 0, -6.1123, -6.4712, -0.3622, -11.7183, -9.0446, 0, 18.9484, 0, 0, 0, 8.727, -1.6417, -9.932, -8.3408, 0, 6.3093, 0, 0, 0}, 
            new double[] {-1.05, 116.74, 111.12, 0, -8.1749, 0, -35.5923, 0, -3.436, 30, 0, 0, 38.7, 1.1687, 0.6675, 0, 0, 0, -7.4457, -51.9501, -69.6503, -9.7284, 2.6794, 15.2517, -10.9271, 0, -12.221, -13.8587, -1.0909, 10.0342, 0, 0, 0, 0, 0, 0, -1.5479, 47.5401, 65.3504, 0, 0, -20.4952, 4.2112, 0, -8.3897, -6.908, -0.3094, -11.4243, -8.5025, 0, 18.8072, 0, 0, 0, 8.799, -1.6285, -9.8144, -8.4489, 0, 6.4579, 0, 0, 0}, 
            new double[] {-1.36, 116.61, 111.01, 0, -8.2439, 0, -35.4985, 0, -3.3919, 30, 0, 0, 38.7704, 1.1956, 0.683, 0, 0, 0, -7.3887, -52.0092, -69.4922, -9.553, 2.6322, 15.2633, -16.1248, 0, -13.2011, -13.8938, -1.112, 10.2819, 0, 0, 0, 0, 0, 0, -1.6251, 47.5437, 65.48, -13.2407, 3.4071, -14.1752, -4.3151, 0, -10.3101, -7.2851, -0.2645, -11.125, -7.9992, 0, 18.5787, 0, 0, 0, 8.8242, -1.6096, -9.6876, -8.4816, 0, 6.5375, 0, 0, 0}, 
            new double[] {-1.44, 116.81, 110.92, 0, -8.3425, 0, -35.4367, 0, -3.2925, 30, 0, 0, 38.8312, 1.197, 0.7111, 0, 0, 0, -7.3354, -52.0259, -69.3901, -9.3993, 2.5873, 15.2539, -15.4426, 0, -12.7304, -13.859, -1.1103, 10.2098, 0, 0, 0, 0, 0, 0, -1.6174, 47.4766, 65.5494, -12.9383, 3.2669, -13.9312, -5.6293, 0, -10.7042, -11.6502, 0.1134, -7.8609, 2.9177, 1.3937, 11.0767, 0, 0, 0, 8.8158, -1.5947, -9.6027, -8.4589, 0, 6.5967, 0, 0, 0},  
        };

        double[][][] words = { hello, car, wardrobe };

        int[] labels = { 0, 1, 2 };

        var teacher = new HiddenMarkovClassifierLearning<Independent<NormalDistribution>, double[]>()
        {
            // Train each model until the log-likelihood changes less than 0.001
            Learner = (i) => new BaumWelchLearning<Independent<NormalDistribution>, double[]>()
            {
                Topology = new Forward(5), // this value can be found by trial-and-error

                // We will create our classifiers assuming an independent Gaussian distribution 
                // for each component in our feature vectors (assuming a Naive Bayes assumption).
                Emissions = (s) => new Independent<NormalDistribution>(dimensions: 63), // 4 dimensions

                Tolerance = 0.001,
                MaxIterations = 100,

                // This is necessary so the code doesn't blow up when it realizes there is only one 
                // sample per word class. But this could also be needed in normal situations as well:
                FittingOptions = new IndependentOptions()
                {
                    InnerOption = new NormalOptions() { Regularization = 1e-5 }
                }
            }
        };

        // PS: In case you find exceptions trying to configure your model, you might want 
        //     to try disabling parallel processing to get more descriptive error messages:
        // teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

        // Finally, we can run the learning algorithm!

        /*try
        {*/
            var hmm = teacher.Learn(words, labels);
            Console.WriteLine("**hmm***");
             //SOLO NECESARIO PA HMM
             double logLikelihood = teacher.LogLikelihood;

            // At this point, the classifier should be successfully 
            // able to distinguish between our three word classes:
            // 
            int tc1 = hmm.Decide(hello);    // should be 0
            int tc2 = hmm.Decide(car);      // should be 1
            int tc3 = hmm.Decide(wardrobe); // should be 2

            Console.WriteLine("**hcrf***");
            // Now, we can use the Markov classifier to initialize a HCRF
            var baseline = HiddenConditionalRandomField.FromHiddenMarkov(hmm);

            // We can check that both are equivalent, although they have
            // formulations that can be learned with different methods:
            int[] predictedLabels = baseline.Decide(words);

            // Now we can learn the HCRF using one of the best learning
            // algorithms available, Resilient Backpropagation learning:

            // Create the Resilient Backpropagation learning algorithm
            var rprop = new HiddenResilientGradientLearning<double[]>()
            {
                Function = baseline.Function, // use the same HMM function

                MaxIterations = 50,
                Tolerance = 1e-5
            };

            // Run the algorithm and learn the models
            var hcrf = rprop.Learn(words, labels);

            // At this point, the HCRF should be successfully 
            // able to distinguish between our three word classes:
            // 
            int hc1 = hcrf.Decide(hello);    // should be 0
            int hc2 = hcrf.Decide(car);      // should be 1
            int hc3 = hcrf.Decide(wardrobe); // should be 2

            Console.WriteLine("*****" + hc1);
            Console.WriteLine("*****" + hc2);
            Console.WriteLine("*****" + hc3);

        /*}
        catch (AggregateException e)
        {

        }*/

    }

}

} `

kjaisinghyadav commented 6 years ago

hi, I am getting this Error " System.AggregateException was unhandled" in this line bow =surfBow.Learn(originalTrainImages.Values.ToArray()); in Classification BOW & SVM.