Пример #1
0
        public static void Run(IEnumerable <string> args)
        {
            if (args.Count() != 1)
            {
                Console.WriteLine("Usage: AnnLab <nRuns>");
                return;
            }
            int nRuns = int.Parse(args.First());

            if (nRuns < 0)
            {
                Console.WriteLine("Usage: AnnLab <nRuns>");
                return;
            }
            int[] possibleN = { 100, 200 };
            int[] possibleP = { 10, 20, 30, 40, 50, 75, 100, 150, 200 };

            var jobs = possibleN.SelectMany(N => possibleP.Select(p => new JobDescription {
                N = N, p = p
            }));
            var runs = Enumerable.Repeat(jobs, nRuns).SelectMany(x => x);

            TotalJobs = runs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));

            progress.Start();

            var results  = runs.AsParallel().Select(RunJob).ToList();
            var resByJob = results
                           .GroupBy(res => Tuple.Create(res.Job.p, res.Job.N));
            var avgByJob = resByJob.Select(byJob => Tuple.Create(byJob.Key.Item1 / (double)byJob.Key.Item2,
                                                                 byJob.Key,
                                                                 byJob.Average(res => res.AverageErrorRate)))
                           .OrderBy(byJob => byJob.Item1);


            string fileStr  = nRuns + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string filename = "result_" + fileStr + ".txt";

            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew), Encoding.ASCII))
            {
                foreach (var avg in avgByJob)
                {
                    sw.WriteLine(avg.Item1.ToString(CultureInfo.InvariantCulture) + " " + avg.Item3.ToString(CultureInfo.InvariantCulture) + " " + avg.Item2.Item1 + " " + avg.Item2.Item2);
                }
            }

            string errorLog = "result_matlab_" + fileStr + ".log";

            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Task1Grapher", "'" + filename + "'"))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }
            Console.WriteLine("Done!");
        }
Пример #2
0
        public static void Run(IEnumerable <string> args)
        {
#if DEBUG
            args = new List <string> {
                "60000"
            };
#endif
            if (args.Count() < 1)
            {
                Console.WriteLine("Must specify nRuns");
                return;
            }
            if (args.Count() > 2)
            {
                Console.WriteLine("Too many arguments");
                return;
            }
            int nRuns = int.Parse(args.First());
            if (nRuns < 0)
            {
                Console.WriteLine("nRuns can't be negative");
                return;
            }
            int qSteps = 100;
            if (args.Count() == 2)
            {
                qSteps = int.Parse(args.Last());
                if (qSteps < 1)
                {
                    Console.WriteLine("qSteps can't be zero or negative");
                    return;
                }
            }
            var             qs = Enumerable.Range(0, qSteps).Select(step => step * 1d / (qSteps - 1));
            Matrix <double> W  = Task1.InitWeights(160, DIGITS);

            var jobs = qs.Select(q => new JobDescription {
                q = q, W = W
            });
            var runs = Enumerable.Repeat(jobs, nRuns).SelectMany(x => x);
            TotalJobs = runs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));
            progress.Start();

            var results         = runs.AsParallel().Select(RunJob).ToList();
            var resByJob        = results.GroupBy(res => res.q);
            var avgByJobByDigit = AggregateThrowsStackOverflowExceptionWorkaround(resByJob, nRuns);
            var avgByJob        = resByJob.Select(byJob => Tuple.Create(byJob.Key,
                                                                        avgByJobByDigit[byJob.Key],

                                                                        /* Throws StackOverflowException when nRuns is somewhat large,
                                                                         * avgByJobByDigit is a "manual" implementation that works
                                                                         * byJob.Select(res => res.Corrects)
                                                                         *   .Aggregate(Enumerable.Repeat(0, DIGITS.Length), (acc, next) => acc.Zip(next, (a, b) => b ? a + 1 : a))
                                                                         *   .Select(avg => avg / (double)nRuns),*/
                                                                        byJob.Average(res => res.AverageCorrectRate)))
                                  .OrderBy(avg => avg.Item1);

            string fileStr  = nRuns + "_" + qSteps + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string filename = "task2_" + fileStr + ".txt";
            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew), Encoding.ASCII))
            {
                foreach (var avg in avgByJob)
                {
                    sw.Write(avg.Item1.ToString(CultureInfo.InvariantCulture));
                    foreach (var dig in avg.Item2)
                    {
                        sw.Write(" " + dig.ToString(CultureInfo.InvariantCulture));
                    }
                    sw.WriteLine(" " + avg.Item3.ToString(CultureInfo.InvariantCulture));
                }
            }

            string errorLog = "task2_" + fileStr + ".log";
            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Task2Grapher", "'" + filename + "'"))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }
            Console.WriteLine("Done!");
        }
