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

Function test_softmax_all

dlib/test/dnn.cpp:248–297  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

246 }
247
248 void test_softmax_all()
249 {
250 using namespace dlib::tt;
251 print_spinner();
252 const long nr = 3;
253 const long nc = 3;
254 resizable_tensor src(5,5,nr,nc), dest(5,5,nr,nc), gradient_input(5,5,nr,nc);
255 tt::tensor_rand rnd;
256 rnd.fill_uniform(src);
257 rnd.fill_uniform(dest);
258 // fill like this as a test of the assignment operator.
259 gradient_input = matrix_cast<float>(gaussian_randm(5,5*nr*nc, 2));
260
261
262
263 auto grad_src = [&](long idx) {
264 auto f = [&](float eps) {
265 const float old = src.host()[idx];
266 src.host()[idx] += eps;
267 tt::softmax_all(dest, src);
268 float result = dot(gradient_input, dest);
269 src.host()[idx] = old;
270 return result;
271 };
272 const float eps = 0.01;
273 return (f(+eps)-f(-eps))/(2*eps);
274 };
275
276 resizable_tensor src_grad;
277 src_grad.copy_size(src);
278 src_grad = 0;
279
280 tt::softmax_all(dest, src);
281 softmax_all_gradient(src_grad, dest, gradient_input);
282
283 auto grad_error = compare_gradients(src_grad, grad_src);
284 dlog << LINFO << "src error: " << grad_error;
285 DLIB_TEST(grad_error < 0.001);
286
287#ifdef DLIB_USE_CUDA
288 resizable_tensor src1 = src;
289 resizable_tensor src2 = src;
290 resizable_tensor dest1, dest2;
291 dest1.copy_size(src);
292 dest2.copy_size(src);
293 cuda::softmax_all(dest1, src1);
294 cpu::softmax_all(dest2, src2);
295 DLIB_TEST_MSG(max(abs(mat(dest1)-mat(dest2))) < 1e-5, max(abs(mat(dest1)-mat(dest2))));
296#endif
297 }
298
299 void test_mish()
300 {

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