| 5079 | // ---------------------------------------------------------------------------------------- |
| 5080 | |
| 5081 | void test_loss_multibinary_log() |
| 5082 | { |
| 5083 | print_spinner(); |
| 5084 | dlib::rand rnd; |
| 5085 | |
| 5086 | const long dims = 3; |
| 5087 | const std::vector<float> empty_label(2, -1.f); |
| 5088 | std::vector<matrix<float, 0, 1>> samples; |
| 5089 | std::vector<std::vector<float>> labels(128, empty_label); |
| 5090 | |
| 5091 | for (size_t i = 0; i < labels.size(); ++i) |
| 5092 | { |
| 5093 | const double class_boundary_1 = 3.0; |
| 5094 | const double class_boundary_2 = 6.0; |
| 5095 | |
| 5096 | const double desired_margin = 0.1; |
| 5097 | |
| 5098 | const auto get_random_matrix = [&rnd, dims]() |
| 5099 | { |
| 5100 | return matrix<float, 0, 1>(matrix_cast<float>(randm(dims, 1)) * rnd.get_double_in_range(1, 9)); |
| 5101 | }; |
| 5102 | |
| 5103 | const auto get_distance_from_nearest_class_boundary = [class_boundary_1, class_boundary_2](double norm) |
| 5104 | { |
| 5105 | return std::min( |
| 5106 | std::abs(norm - class_boundary_1), |
| 5107 | std::abs(norm - class_boundary_2) |
| 5108 | ); |
| 5109 | }; |
| 5110 | |
| 5111 | auto x = get_random_matrix(); |
| 5112 | auto norm = sqrt(sum(squared(x))); |
| 5113 | auto distance_from_nearest_class_boundary = get_distance_from_nearest_class_boundary(norm); |
| 5114 | |
| 5115 | // Try again if the newly generated sample is very close to either of the class boundaries |
| 5116 | int retry_counter = 0; |
| 5117 | const int max_retry_counter = 10; |
| 5118 | while (distance_from_nearest_class_boundary < desired_margin && ++retry_counter <= max_retry_counter) |
| 5119 | { |
| 5120 | const auto new_x = get_random_matrix(); |
| 5121 | const auto new_norm = sqrt(sum(squared(new_x))); |
| 5122 | const auto new_distance_from_nearest_class_boundary = get_distance_from_nearest_class_boundary(new_norm); |
| 5123 | |
| 5124 | if (new_distance_from_nearest_class_boundary > distance_from_nearest_class_boundary) |
| 5125 | { |
| 5126 | x = new_x; |
| 5127 | norm = new_norm; |
| 5128 | distance_from_nearest_class_boundary = new_distance_from_nearest_class_boundary; |
| 5129 | } |
| 5130 | } |
| 5131 | |
| 5132 | if (norm < class_boundary_1) |
| 5133 | { |
| 5134 | labels[i][0] = 1.f; |
| 5135 | } |
| 5136 | else if (class_boundary_1 <= norm && norm < class_boundary_2) |
| 5137 | { |
| 5138 | labels[i][0] = 1.f; |
no test coverage detected