Пример #3
0
        public static void Run(IEnumerable <string> args)
        {
#if DEBUG
            args = new List <string> {
                "100"
            };
#endif
            if (args.Count() != 1)
            {
                Console.WriteLine("Usage: lab2.task1 <sigma_0>");
                return;
            }
            double sigma0 = int.Parse(args.First());
            if (sigma0 < 1)
            {
                Console.WriteLine("sigma_0 must be larger than 0");
                return;
            }
            double n0 = 0.1;
            int    T = 200;
            double sigmaconv = 0.9;
            double nconv = 0.01;
            int    iters_order = (int)1E3, iters_conv = (int)5E4;
            string dateStr = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string fileStr = sigma0 + "_" + dateStr;

            TotalJobs = iters_order + iters_conv;
            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));
            progress.Start();

            //Matrix<double> input = GenerateTriangleInput(1000, 1, 0, 0.5, 1);
            Matrix <double> input   = GenerateCircleInput(1000);
            Matrix <double> weights = new Matrix <double>(100, 2);
            Random          rng     = new Random();
            for (int i = 0; i < weights.Rows; i++)
            {
                for (int j = 0; j < weights.Cols; j++)
                {
                    weights[i, j] = rng.NextDouble();
                }
            }
            double[] hood = new double[weights.Rows];

            // ordering phase
            for (int t = 0; t < iters_order; t++)
            {
                int    target       = rng.Next(input.Rows);
                int    bmu          = BMU(input, weights, target);
                double sigma        = NeighbourhoodWidth(t, T, sigma0);
                double learningRate = LearningRate(t, T, n0);
                Neighbourhood(hood, bmu, sigma);
                for (int i = 0; i < weights.Rows; i++)
                {
                    weights[i, 0] += hood[i] * learningRate * (input[target, 0] - weights[i, 0]);
                    weights[i, 1] += hood[i] * learningRate * (input[target, 1] - weights[i, 1]);
                }
                JobsCompleted++;
            }

            string filename = "lab2task1_ordering_" + fileStr + ".txt";
            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew)))
            {
                for (int i = 0; i < weights.Rows; i++)
                {
                    sw.WriteLine(weights[i, 0].ToString(CultureInfo.InvariantCulture) + ", " + weights[i, 1].ToString(CultureInfo.InvariantCulture));
                }
            }

            // convergance phase
            double[][] hoods = new double[input.Rows][];
            for (int t = 0; t < iters_conv; t++)
            {
                int target = rng.Next(input.Rows);
                int bmu    = BMU(input, weights, target);
                if (hoods[bmu] == null)
                {
                    Neighbourhood(hoods[bmu] = new double[weights.Rows], bmu, sigmaconv);
                }
                for (int i = 0; i < weights.Rows; i++)
                {
                    weights[i, 0] += hoods[bmu][i] * nconv * (input[target, 0] - weights[i, 0]);
                    weights[i, 1] += hoods[bmu][i] * nconv * (input[target, 1] - weights[i, 1]);
                }
                JobsCompleted++;
            }

            filename = "lab2task1_convergance_" + fileStr + ".txt";
            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.Create)))
            {
                for (int i = 0; i < weights.Rows; i++)
                {
                    sw.WriteLine(weights[i, 0].ToString(CultureInfo.InvariantCulture) + ", " + weights[i, 1].ToString(CultureInfo.InvariantCulture));
                }
            }

            filename = "lab2task1_input_" + fileStr + ".txt";
            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew)))
            {
                for (int i = 0; i < input.Rows; i++)
                {
                    sw.WriteLine(input[i, 0].ToString(CultureInfo.InvariantCulture) + ", " + input[i, 1].ToString(CultureInfo.InvariantCulture));
                }
            }

            string errorLog = "lab2task1_matlab_" + fileStr + ".log";
            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Lab2Task1InputGrapher", "'lab2task1_%s_%d_" + dateStr + ".txt'", sigma0.ToString()))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }

            Console.WriteLine("Done!");
        }
