Exemple #1
0
        public static void test_matrix_to_csv_and_back()
        {
            var result    = new result_state();
            var x         = ml_funcs.matrix_create(2, 3);
            var file_name = "test_matrix_to_csv_and_back.txt";

            double[][] y;

            x[0] = new double[] { 1.123, 2.12, 3.123 };
            x[1] = new double[] { 4.123, 5.123, 6.123 };

            result = ml_funcs.matrix_to_csv(file_name, x);

            Console.Write("matrix_to_csv_and_back");
            if (result.has_errors())
            {
                Console.WriteLine(" .. FAILED");
                Console.WriteLine(result.all_errors_to_string());
                return;
            }

            result = ml_funcs.matrix_from_csv(file_name, out y);

            if (result.has_errors())
            {
                Console.WriteLine(" .. FAILED");
                Console.WriteLine(result.all_errors_to_string());
                return;
            }

            if (ml_funcs.matrix_compare_deep(x, y))
            {
                Console.WriteLine(" .. OK");
            }
            else
            {
                Console.WriteLine(" .. FAILED");
            }

            File.Delete(file_name);
        }
Exemple #2
0
        public static void test_nn()
        {
            Console.Write("two_layer_nn ");
            double    lambda            = 0;
            const int input_layer_size  = 400;
            const int hidden_layer_size = 25;
            const int output_layer_size = 10;
            int       training_examples = 0;

            double cost;

            double[]   result_data, unrolled_theta, trained_theta, cost_list;           // y
            double[][] train_data, theta_1, theta_2;                                    // X - train_data
            double[][] theta1_gradient, theta2_gradient;

            result_state result;

            // Loading X and Y
            {
                string X_file_content;
                result = file_utils.read_file("./data/ex4_data_X.txt", out X_file_content);
                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }

                train_data = string_utils.string_to_matrix(X_file_content, " ");
                if (train_data.Length != 5000 || train_data[0].Length != 400)
                {
                    Console.WriteLine(" .. FAILED. Should have 5000 training examples and 400 features");
                    return;
                }

                training_examples = train_data.Length;
                result_data       = new double[training_examples];
                int y = 0, label = 10;
                for (var i = 0; i < training_examples; i++)
                {
                    result_data[i] = label;
                    if (++y == 500)
                    {
                        if (label == 10)
                        {
                            label = 1;
                        }
                        else
                        {
                            label++;
                        }
                        y = 0;
                    }
                }
            }

            // Load weights
            {
                string file_content_1, file_content_2;
                result = file_utils.read_file("./data/ex4_theta_1.txt", out file_content_1);
                result.combine_errors(utils.file_utils.read_file("./data/ex4_theta_2.txt", out file_content_2));

                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }

                theta_1 = string_utils.string_to_matrix(file_content_1, " ");
                theta_2 = string_utils.string_to_matrix(file_content_2, " ");

                unrolled_theta = matrix_flatten_two(theta_1, theta_2, flatten_direction.by_column);

                if (unrolled_theta.Length != 10285)
                {
                    Console.WriteLine(".. FAILED. Incorrect unrolled theta parameter count");
                    return;
                }
            }

            // testing cost with initial thetas
            if (1 == 0)
            {
                lambda = 0;
                result = nn_cost_two_layer(train_data, result_data, matrix_transpose(theta_1), matrix_transpose(theta_2), output_layer_size, lambda, out cost, out theta1_gradient, out theta2_gradient);
                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }

                if (Math.Round(cost, 6) != 0.287629)
                {
                    Console.WriteLine(".. FAILED. Cost with regularization off (labda 0) is incorrect");
                    return;
                }

                lambda = 1;
                result = nn_cost_two_layer(train_data, result_data, matrix_transpose(theta_1), matrix_transpose(theta_2), output_layer_size, lambda, out cost, out theta1_gradient, out theta2_gradient);
                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }

                if (Math.Round(cost, 6) != 0.383770)
                {
                    Console.WriteLine(".. FAILED. Cost with regularization off (labda 1) is incorrect");
                    return;
                }
            }

            // training nn
            {
                theta_1 = nn_random_weights(input_layer_size + 1, hidden_layer_size);
                theta_2 = nn_random_weights(hidden_layer_size + 1, output_layer_size);

                lambda = 1;

                result = nn_cost_two_layer(train_data, result_data, matrix_transpose(theta_1), matrix_transpose(theta_2), output_layer_size, lambda, out cost, out theta1_gradient, out theta2_gradient);

                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }

                cost_delegate nn_cost_delegate = (double[][] train_data, double[] result_data, double[] theta, double lambda, out double cost, out double[] gradient) => {
                    var result = new result_state();
                    cost     = 0;
                    gradient = null;

                    // 1. convert theta back to neural network layers
                    var theta1 = matrix_unflatten(theta, hidden_layer_size, 0, (input_layer_size + 1) * hidden_layer_size - 1);
                    var theta2 = matrix_unflatten(theta, output_layer_size, (input_layer_size + 1) * hidden_layer_size);

                    // 2. pass to nn_cost_two_layer neural network thetas
                    result = nn_cost_two_layer(train_data, result_data, theta1, theta2, output_layer_size, lambda, out cost, out theta1_gradient, out theta2_gradient);

                    Console.WriteLine($"Cost: {cost}");

                    if (result.has_errors())
                    {
                        Console.WriteLine(result.all_errors_to_string());
                        return(result);
                    }

                    // 3. we get theta1, theta2 gradients then we flattan them into gradient
                    gradient = matrix_flatten_two(theta1_gradient, theta2_gradient);

                    return(result);
                };

                result = rasmussen(train_data, result_data, unrolled_theta, lambda, max_iterations: 3, nn_cost_delegate, out cost_list, out trained_theta);
            }

            Console.WriteLine(".. OK");
        }
