| 883 | } |
| 884 | |
| 885 | void test_elu() |
| 886 | { |
| 887 | IF_DLIB_USE_CUDA( |
| 888 | using namespace dlib::tt; |
| 889 | print_spinner(); |
| 890 | const long n = 4; |
| 891 | const long k = 5; |
| 892 | const long nr = 3; |
| 893 | const long nc = 3; |
| 894 | const float alpha = 1.0f; |
| 895 | resizable_tensor src(n, k, nr, nc); |
| 896 | tt::tensor_rand rnd; |
| 897 | rnd.fill_gaussian(src); |
| 898 | resizable_tensor dest_cuda, dest_cpu; |
| 899 | dest_cuda.copy_size(src); |
| 900 | dest_cpu.copy_size(src); |
| 901 | // initialize to different values in order to make sure the output is actually changed |
| 902 | dest_cuda = 1; |
| 903 | dest_cpu = 2; |
| 904 | cuda::elu(dest_cuda, src, alpha); |
| 905 | cpu::elu(dest_cpu, src, alpha); |
| 906 | auto error = max(abs(mat(dest_cuda) - mat(dest_cpu))); |
| 907 | DLIB_TEST_MSG(error < 1e-7, "error: " << error); |
| 908 | // test gradients |
| 909 | resizable_tensor grad_cuda, grad_cpu, grad_input; |
| 910 | grad_cuda.copy_size(src); |
| 911 | grad_cpu.copy_size(src); |
| 912 | grad_input.copy_size(src); |
| 913 | rnd.fill_gaussian(grad_input); |
| 914 | grad_cuda = 0; |
| 915 | grad_cpu = 0; |
| 916 | cuda::elu_gradient(grad_cuda, dest_cuda, grad_input, alpha); |
| 917 | cpu::elu_gradient(grad_cpu, dest_cpu, grad_input, alpha); |
| 918 | error = max(abs(mat(grad_cuda) - mat(grad_cpu))); |
| 919 | DLIB_TEST_MSG(error < 1e-6, "error: " << error); |
| 920 | ) |
| 921 | } |
| 922 | |
| 923 | void test_gelu() |
| 924 | { |
no test coverage detected