Пример #4
0
        public static void Run(IEnumerable <string> args)
        {
#if DEBUG
            args = new List <string> {
                "2"
            };
#endif
            if (!args.Any())
            {
                Console.WriteLine("Usage: task3 <nRuns> <aSteps>");
                return;
            }
            if (args.Count() > 2)
            {
                Console.WriteLine("Too many arguments");
                return;
            }
            int nRuns = int.Parse(args.First());
            if (nRuns < 0)
            {
                Console.WriteLine("nRuns can't be negative");
                return;
            }
            int aSteps = 50;
            if (args.Count() == 2)
            {
                aSteps = int.Parse(args.Last());
                if (aSteps < 1)
                {
                    Console.WriteLine("aSteps can't be zero or negative");
                    return;
                }
            }

            var    alphas = Enumerable.Range(1, aSteps).Select(step => step * 1d / aSteps);
            var    Ns     = new int[] { 50, 100, 250, 500 };
            double Beta   = 2;

            foreach (var N in Ns)
            {
                if (N % aSteps != 0)
                {
                    Console.WriteLine("Invalid aSteps, " + N + " is not divisable with " + aSteps);
                    return;
                }
            }

            var jobs = alphas.SelectMany(a => Ns.Select(N => new JobDescription {
                p = Math.Max(1, (int)(a * N)), N = N, Beta = Beta
            }));
            var runs = Enumerable.Repeat(jobs, nRuns).SelectMany(x => x);
            TotalJobs = runs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));
            progress.Start();

            var results = runs.AsParallel().Select(RunJob).ToList();
            var byJob   = results.GroupBy(res => Tuple.Create(res.Job.p, res.Job.N));
            var avgByN  = byJob.GroupBy(job => job.Key.Item2, job => Tuple.Create(job.Key.Item1 / (double)job.Key.Item2, job.Average(res => res.m)));

            string argStr  = nRuns + "_" + aSteps;
            string dateStr = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            foreach (var N in avgByN)
            {
                string filename = "task3_" + argStr + "_" + N.Key + "_" + dateStr + ".txt";
                Console.WriteLine("Writing to " + filename + "...");
                using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew), Encoding.ASCII))
                {
                    foreach (var avg in N.OrderBy(avg => avg.Item1))
                    {
                        sw.WriteLine(avg.Item1.ToString(CultureInfo.InvariantCulture) + " " + avg.Item2.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }

            string errorLog = "task3_" + argStr + "_" + dateStr + ".log";
            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Task3Grapher", "'task3_" + argStr + "_%d_" + dateStr + ".txt'", "[" + string.Join(" ", Ns) + "]"))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }
            Console.WriteLine("Done!");
        }
Пример #5
0
        public static void Run(IEnumerable <string> args)
        {
            if (!args.Any())
            {
                args = new List <string> {
                    "1", "20", "5", "20"
                }
            }
            ;
            if (args.Count() < 2)
            {
                Console.WriteLine("Usage: lab2.task3 <ost_file> <kLow> <kHigh> <kDumps...>");
                return;
            }
            string ostFile;
            int    koffset;

            if (int.TryParse(args.First(), out koffset))
            {
                if (args.Count() < 3)
                {
                    Console.WriteLine("Usage: lab2.task3 <ost_file> <kLow> <kHigh> <kDumps...>");
                    return;
                }
                ostFile = "C:\\ann\\task3.txt";
                koffset = 0;
            }
            else
            {
                ostFile = args.First();
                koffset = 1;
            }
            int klow = int.Parse(args.Skip(koffset).First()), khigh = int.Parse(args.Skip(koffset + 1).First());

            if (klow < 1)
            {
                Console.WriteLine("kLow must be larger than 0");
                return;
            }
            if (khigh < klow)
            {
                Console.WriteLine("kHigh must be larger than kLow");
                return;
            }
            List <int> kDumps = args.Skip(koffset + 2).Select(x => int.Parse(x)).ToList();

            int[]  ks = Enumerable.Range(klow, khigh - klow + 1).ToArray();
            int    iters_kohonen = (int)1E5;
            double eta_kohonen = 0.02;
            int    iters_perceptron = 3000;
            double beta = 0.5, eta_perceptron = 0.1;
            int    runsPerJob = 20;

            int[][]         ostClasses;
            Matrix <double> ost = ReadOst(ostFile, out ostClasses);

            var jobs = ks.SelectMany(k => Enumerable.Repeat(new JobDescription
            {
                ost              = ost,
                ostClasses       = ostClasses,
                iters_kohonen    = iters_kohonen,
                iters_perceptron = iters_perceptron,
                eta_kohonen      = eta_kohonen,
                eta_perceptron   = eta_perceptron,
                beta             = beta,
                k = k,
            }, runsPerJob));

            TotalJobs = jobs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));

            progress.Start();

            var results    = jobs.AsParallel().Select(RunJob).ToList();
            var resultsByK = results.GroupBy(res => res.k).OrderBy(byK => byK.Key);

            string dateStr  = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string filename = "lab2task3_k_" + dateStr + ".txt";

            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew)))
            {
                foreach (var res in resultsByK)
                {
                    sw.WriteLine(res.Key + "," + res.Average(r => r.validationErrorRate).ToString(CultureInfo.InvariantCulture));

                    if (kDumps.Contains(res.Key))
                    {
                        string fileStr  = res.Key + "_" + dateStr;
                        var    minError = res.OrderBy(r => r.validationErrorRate).First();

                        string dumpname = "lab2task3_kohonen_" + fileStr + ".txt";
                        Console.WriteLine("Writing to " + dumpname + "...");
                        using (StreamWriter sw2 = new StreamWriter(new FileStream(dumpname, FileMode.CreateNew)))
                        {
                            for (int i = 0; i < minError.weights.Rows; i++)
                            {
                                sw2.WriteLine(minError.weights[i, 0].ToString(CultureInfo.InvariantCulture) + "," + minError.weights[i, 1].ToString(CultureInfo.InvariantCulture));
                            }
                        }

                        BoundaryDump(fileStr, ost, minError.weights, minError.radial, minError.nn);
                    }
                }
            }

            string errorLog = "lab2task3_" + dateStr + ".log";

            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Lab2Task3Grapher", "'" + ostFile + "'", "'lab2task3_%s_" + dateStr + ".txt'", "[" + string.Join(" ", kDumps) + "]"))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }
            Console.WriteLine("Done!");
        }
