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

Function test_softmax

dlib/test/dnn.cpp:118–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116 }
117
118 void test_softmax()
119 {
120 using namespace dlib::tt;
121 print_spinner();
122 const long nr = 3;
123 const long nc = 3;
124 resizable_tensor src(5,5,nr,nr), dest(5,5,nr,nc), gradient_input(5,5,nr,nc);
125 tt::tensor_rand rnd;
126 rnd.fill_uniform(src);
127 rnd.fill_uniform(dest);
128 // fill like this as a test of the assignment operator.
129 gradient_input = matrix_cast<float>(gaussian_randm(5,5*nr*nc, 2));
130
131
132
133 auto grad_src = [&](long idx) {
134 auto f = [&](float eps) {
135 const float old = src.host()[idx];
136 src.host()[idx] += eps;
137 tt::softmax(dest, src);
138 float result = dot(gradient_input, dest);
139 src.host()[idx] = old;
140 return result;
141 };
142 const float eps = 0.01;
143 return (f(+eps)-f(-eps))/(2*eps);
144 };
145
146 resizable_tensor src_grad;
147 src_grad.copy_size(src);
148 src_grad = 0;
149
150 tt::softmax(dest, src);
151 softmax_gradient(src_grad, dest, gradient_input);
152
153 auto grad_error = compare_gradients(src_grad, grad_src);
154 dlog << LINFO << "src error: " << grad_error;
155 DLIB_TEST(grad_error < 0.001);
156
157#ifdef DLIB_USE_CUDA
158 resizable_tensor src1 = src;
159 resizable_tensor src2 = src;
160 resizable_tensor dest1, dest2;
161 dest1.copy_size(src);
162 dest2.copy_size(src);
163 cuda::softmax_all(dest1, src1);
164 cpu::softmax_all(dest2, src2);
165 DLIB_TEST_MSG(max(abs(mat(dest1)-mat(dest2))) < 1e-5, max(abs(mat(dest1)-mat(dest2))));
166#endif
167 }
168
169 void test_softmaxm()
170 {

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