test_logsumexp.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import (assert_almost_equal, assert_equal, assert_allclose,
  4. assert_array_almost_equal, assert_)
  5. from scipy.special import logsumexp, softmax
  6. def test_logsumexp():
  7. # Test whether logsumexp() function correctly handles large inputs.
  8. a = np.arange(200)
  9. desired = np.log(np.sum(np.exp(a)))
  10. assert_almost_equal(logsumexp(a), desired)
  11. # Now test with large numbers
  12. b = [1000, 1000]
  13. desired = 1000.0 + np.log(2.0)
  14. assert_almost_equal(logsumexp(b), desired)
  15. n = 1000
  16. b = np.full(n, 10000, dtype='float64')
  17. desired = 10000.0 + np.log(n)
  18. assert_almost_equal(logsumexp(b), desired)
  19. x = np.array([1e-40] * 1000000)
  20. logx = np.log(x)
  21. X = np.vstack([x, x])
  22. logX = np.vstack([logx, logx])
  23. assert_array_almost_equal(np.exp(logsumexp(logX)), X.sum())
  24. assert_array_almost_equal(np.exp(logsumexp(logX, axis=0)), X.sum(axis=0))
  25. assert_array_almost_equal(np.exp(logsumexp(logX, axis=1)), X.sum(axis=1))
  26. # Handling special values properly
  27. assert_equal(logsumexp(np.inf), np.inf)
  28. assert_equal(logsumexp(-np.inf), -np.inf)
  29. assert_equal(logsumexp(np.nan), np.nan)
  30. assert_equal(logsumexp([-np.inf, -np.inf]), -np.inf)
  31. # Handling an array with different magnitudes on the axes
  32. assert_array_almost_equal(logsumexp([[1e10, 1e-10],
  33. [-1e10, -np.inf]], axis=-1),
  34. [1e10, -1e10])
  35. # Test keeping dimensions
  36. assert_array_almost_equal(logsumexp([[1e10, 1e-10],
  37. [-1e10, -np.inf]],
  38. axis=-1,
  39. keepdims=True),
  40. [[1e10], [-1e10]])
  41. # Test multiple axes
  42. assert_array_almost_equal(logsumexp([[1e10, 1e-10],
  43. [-1e10, -np.inf]],
  44. axis=(-1,-2)),
  45. 1e10)
  46. def test_logsumexp_b():
  47. a = np.arange(200)
  48. b = np.arange(200, 0, -1)
  49. desired = np.log(np.sum(b*np.exp(a)))
  50. assert_almost_equal(logsumexp(a, b=b), desired)
  51. a = [1000, 1000]
  52. b = [1.2, 1.2]
  53. desired = 1000 + np.log(2 * 1.2)
  54. assert_almost_equal(logsumexp(a, b=b), desired)
  55. x = np.array([1e-40] * 100000)
  56. b = np.linspace(1, 1000, 100000)
  57. logx = np.log(x)
  58. X = np.vstack((x, x))
  59. logX = np.vstack((logx, logx))
  60. B = np.vstack((b, b))
  61. assert_array_almost_equal(np.exp(logsumexp(logX, b=B)), (B * X).sum())
  62. assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=0)),
  63. (B * X).sum(axis=0))
  64. assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=1)),
  65. (B * X).sum(axis=1))
  66. def test_logsumexp_sign():
  67. a = [1,1,1]
  68. b = [1,-1,-1]
  69. r, s = logsumexp(a, b=b, return_sign=True)
  70. assert_almost_equal(r,1)
  71. assert_equal(s,-1)
  72. def test_logsumexp_sign_zero():
  73. a = [1,1]
  74. b = [1,-1]
  75. r, s = logsumexp(a, b=b, return_sign=True)
  76. assert_(not np.isfinite(r))
  77. assert_(not np.isnan(r))
  78. assert_(r < 0)
  79. assert_equal(s,0)
  80. def test_logsumexp_sign_shape():
  81. a = np.ones((1,2,3,4))
  82. b = np.ones_like(a)
  83. r, s = logsumexp(a, axis=2, b=b, return_sign=True)
  84. assert_equal(r.shape, s.shape)
  85. assert_equal(r.shape, (1,2,4))
  86. r, s = logsumexp(a, axis=(1,3), b=b, return_sign=True)
  87. assert_equal(r.shape, s.shape)
  88. assert_equal(r.shape, (1,3))
  89. def test_logsumexp_shape():
  90. a = np.ones((1, 2, 3, 4))
  91. b = np.ones_like(a)
  92. r = logsumexp(a, axis=2, b=b)
  93. assert_equal(r.shape, (1, 2, 4))
  94. r = logsumexp(a, axis=(1, 3), b=b)
  95. assert_equal(r.shape, (1, 3))
  96. def test_logsumexp_b_zero():
  97. a = [1,10000]
  98. b = [1,0]
  99. assert_almost_equal(logsumexp(a, b=b), 1)
  100. def test_logsumexp_b_shape():
  101. a = np.zeros((4,1,2,1))
  102. b = np.ones((3,1,5))
  103. logsumexp(a, b=b)
  104. def test_softmax_fixtures():
  105. assert_allclose(softmax([1000, 0, 0, 0]), np.array([1, 0, 0, 0]),
  106. rtol=1e-13)
  107. assert_allclose(softmax([1, 1]), np.array([.5, .5]), rtol=1e-13)
  108. assert_allclose(softmax([0, 1]), np.array([1, np.e])/(1 + np.e),
  109. rtol=1e-13)
  110. # Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
  111. # converted to float.
  112. x = np.arange(4)
  113. expected = np.array([0.03205860328008499,
  114. 0.08714431874203256,
  115. 0.23688281808991013,
  116. 0.6439142598879722])
  117. assert_allclose(softmax(x), expected, rtol=1e-13)
  118. # Translation property. If all the values are changed by the same amount,
  119. # the softmax result does not change.
  120. assert_allclose(softmax(x + 100), expected, rtol=1e-13)
  121. # When axis=None, softmax operates on the entire array, and preserves
  122. # the shape.
  123. assert_allclose(softmax(x.reshape(2, 2)), expected.reshape(2, 2),
  124. rtol=1e-13)
  125. def test_softmax_multi_axes():
  126. assert_allclose(softmax([[1000, 0], [1000, 0]], axis=0),
  127. np.array([[.5, .5], [.5, .5]]), rtol=1e-13)
  128. assert_allclose(softmax([[1000, 0], [1000, 0]], axis=1),
  129. np.array([[1, 0], [1, 0]]), rtol=1e-13)
  130. # Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
  131. # converted to float.
  132. x = np.array([[-25, 0, 25, 50],
  133. [1, 325, 749, 750]])
  134. expected = np.array([[2.678636961770877e-33,
  135. 1.9287498479371314e-22,
  136. 1.3887943864771144e-11,
  137. 0.999999999986112],
  138. [0.0,
  139. 1.9444526359919372e-185,
  140. 0.2689414213699951,
  141. 0.7310585786300048]])
  142. assert_allclose(softmax(x, axis=1), expected, rtol=1e-13)
  143. assert_allclose(softmax(x.T, axis=0), expected.T, rtol=1e-13)
  144. # 3-d input, with a tuple for the axis.
  145. x3d = x.reshape(2, 2, 2)
  146. assert_allclose(softmax(x3d, axis=(1, 2)), expected.reshape(2, 2, 2),
  147. rtol=1e-13)