test_hessian_update_strategy.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import numpy as np
  2. from copy import deepcopy
  3. from numpy.linalg import norm
  4. from numpy.testing import (TestCase, assert_array_almost_equal,
  5. assert_array_equal, assert_array_less)
  6. from scipy.optimize import (BFGS, SR1)
  7. class Rosenbrock:
  8. """Rosenbrock function.
  9. The following optimization problem:
  10. minimize sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
  11. """
  12. def __init__(self, n=2, random_state=0):
  13. rng = np.random.RandomState(random_state)
  14. self.x0 = rng.uniform(-1, 1, n)
  15. self.x_opt = np.ones(n)
  16. def fun(self, x):
  17. x = np.asarray(x)
  18. r = np.sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0,
  19. axis=0)
  20. return r
  21. def grad(self, x):
  22. x = np.asarray(x)
  23. xm = x[1:-1]
  24. xm_m1 = x[:-2]
  25. xm_p1 = x[2:]
  26. der = np.zeros_like(x)
  27. der[1:-1] = (200 * (xm - xm_m1**2) -
  28. 400 * (xm_p1 - xm**2) * xm - 2 * (1 - xm))
  29. der[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0])
  30. der[-1] = 200 * (x[-1] - x[-2]**2)
  31. return der
  32. def hess(self, x):
  33. x = np.atleast_1d(x)
  34. H = np.diag(-400 * x[:-1], 1) - np.diag(400 * x[:-1], -1)
  35. diagonal = np.zeros(len(x), dtype=x.dtype)
  36. diagonal[0] = 1200 * x[0]**2 - 400 * x[1] + 2
  37. diagonal[-1] = 200
  38. diagonal[1:-1] = 202 + 1200 * x[1:-1]**2 - 400 * x[2:]
  39. H = H + np.diag(diagonal)
  40. return H
  41. class TestHessianUpdateStrategy(TestCase):
  42. def test_hessian_initialization(self):
  43. quasi_newton = (BFGS(), SR1())
  44. for qn in quasi_newton:
  45. qn.initialize(5, 'hess')
  46. B = qn.get_matrix()
  47. assert_array_equal(B, np.eye(5))
  48. # For this list of points, it is known
  49. # that no exception occur during the
  50. # Hessian update. Hence no update is
  51. # skiped or damped.
  52. def test_rosenbrock_with_no_exception(self):
  53. # Define auxiliar problem
  54. prob = Rosenbrock(n=5)
  55. # Define iteration points
  56. x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
  57. [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
  58. [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
  59. [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
  60. [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
  61. [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
  62. [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184],
  63. [0.2354930, 0.0443711, 0.0173959, 0.0041872, 0.00794563],
  64. [0.4168118, 0.1433867, 0.0111714, 0.0126265, -0.00658537],
  65. [0.4681972, 0.2153273, 0.0225249, 0.0152704, -0.00463809],
  66. [0.6023068, 0.3346815, 0.0731108, 0.0186618, -0.00371541],
  67. [0.6415743, 0.3985468, 0.1324422, 0.0214160, -0.00062401],
  68. [0.7503690, 0.5447616, 0.2804541, 0.0539851, 0.00242230],
  69. [0.7452626, 0.5644594, 0.3324679, 0.0865153, 0.00454960],
  70. [0.8059782, 0.6586838, 0.4229577, 0.1452990, 0.00976702],
  71. [0.8549542, 0.7226562, 0.4991309, 0.2420093, 0.02772661],
  72. [0.8571332, 0.7285741, 0.5279076, 0.2824549, 0.06030276],
  73. [0.8835633, 0.7727077, 0.5957984, 0.3411303, 0.09652185],
  74. [0.9071558, 0.8299587, 0.6771400, 0.4402896, 0.17469338],
  75. [0.9190793, 0.8486480, 0.7163332, 0.5083780, 0.26107691],
  76. [0.9371223, 0.8762177, 0.7653702, 0.5773109, 0.32181041],
  77. [0.9554613, 0.9119893, 0.8282687, 0.6776178, 0.43162744],
  78. [0.9545744, 0.9099264, 0.8270244, 0.6822220, 0.45237623],
  79. [0.9688112, 0.9351710, 0.8730961, 0.7546601, 0.56622448],
  80. [0.9743227, 0.9491953, 0.9005150, 0.8086497, 0.64505437],
  81. [0.9807345, 0.9638853, 0.9283012, 0.8631675, 0.73812581],
  82. [0.9886746, 0.9777760, 0.9558950, 0.9123417, 0.82726553],
  83. [0.9899096, 0.9803828, 0.9615592, 0.9255600, 0.85822149],
  84. [0.9969510, 0.9935441, 0.9864657, 0.9726775, 0.94358663],
  85. [0.9979533, 0.9960274, 0.9921724, 0.9837415, 0.96626288],
  86. [0.9995981, 0.9989171, 0.9974178, 0.9949954, 0.99023356],
  87. [1.0002640, 1.0005088, 1.0010594, 1.0021161, 1.00386912],
  88. [0.9998903, 0.9998459, 0.9997795, 0.9995484, 0.99916305],
  89. [1.0000008, 0.9999905, 0.9999481, 0.9998903, 0.99978047],
  90. [1.0000004, 0.9999983, 1.0000001, 1.0000031, 1.00000297],
  91. [0.9999995, 1.0000003, 1.0000005, 1.0000001, 1.00000032],
  92. [0.9999999, 0.9999997, 0.9999994, 0.9999989, 0.99999786],
  93. [0.9999999, 0.9999999, 0.9999999, 0.9999999, 0.99999991]]
  94. # Get iteration points
  95. grad_list = [prob.grad(x) for x in x_list]
  96. delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
  97. for i in range(len(x_list)-1)]
  98. delta_grad = [grad_list[i+1]-grad_list[i]
  99. for i in range(len(grad_list)-1)]
  100. # Check curvature condition
  101. for s, y in zip(delta_x, delta_grad):
  102. if np.dot(s, y) <= 0:
  103. raise ArithmeticError()
  104. # Define QuasiNewton update
  105. for quasi_newton in (BFGS(init_scale=1, min_curvature=1e-4),
  106. SR1(init_scale=1)):
  107. hess = deepcopy(quasi_newton)
  108. inv_hess = deepcopy(quasi_newton)
  109. hess.initialize(len(x_list[0]), 'hess')
  110. inv_hess.initialize(len(x_list[0]), 'inv_hess')
  111. # Compare the hessian and its inverse
  112. for s, y in zip(delta_x, delta_grad):
  113. hess.update(s, y)
  114. inv_hess.update(s, y)
  115. B = hess.get_matrix()
  116. H = inv_hess.get_matrix()
  117. assert_array_almost_equal(np.linalg.inv(B), H, decimal=10)
  118. B_true = prob.hess(x_list[len(delta_x)])
  119. assert_array_less(norm(B - B_true)/norm(B_true), 0.1)
  120. def test_SR1_skip_update(self):
  121. # Define auxiliary problem
  122. prob = Rosenbrock(n=5)
  123. # Define iteration points
  124. x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
  125. [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
  126. [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
  127. [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
  128. [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
  129. [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
  130. [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184],
  131. [0.2354930, 0.0443711, 0.0173959, 0.0041872, 0.00794563],
  132. [0.4168118, 0.1433867, 0.0111714, 0.0126265, -0.00658537],
  133. [0.4681972, 0.2153273, 0.0225249, 0.0152704, -0.00463809],
  134. [0.6023068, 0.3346815, 0.0731108, 0.0186618, -0.00371541],
  135. [0.6415743, 0.3985468, 0.1324422, 0.0214160, -0.00062401],
  136. [0.7503690, 0.5447616, 0.2804541, 0.0539851, 0.00242230],
  137. [0.7452626, 0.5644594, 0.3324679, 0.0865153, 0.00454960],
  138. [0.8059782, 0.6586838, 0.4229577, 0.1452990, 0.00976702],
  139. [0.8549542, 0.7226562, 0.4991309, 0.2420093, 0.02772661],
  140. [0.8571332, 0.7285741, 0.5279076, 0.2824549, 0.06030276],
  141. [0.8835633, 0.7727077, 0.5957984, 0.3411303, 0.09652185],
  142. [0.9071558, 0.8299587, 0.6771400, 0.4402896, 0.17469338]]
  143. # Get iteration points
  144. grad_list = [prob.grad(x) for x in x_list]
  145. delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
  146. for i in range(len(x_list)-1)]
  147. delta_grad = [grad_list[i+1]-grad_list[i]
  148. for i in range(len(grad_list)-1)]
  149. hess = SR1(init_scale=1, min_denominator=1e-2)
  150. hess.initialize(len(x_list[0]), 'hess')
  151. # Compare the Hessian and its inverse
  152. for i in range(len(delta_x)-1):
  153. s = delta_x[i]
  154. y = delta_grad[i]
  155. hess.update(s, y)
  156. # Test skip update
  157. B = np.copy(hess.get_matrix())
  158. s = delta_x[17]
  159. y = delta_grad[17]
  160. hess.update(s, y)
  161. B_updated = np.copy(hess.get_matrix())
  162. assert_array_equal(B, B_updated)
  163. def test_BFGS_skip_update(self):
  164. # Define auxiliar problem
  165. prob = Rosenbrock(n=5)
  166. # Define iteration points
  167. x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
  168. [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
  169. [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
  170. [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
  171. [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
  172. [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
  173. [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184]]
  174. # Get iteration points
  175. grad_list = [prob.grad(x) for x in x_list]
  176. delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
  177. for i in range(len(x_list)-1)]
  178. delta_grad = [grad_list[i+1]-grad_list[i]
  179. for i in range(len(grad_list)-1)]
  180. hess = BFGS(init_scale=1, min_curvature=10)
  181. hess.initialize(len(x_list[0]), 'hess')
  182. # Compare the Hessian and its inverse
  183. for i in range(len(delta_x)-1):
  184. s = delta_x[i]
  185. y = delta_grad[i]
  186. hess.update(s, y)
  187. # Test skip update
  188. B = np.copy(hess.get_matrix())
  189. s = delta_x[5]
  190. y = delta_grad[5]
  191. hess.update(s, y)
  192. B_updated = np.copy(hess.get_matrix())
  193. assert_array_equal(B, B_updated)