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

Function test_softmax_all

dlib/test/dnn.cpp:743–792  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

741 }
742
743 void test_softmax_all()
744 {
745 using namespace dlib::tt;
746 print_spinner();
747 const long nr = 3;
748 const long nc = 3;
749 resizable_tensor src(5,5,nr,nc), dest(5,5,nr,nc), gradient_input(5,5,nr,nc);
750 tt::tensor_rand rnd;
751 rnd.fill_uniform(src);
752 rnd.fill_uniform(dest);
753 // fill like this as a test of the assignment operator.
754 gradient_input = matrix_cast<float>(gaussian_randm(5,5*nr*nc, 2));
755
756
757
758 auto grad_src = [&](long idx) {
759 auto f = [&](float eps) {
760 const float old = src.host()[idx];
761 src.host()[idx] += eps;
762 tt::softmax_all(dest, src);
763 float result = dot(gradient_input, dest);
764 src.host()[idx] = old;
765 return result;
766 };
767 const float eps = 0.01;
768 return (f(+eps)-f(-eps))/(2*eps);
769 };
770
771 resizable_tensor src_grad;
772 src_grad.copy_size(src);
773 src_grad = 0;
774
775 tt::softmax_all(dest, src);
776 softmax_all_gradient(src_grad, dest, gradient_input);
777
778 auto grad_error = compare_gradients(src_grad, grad_src);
779 dlog << LINFO << "src error: " << grad_error;
780 DLIB_TEST(grad_error < 0.001);
781
782 IF_DLIB_USE_CUDA(
783 resizable_tensor src1 = src;
784 resizable_tensor src2 = src;
785 resizable_tensor dest1, dest2;
786 dest1.copy_size(src);
787 dest2.copy_size(src);
788 cuda::softmax_all(dest1, src1);
789 cpu::softmax_all(dest2, src2);
790 DLIB_TEST_MSG(max(abs(mat(dest1)-mat(dest2))) < 1e-5, max(abs(mat(dest1)-mat(dest2))));
791 )
792 }
793
794 void test_mish()
795 {

Callers 1

run_testsMethod · 0.85

Calls 12

print_spinnerFunction · 0.85
gaussian_randmFunction · 0.85
compare_gradientsFunction · 0.85
absFunction · 0.85
copy_sizeMethod · 0.80
fFunction · 0.70
softmax_allFunction · 0.50
dotFunction · 0.50
softmax_all_gradientFunction · 0.50
maxFunction · 0.50
matFunction · 0.50
hostMethod · 0.45

Tested by

no test coverage detected