| 661 | } |
| 662 | |
| 663 | void test_softmaxm() |
| 664 | { |
| 665 | print_spinner(); |
| 666 | using net_type = tag1<softmaxm<tag2<input<matrix<float>>>>>; |
| 667 | net_type net; |
| 668 | |
| 669 | // Initialization |
| 670 | dlib::rand rnd(std::rand()); |
| 671 | const long nr = 2, nc = 3; |
| 672 | const int n_samples = 3, k = 1; |
| 673 | std::vector<matrix<float>> x(n_samples); |
| 674 | matrix<float> xtmp(nr, nc); |
| 675 | for (int ii = 0; ii < n_samples; ++ii) { |
| 676 | for (int jj = 0; jj < nr; ++jj) |
| 677 | for (int kk = 0; kk < nc; ++kk) { |
| 678 | float r = rnd.get_random_gaussian(); |
| 679 | if (r > 1 || r < -1) r = -std::numeric_limits<float>::infinity(); |
| 680 | xtmp(jj, kk) = r; |
| 681 | } |
| 682 | x[ii] = xtmp; |
| 683 | } |
| 684 | |
| 685 | // Convert input matrix to tensor |
| 686 | resizable_tensor input_tensor; |
| 687 | net.to_tensor(&x[0], &x[0] + n_samples, input_tensor); |
| 688 | net.forward(input_tensor); |
| 689 | |
| 690 | // Expected output tensor |
| 691 | resizable_tensor expected_output; |
| 692 | expected_output.copy_size(input_tensor); |
| 693 | for (int ii = 0; ii < n_samples; ++ii) { |
| 694 | for (int jj = 0; jj < nr; ++jj) { |
| 695 | matrix<float> m(1, nc); |
| 696 | bool all_neg_inf = true; |
| 697 | for (int kk = 0; kk < nc; ++kk) { |
| 698 | m(0, kk) = input_tensor.host()[tensor_index(input_tensor, ii, 0, jj, kk)]; |
| 699 | if (m(0, kk) > -std::numeric_limits<float>::infinity()) all_neg_inf = false; |
| 700 | } |
| 701 | |
| 702 | matrix<float> r(1, nc); |
| 703 | if (all_neg_inf) |
| 704 | for (int kk = 0; kk < nc; ++kk) r(0, kk) = 0.0f; |
| 705 | else { |
| 706 | // Stabilize the computation by subtracting the max value |
| 707 | float max_val = max(m); |
| 708 | matrix<float> exp_m = exp(m - max_val); |
| 709 | float sum_exp = sum(exp_m) + std::numeric_limits<float>::epsilon(); |
| 710 | r = exp_m / sum_exp; |
| 711 | } |
| 712 | for (int kk = 0; kk < nc; ++kk) |
| 713 | expected_output.host()[tensor_index(expected_output, ii, 0, jj, kk)] = r(0, kk); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // Compare output tensor with expected output |
| 718 | auto& net_output = layer<tag1>(net).get_output(); |
| 719 | DLIB_TEST(max(abs(mat(net_output) - mat(expected_output))) < 1e-5); |
| 720 |
no test coverage detected