| 44 | // ---------------------------------------------------------------------------------------- |
| 45 | |
| 46 | void test_tanh() |
| 47 | { |
| 48 | using namespace dlib::tt; |
| 49 | print_spinner(); |
| 50 | resizable_tensor src, dest, gradient_input; |
| 51 | src = matrix_cast<float>(gaussian_randm(5,5, 0)); |
| 52 | dest = matrix_cast<float>(gaussian_randm(5,5, 1)); |
| 53 | gradient_input = matrix_cast<float>(gaussian_randm(5,5, 2)); |
| 54 | |
| 55 | |
| 56 | |
| 57 | auto grad_src = [&](long idx) { |
| 58 | auto f = [&](float eps) { |
| 59 | const float old = src.host()[idx]; |
| 60 | src.host()[idx] += eps; |
| 61 | tanh(dest, src); |
| 62 | float result = dot(gradient_input, dest); |
| 63 | src.host()[idx] = old; |
| 64 | return result; |
| 65 | }; |
| 66 | const float eps = 0.01; |
| 67 | return (f(+eps)-f(-eps))/(2*eps); |
| 68 | }; |
| 69 | |
| 70 | resizable_tensor src_grad; |
| 71 | src_grad.copy_size(src); |
| 72 | src_grad = 0; |
| 73 | |
| 74 | tanh(dest, src); |
| 75 | tanh_gradient(src_grad, dest, gradient_input); |
| 76 | |
| 77 | auto grad_error = compare_gradients(src_grad, grad_src); |
| 78 | dlog << LINFO << "src error: " << grad_error; |
| 79 | DLIB_TEST(grad_error < 0.001); |
| 80 | } |
| 81 | |
| 82 | void test_sigmoid() |
| 83 | { |
no test coverage detected