Пример #6
0
        public static void Run(IEnumerable <string> args)
        {
            int nRuns;

            if (!Task4a.ParseArgs("task4b", ref args, out nRuns, out Dump))
            {
                return;
            }
            DateStr = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");

            Matrix <double>[][] dataAll;
            int[][][]           classesAll;
            Task4a.ReadNormalizeSplit(out classesAll, out dataAll, args.First(), args.Skip(1).First());
            int[][]           trainingClasses = classesAll[0], validationClasses = classesAll[1];
            Matrix <double>[] trainingData    = dataAll[0], validationData = dataAll[1];

            double Beta         = 0.5;
            double learningRate = 0.01;

            int[] hiddenNeurons = { 0, 2, 4, 8, 16, 32 };

            var jobs = hiddenNeurons.Select(hn => new JobDescription
            {
                HiddenNeurons     = hn,
                Beta              = Beta,
                LearningRate      = learningRate,
                TrainingData      = trainingData,
                TrainingClasses   = trainingClasses,
                ValidationData    = validationData,
                ValidationClasses = validationClasses
            });
            var runs = Enumerable.Repeat(jobs, nRuns).SelectMany(x => x);

            TotalJobs = runs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));

            progress.Start();

            var results = runs.AsParallel().Select(RunJob).ToList();

            if (!Dump)
            {
                var    byNeurons = results.GroupBy(res => res.HiddenNeurons).OrderBy(n => n.Key);
                string filename  = "task4b_" + nRuns + "_" + DateStr + ".txt";
                Console.WriteLine("Writing to " + filename + "...");
                using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew), Encoding.ASCII))
                {
                    foreach (var byNeuron in byNeurons)
                    {
                        double trainingErrorRate   = byNeuron.Average(res => res.Training);
                        double validationErrorRate = byNeuron.Average(res => res.Validation);

                        sw.WriteLine(byNeuron.Key + " " +
                                     trainingErrorRate.ToString(CultureInfo.InvariantCulture) + " " +
                                     validationErrorRate.ToString(CultureInfo.InvariantCulture));
                    }
                }

                string errorLog = "task4b_" + nRuns + "_" + DateStr + ".log";
                Console.WriteLine("Executing MATLAB script...");
                if (!MATLAB.RunScript(errorLog, "Task4bGrapher", "'" + filename + "'"))
                {
                    Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
                }
            }
            else
            {
                string errorRateFile = Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "task4b_*_*.txt")
                                       .Where(file => !file.StartsWith("task4b_dump_"))
                                       .OrderBy(file => file)
                                       .LastOrDefault();
                if (errorRateFile == null)
                {
                    Console.WriteLine("Unable to find an error rates file, skipping MATLAB script...");
                }
                else
                {
                    Console.WriteLine(errorRateFile);
                    string errorLog = "task4b_dump_" + DateStr + ".log";
                    string dumpname = "task4b_dump_%d_" + DateStr + ".txt";
                    Console.WriteLine("Executing MATLAB script...");
                    if (!MATLAB.RunScript(errorLog, "Task4bExtra", "'" + dumpname + "'", "'" + errorRateFile + "'"))
                    {
                        Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
                    }
                }
            }
            Console.WriteLine("Done!");
        }