Exemple #3
0
        public static void test_rasmussen(bool force_training = false)
        {
            var          result = new result_state();
            const string trained_theta_file_path    = "./data/ex3_trained_theta.txt";
            const string cost_progression_file_path = "./data/ex3_cost_progression.txt";
            const double lambda         = 0.1d;
            const int    max_iterations = 10;
            var          file_path      = "./data/ex3data1.txt";

            double[][] train_data, trained_theta, cost_progression;
            double[]   result_data, label_result_data, initial_theta;
            int        i, labels, label_train_count;

            int[] predict_indices;

            var parse_result = file_utils.parse_file(file_path, out train_data, out result_data);

            if (parse_result.has_errors())
            {
                Console.WriteLine(parse_result.all_errors_to_string());
                return;
            }

            label_train_count = result_data.Length / 10;             // how many training examples we have for each label (numbers from 0 to 9)

            trained_theta    = ml_funcs.matrix_create(10, train_data[0].Length);
            cost_progression = ml_funcs.matrix_create(10, 10);

            // don't retrain
            // delete file if you want to retrain
            if (!File.Exists(trained_theta_file_path) || force_training)
            {
                for (labels = 0; labels < 10; labels++)
                {
                    Console.WriteLine($"Training label: {labels}");
                    label_result_data = new double[train_data.Length];
                    for (i = 0; i < label_train_count; i++)
                    {
                        label_result_data[label_train_count * labels + i] = 1;
                    }

                    initial_theta = new double[train_data[0].Length + 1];
                    result        = ml_funcs.rasmussen(train_data, label_result_data, initial_theta, lambda, max_iterations, ml_funcs.cost_logistic_regression_regularized, out cost_progression[labels], out trained_theta[labels]);

                    if (result.has_errors())
                    {
                        Console.WriteLine(result.all_errors_to_string());
                        return;
                    }
                }

                result = ml.ml_funcs.matrix_to_csv(trained_theta_file_path, trained_theta);
                var write_vector_result = ml_funcs.matrix_to_csv(cost_progression_file_path, cost_progression);
                result.combine_errors(write_vector_result);

                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }
            }
            else
            {
                result = ml_funcs.matrix_from_csv(trained_theta_file_path, out trained_theta);

                if (result.has_errors())
                {
                    Console.WriteLine(result.all_errors_to_string());
                    return;
                }
            }

            result = ml_funcs.predict_one_vs_all(trained_theta, train_data, out predict_indices);

            if (result.has_errors())
            {
                Console.WriteLine(result.all_errors_to_string());
                return;
            }

            var correct_predictions = 0;

            for (i = 0; i < result_data.Length; i++)
            {
                if (result_data[i] == predict_indices[i])
                {
                    correct_predictions++;
                }
            }

            double training_accuracy = (double)correct_predictions / (double)result_data.Length * 100;

            Console.Write("rasmussen");
            if (training_accuracy > 90d)
            {
                Console.WriteLine(" .. OK");
            }
            else
            {
                Console.WriteLine(" .. FAILED");
            }

            // Console.WriteLine($"Training accuracy with {max_iterations} iterations is {training_accuracy}%");
        }