| 349 | } |
| 350 | |
| 351 | void test_clipped_relu() |
| 352 | { |
| 353 | #ifdef DLIB_USE_CUDA |
| 354 | using namespace dlib::tt; |
| 355 | print_spinner(); |
| 356 | const long n = 4; |
| 357 | const long k = 5; |
| 358 | const long nr = 3; |
| 359 | const long nc = 3; |
| 360 | const float ceiling = 6.0f; |
| 361 | resizable_tensor src(n, k, nr, nc); |
| 362 | tt::tensor_rand rnd; |
| 363 | rnd.fill_gaussian(src, 0, 3); |
| 364 | resizable_tensor dest_cuda, dest_cpu; |
| 365 | dest_cuda.copy_size(src); |
| 366 | dest_cpu.copy_size(src); |
| 367 | // initialize to different values in order to make sure the output is actually changed |
| 368 | dest_cuda = 1; |
| 369 | dest_cpu = 2; |
| 370 | cuda::clipped_relu(dest_cuda, src, ceiling); |
| 371 | cpu::clipped_relu(dest_cpu, src, ceiling); |
| 372 | auto error = max(abs(mat(dest_cuda) - mat(dest_cpu))); |
| 373 | DLIB_TEST_MSG(error < 1e-7, "error: " << error); |
| 374 | |
| 375 | // test gradients |
| 376 | resizable_tensor grad_cuda, grad_cpu, grad_input; |
| 377 | grad_cuda.copy_size(src); |
| 378 | grad_cpu.copy_size(src); |
| 379 | grad_input.copy_size(src); |
| 380 | rnd.fill_uniform(grad_input); |
| 381 | grad_cuda = 0; |
| 382 | grad_cpu = 0; |
| 383 | cuda::clipped_relu_gradient(grad_cuda, dest_cuda, grad_input, ceiling); |
| 384 | cpu::clipped_relu_gradient(grad_cpu, dest_cpu, grad_input, ceiling); |
| 385 | error = max(abs(mat(grad_cuda) - mat(grad_cpu))); |
| 386 | DLIB_TEST_MSG(error < 1e-7, "error: " << error); |
| 387 | #endif // DLIB_USE_CUDA |
| 388 | } |
| 389 | |
| 390 | void test_elu() |
| 391 | { |
no test coverage detected