test_sparse_costs.py
Go to the documentation of this file.
1 from __future__ import print_function, division
2 import numpy as np
3 import pyexotica as exo
4 
5 NUM_TESTS = 1000
6 
7 
8 def num_diff_1d(method, x, arg):
9  eps = 1e-6
10  x_plus = method(x + eps/2.0, arg)
11  x_minus = method(x - eps/2.0, arg)
12  dx = (x_plus - x_minus) / eps
13  return dx
14 
15 
16 def check_derivative_1d(f, df, arg):
17  for _ in range(NUM_TESTS):
18  x = np.random.random((1,))
19  dx_analytic = df(x, arg)
20  dx_numdiff = num_diff_1d(f, x, arg)
21  # that's a huge error margin!
22  np.testing.assert_allclose(dx_analytic, dx_numdiff, rtol=1e-3)
23 
24 
25 def test_huber():
26  for delta in [0.001, 0.01, 0.1, 1.0, 10.0]:
27  check_derivative_1d(exo.Tools.SparseCosts.huber_cost,
28  exo.Tools.SparseCosts.huber_jacobian, delta)
29  check_derivative_1d(exo.Tools.SparseCosts.huber_jacobian,
30  exo.Tools.SparseCosts.huber_hessian, delta)
31 
32 
34  for alpha in [1.0, 10.0]:
35  check_derivative_1d(exo.Tools.SparseCosts.smooth_l1_cost,
36  exo.Tools.SparseCosts.smooth_l1_jacobian, alpha)
37  check_derivative_1d(exo.Tools.SparseCosts.smooth_l1_jacobian,
38  exo.Tools.SparseCosts.smooth_l1_hessian, alpha)
39 
40 
41 if __name__ == "__main__":
42  test_huber()
def check_derivative_1d(f, df, arg)
def num_diff_1d(method, x, arg)


exotica_python
Author(s):
autogenerated on Sat Apr 10 2021 02:35:59