| 3325 | typename layer_details_type |
| 3326 | > |
| 3327 | layer_test_results impl_test_layer ( |
| 3328 | layer_details_type l, |
| 3329 | const float base_eps |
| 3330 | ) |
| 3331 | { |
| 3332 | using namespace timpl; |
| 3333 | // Do some setup |
| 3334 | running_stats<double> rs_data, rs_params; |
| 3335 | dlib::rand rnd; |
| 3336 | std::ostringstream sout; |
| 3337 | for (int iter = 0; iter < 10; ++iter) |
| 3338 | { |
| 3339 | test_layer_subnet subnetwork(rnd); |
| 3340 | resizable_tensor output, out2, out3; |
| 3341 | // Run setup() and forward() as well to make sure any calls to subnet() have |
| 3342 | // happened before we start assuming we know how many data elements there are |
| 3343 | // (since we do a lazy layer creation thing based on calls to subnet() inside |
| 3344 | // test_layer_subnet). |
| 3345 | l.setup(subnetwork); |
| 3346 | impl::call_layer_forward(l, subnetwork, output); |
| 3347 | |
| 3348 | resizable_tensor input_grad; |
| 3349 | input_grad.copy_size(output); |
| 3350 | fill_with_gassuan_random_numbers(input_grad, rnd); |
| 3351 | |
| 3352 | |
| 3353 | // The f() we are computing gradients of is this thing. It's value at the current |
| 3354 | // parameter and data values is: |
| 3355 | //sout << "f(data,params): " << dot(output, input_grad) << std::endl; |
| 3356 | |
| 3357 | // We are going to save a copy of the subnetwork.get_gradient_input() data before we do |
| 3358 | // backpropagation since the backward() function is supposed to *add* to the |
| 3359 | // gradients rather than overwrite them. We will use this saved data to check if |
| 3360 | // that is the case. |
| 3361 | const unsigned long num_data_inputs = subnetwork.count_outputs(); |
| 3362 | std::vector<float> initial_gradient_input(num_data_inputs); |
| 3363 | for (unsigned long i = 0; i < num_data_inputs; ++i) |
| 3364 | initial_gradient_input[i] = subnetwork.get_gradient_input_element(i); |
| 3365 | |
| 3366 | |
| 3367 | // Now tell the layer to compute all the gradients. In the rest of this function |
| 3368 | // we will just be checking that these gradients were computed correctly by |
| 3369 | // comparing them to a central differences approximation. |
| 3370 | resizable_tensor params_grad; |
| 3371 | params_grad.copy_size(l.get_layer_params()); |
| 3372 | // But first, set the params grad to something crazy so that it's very obvious if |
| 3373 | // it doesn't get fully assigned. |
| 3374 | params_grad = std::numeric_limits<float>::infinity(); |
| 3375 | impl::call_layer_backward(l, output, input_grad, subnetwork, params_grad); |
| 3376 | |
| 3377 | static_assert(impl::is_inplace_layer(l, subnetwork) == impl::has_inplace_backward(l, subnetwork), |
| 3378 | "Layer not defined correctly. forward and backward methods must either both be in-place or both out-of-place. "); |
| 3379 | |
| 3380 | // Make sure the outputs of forward() and backward() are the same when they are run |
| 3381 | // in in-place mode. |
| 3382 | if (impl::is_inplace_layer(l, subnetwork)) |
| 3383 | { |
| 3384 | test_layer_subnet subnetwork2(rnd); |