Пример #7
0
        public static void Run(IEnumerable <string> args)
        {
            double sigma_0 = 30, eta_0 = 0.1, sigma_conv = 0.9, eta_conv = 0.01;
            int    T_order = (int)1E3, T_conv = (int)2E4, tou = 300;

            if (!args.Any())
            {
                args = new List <string> {
                    "C:\\ann\\wine.data.txt"
                };
            }
            if (args.Count() > 1)
            {
                Console.WriteLine("Usage: lab2.task2 <wine_file>");
                return;
            }
            string wineFile = args.First();

            int[]           wineClasses;
            Matrix <double> wine = ReadWine(wineFile, out wineClasses);

            NormalizeMeanAndVarInPlace(wine);
            double[,,] weights = GenerateRandomWeights();
            double[,] hood     = new double[20, 20];
            Random rng = new Random();

            TotalJobs = T_order + T_conv;
            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));

            progress.Start();

            // ordering phase
            for (int t = 0; t < T_order; t++)
            {
                int wineIndex = rng.Next(wine.Rows);
                var bmu       = BMU(wine, weights, wineIndex);
                Neighbourhood(hood, bmu, Task1.NeighbourhoodWidth(t, tou, sigma_0));
                double eta_t = Task1.LearningRate(t, tou, eta_0);
                for (int i = 0; i < weights.GetLength(0); i++)
                {
                    for (int j = 0; j < weights.GetLength(1); j++)
                    {
                        if (hood[i, j] == 0)
                        {
                            continue;
                        }
                        for (int k = 0; k < weights.GetLength(2); k++)
                        {
                            weights[i, j, k] += hood[i, j] * eta_t * (wine[wineIndex, k] - weights[i, j, k]);
                        }
                    }
                }
                JobsCompleted++;
            }

            // convergance phase
            double[, ][,] hoods = new double[weights.GetLength(0), weights.GetLength(1)][, ];
            for (int t = 0; t < T_conv; t++)
            {
                int wineIndex = rng.Next(wine.Rows);
                var bmu       = BMU(wine, weights, wineIndex);
                double[,] myHood = hoods[bmu.Item1, bmu.Item2];
                if (myHood == null)
                {
                    Neighbourhood(hoods[bmu.Item1, bmu.Item2] = myHood = new double[20, 20], bmu, sigma_conv);
                }
                for (int i = 0; i < weights.GetLength(0); i++)
                {
                    for (int j = 0; j < weights.GetLength(1); j++)
                    {
                        for (int k = 0; k < weights.GetLength(2); k++)
                        {
                            weights[i, j, k] += myHood[i, j] * eta_conv * (wine[wineIndex, k] - weights[i, j, k]);
                        }
                    }
                }
                JobsCompleted++;
            }

            // coloring phase
            int[,] colors = new int[20, 20];
            for (int wineIndex = 0; wineIndex < wine.Rows; wineIndex++)
            {
                var bmu = BMU(wine, weights, wineIndex);
                colors[bmu.Item1, bmu.Item2] = wineClasses[wineIndex];
            }

            string dateStr  = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string filename = "lab2task2_" + dateStr + ".txt";

            Console.WriteLine("Writing to " + filename + "...");
            using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew)))
            {
                for (int i = 0; i < colors.GetLength(0); i++)
                {
                    for (int j = 0; j < colors.GetLength(1); j++)
                    {
                        sw.WriteLine(i + "," + j + "," + colors[i, j]);
                    }
                }
            }

            string errorLog = "lab2task2_" + dateStr + ".log";

            Console.WriteLine("Executing MATLAB script...");
            if (!MATLAB.RunScript(errorLog, "Lab2Task2Grapher", "'" + filename + "'"))
            {
                Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
            }
            Console.WriteLine("Done!");
        }
