| 538 | // ---------------------------------------------------------------------------------------- |
| 539 | |
| 540 | void test_tanh() |
| 541 | { |
| 542 | using namespace dlib::tt; |
| 543 | print_spinner(); |
| 544 | resizable_tensor src, dest, gradient_input; |
| 545 | src = matrix_cast<float>(gaussian_randm(5,5, 0)); |
| 546 | dest = matrix_cast<float>(gaussian_randm(5,5, 1)); |
| 547 | gradient_input = matrix_cast<float>(gaussian_randm(5,5, 2)); |
| 548 | |
| 549 | |
| 550 | |
| 551 | auto grad_src = [&](long idx) { |
| 552 | auto f = [&](float eps) { |
| 553 | const float old = src.host()[idx]; |
| 554 | src.host()[idx] += eps; |
| 555 | tanh(dest, src); |
| 556 | float result = dot(gradient_input, dest); |
| 557 | src.host()[idx] = old; |
| 558 | return result; |
| 559 | }; |
| 560 | const float eps = 0.01; |
| 561 | return (f(+eps)-f(-eps))/(2*eps); |
| 562 | }; |
| 563 | |
| 564 | resizable_tensor src_grad; |
| 565 | src_grad.copy_size(src); |
| 566 | src_grad = 0; |
| 567 | |
| 568 | tanh(dest, src); |
| 569 | tanh_gradient(src_grad, dest, gradient_input); |
| 570 | |
| 571 | auto grad_error = compare_gradients(src_grad, grad_src); |
| 572 | dlog << LINFO << "src error: " << grad_error; |
| 573 | DLIB_TEST(grad_error < 0.001); |
| 574 | } |
| 575 | |
| 576 | void test_sigmoid() |
| 577 | { |
no test coverage detected