Exemplo n.º 1
0
        static void StochasticGradientDescent()
        {
            string dataDirectory = "C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\HW4-q1\\";

            GradientDescent gd = new StochasticGradientDescent(dataDirectory + "features.txt", dataDirectory + "target.txt");

            Stopwatch stopwatch = new Stopwatch();
            var       results   = gd.Iterate();

            using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\output\\SGD.txt"))
            {
                Console.WriteLine("  k \t      Cost\t\t   Time\n----\t----------------\t----------");
                file.WriteLine("  k \t      Cost\t\t   Time\n----\t----------------\t----------");

                foreach (var result in results)
                {
                    if (result.Item1 == 1 || result.Item1 == results.Count || (result.Item1 % 100 == 0))
                    {
                        Console.WriteLine("{0}\t{1}\t{2}", result.Item1.ToString().PadLeft(4), result.Item2.ToString().PadRight(16), FormatSeconds(result.Item3.TotalSeconds));
                    }

                    file.WriteLine("{0}\t{1}\t{2}", result.Item1.ToString().PadLeft(3), result.Item2.ToString().PadRight(16), FormatSeconds(result.Item3.TotalSeconds));
                }
            }

            Console.WriteLine("\nStochastic Gradient Descent Complete.\n");
        }
Exemplo n.º 2
0
        static void Q1f()
        {
            string dataDirectory = "C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\HW4-q1\\";

            Console.WriteLine("C\tTime Elapsed\t\tError");

            foreach (double c in new double[] { 1, 10, 50, 100, 200, 300, 400, 500 })
            {
                GradientDescent gd = new StochasticGradientDescent(dataDirectory + "features.train.txt", dataDirectory + "target.train.txt", c);

                var results = gd.Iterate();

                var timeElapsed = results[results.Count - 1].Item3;
                var error       = gd.GetError(dataDirectory + "features.test.txt", dataDirectory + "target.test.txt");

                Console.WriteLine("{0}\t{1}\t{2}", gd.C, timeElapsed, error);
            }
        }
Exemplo n.º 3
0
        static void StochasticGradientDescent()
        {
            string dataDirectory = "C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\HW4-q1\\";

            GradientDescent gd = new StochasticGradientDescent(dataDirectory + "features.txt", dataDirectory + "target.txt");

            Stopwatch stopwatch = new Stopwatch();
            var results = gd.Iterate();

            using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\output\\SGD.txt"))
            {
                Console.WriteLine("  k \t      Cost\t\t   Time\n----\t----------------\t----------");
                file.WriteLine("  k \t      Cost\t\t   Time\n----\t----------------\t----------");

                foreach (var result in results)
                {
                    if (result.Item1 == 1 || result.Item1 == results.Count || (result.Item1 % 100 == 0))
                    {
                        Console.WriteLine("{0}\t{1}\t{2}", result.Item1.ToString().PadLeft(4), result.Item2.ToString().PadRight(16), FormatSeconds(result.Item3.TotalSeconds));

                    }

                    file.WriteLine("{0}\t{1}\t{2}", result.Item1.ToString().PadLeft(3), result.Item2.ToString().PadRight(16), FormatSeconds(result.Item3.TotalSeconds));
                }
            }

            Console.WriteLine("\nStochastic Gradient Descent Complete.\n");
        }
Exemplo n.º 4
0
        static void Q1f()
        {
            string dataDirectory = "C:\\Dropbox\\Private\\Stanford\\CS246\\Homeworks\\HW4\\Q1\\HW4-q1\\";

            Console.WriteLine("C\tTime Elapsed\t\tError");

            foreach (double c in new double[] { 1, 10, 50, 100, 200, 300, 400, 500 })
            {
                GradientDescent gd = new StochasticGradientDescent(dataDirectory + "features.train.txt", dataDirectory + "target.train.txt", c);

                var results = gd.Iterate();

                var timeElapsed = results[results.Count - 1].Item3;
                var error = gd.GetError(dataDirectory + "features.test.txt", dataDirectory + "target.test.txt");

                Console.WriteLine("{0}\t{1}\t{2}", gd.C, timeElapsed, error);
            }
        }