MCPcopy Create free account
hub / github.com/davisking/dlib / test_softmax

Function test_softmax

dlib/test/dnn.cpp:612–661  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

610 }
611
612 void test_softmax()
613 {
614 using namespace dlib::tt;
615 print_spinner();
616 const long nr = 3;
617 const long nc = 3;
618 resizable_tensor src(5,5,nr,nr), dest(5,5,nr,nc), gradient_input(5,5,nr,nc);
619 tt::tensor_rand rnd;
620 rnd.fill_uniform(src);
621 rnd.fill_uniform(dest);
622 // fill like this as a test of the assignment operator.
623 gradient_input = matrix_cast<float>(gaussian_randm(5,5*nr*nc, 2));
624
625
626
627 auto grad_src = [&](long idx) {
628 auto f = [&](float eps) {
629 const float old = src.host()[idx];
630 src.host()[idx] += eps;
631 tt::softmax(dest, src);
632 float result = dot(gradient_input, dest);
633 src.host()[idx] = old;
634 return result;
635 };
636 const float eps = 0.01;
637 return (f(+eps)-f(-eps))/(2*eps);
638 };
639
640 resizable_tensor src_grad;
641 src_grad.copy_size(src);
642 src_grad = 0;
643
644 tt::softmax(dest, src);
645 softmax_gradient(src_grad, dest, gradient_input);
646
647 auto grad_error = compare_gradients(src_grad, grad_src);
648 dlog << LINFO << "src error: " << grad_error;
649 DLIB_TEST(grad_error < 0.001);
650
651 IF_DLIB_USE_CUDA(
652 resizable_tensor src1 = src;
653 resizable_tensor src2 = src;
654 resizable_tensor dest1, dest2;
655 dest1.copy_size(src);
656 dest2.copy_size(src);
657 cuda::softmax_all(dest1, src1);
658 cpu::softmax_all(dest2, src2);
659 DLIB_TEST_MSG(max(abs(mat(dest1)-mat(dest2))) < 1e-5, max(abs(mat(dest1)-mat(dest2))));
660 )
661 }
662
663 void test_softmaxm()
664 {

Callers 1

run_testsMethod · 0.85

Calls 13

print_spinnerFunction · 0.85
gaussian_randmFunction · 0.85
compare_gradientsFunction · 0.85
absFunction · 0.85
copy_sizeMethod · 0.80
fFunction · 0.70
softmaxFunction · 0.50
dotFunction · 0.50
softmax_gradientFunction · 0.50
softmax_allFunction · 0.50
maxFunction · 0.50
matFunction · 0.50

Tested by

no test coverage detected