Пример #8
0
        public static void Run(IEnumerable <string> args)
        {
            int nRuns;

            if (!ParseArgs("task4a", ref args, out nRuns, out Dump))
            {
                return;
            }
            DateStr = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");

            Matrix <double>[][] dataAll;
            int[][][]           classesAll;
            ReadNormalizeSplit(out classesAll, out dataAll, args.First(), args.Skip(1).First());
            int[][]           trainingClasses = classesAll[0], validationClasses = classesAll[1];
            Matrix <double>[] trainingData    = dataAll[0], validationData = dataAll[1];

            int[]  Ns           = new int[] { 2, 1 };
            double Beta         = 0.5;
            double learningRate = 0.01;

            var job = new JobDescription
            {
                Beta              = Beta,
                LearningRate      = learningRate,
                Ns                = Ns,
                TrainingData      = trainingData,
                TrainingClasses   = trainingClasses,
                ValidationData    = validationData,
                ValidationClasses = validationClasses
            };
            var runs = Enumerable.Repeat(job, nRuns);

            TotalJobs = runs.Count();

            Thread progress = new Thread(() => Progress.ProgressFunc(ref TotalJobs, ref JobsCompleted));

            progress.Start();

            var results = runs.AsParallel().Select(RunJob).ToList();

            if (!Dump)
            {
                double trainingErrorRate   = results.Average(res => res.Training);
                double validationErrorRate = results.Average(res => res.Validation);

                string filename = "task4a_" + nRuns + "_" + DateStr + ".txt";
                Console.WriteLine("Writing to " + filename + "...");
                using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew), Encoding.ASCII))
                {
                    sw.WriteLine("Average training error: " + trainingErrorRate.ToString(CultureInfo.InvariantCulture));
                    sw.WriteLine("Average validation error: " + validationErrorRate.ToString(CultureInfo.InvariantCulture));
                }
            }
            else
            {
                string errorLog = "task4a_dump_" + DateStr + ".log";
                string dumpname = "task4a_dump_" + DateStr + ".txt";
                Console.WriteLine("Executing MATLAB script...");
                if (!MATLAB.RunScript(errorLog, "Task4aGrapher", "'" + dumpname + "'"))
                {
                    Console.WriteLine("An error occured while running MATLAB, check the log\n\tLog file:" + errorLog);
                }
            }
            Console.WriteLine("Done!");
        }