utils_numpy.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import numpy as np
  2. def sigmoid(x):
  3. return 1. / (1 + np.exp(-x))
  4. def softmax(x, axis=-1, copy=True):
  5. """
  6. Args:
  7. copy: Copy x or not.
  8. Referneces:
  9. `from sklearn.utils.extmath import softmax`
  10. """
  11. if copy:
  12. x = np.copy(x)
  13. max_val = np.max(x, axis=axis, keepdims=True)
  14. x -= max_val
  15. np.exp(x, x)
  16. sum_exp = np.sum(x, axis=axis, keepdims=True)
  17. x /= sum_exp
  18. return x
  19. def log_sum_exp(x, axis=-1, keepdims=False):
  20. """
  21. References:
  22. numpy.logaddexp
  23. numpy.logaddexp2
  24. scipy.misc.logsumexp
  25. """
  26. max_val = np.max(x, axis=axis, keepdims=True)
  27. x -= max_val
  28. np.exp(x, x)
  29. sum_exp = np.sum(x, axis=axis, keepdims=keepdims)
  30. lse = np.log(sum_exp, sum_exp)
  31. if not keepdims:
  32. max_val = np.squeeze(max_val, axis=axis)
  33. return max_val + lse
  34. def l2_normalize(x, axis=0, epsilon=1e-12, copy=True):
  35. """L2 normalize an array along an axis.
  36. Args:
  37. x : array_like of floats
  38. Input data.
  39. axis : None or int or tuple of ints, optional
  40. Axis or axes along which to operate.
  41. epsilon: float, optional
  42. A small value such as to avoid division by zero.
  43. copy : bool, optional
  44. Copy X or not.
  45. """
  46. if copy:
  47. x = np.copy(x)
  48. x /= np.maximum(np.linalg.norm(x, axis=axis, keepdims=True), epsilon)
  49. return x
  50. def minmax_normalize(x, axis=0, copy=True):
  51. """minmax normalize an array along a given axis.
  52. Args:
  53. x : array_like of floats
  54. Input data.
  55. axis : None or int or tuple of ints, optional
  56. Axis or axes along which to operate.
  57. copy : bool, optional
  58. Copy X or not.
  59. """
  60. if copy:
  61. x = np.copy(x)
  62. minval = np.min(x, axis=axis, keepdims=True)
  63. maxval = np.max(x, axis=axis, keepdims=True)
  64. maxval -= minval
  65. maxval = np.maximum(maxval, 1e-5)
  66. x -= minval
  67. x /= maxval
  68. return x
  69. def get_order_of_magnitude(number):
  70. number = np.where(number == 0, 1, number)
  71. oom = np.floor(np.log10(np.abs(number)))
  72. return oom.astype(np.int32)
  73. def find_topk(x, k, axis=-1, largest=True, sorted=True):
  74. """Finds values and indices of the k largest/smallest
  75. elements along a given axis.
  76. Args:
  77. x: numpy ndarray
  78. 1-D or higher with given axis at least k.
  79. k: int
  80. Number of top elements to look for along the given axis.
  81. axis: int
  82. The axis to sort along.
  83. largest: bool
  84. Controls whether to return largest or smallest elements
  85. sorted: bool
  86. If true the resulting k elements will be sorted by the values.
  87. Returns:
  88. topk_values:
  89. The k largest/smallest elements along the given axis.
  90. topk_indices:
  91. The indices of the k largest/smallest elements along the given axis.
  92. """
  93. if largest:
  94. index_array = np.argpartition(-x, k-1, axis=axis, order=None)
  95. else:
  96. index_array = np.argpartition(x, k-1, axis=axis, order=None)
  97. topk_indices = np.take(index_array, range(k), axis=axis)
  98. topk_values = np.take_along_axis(x, topk_indices, axis=axis)
  99. if sorted:
  100. if largest:
  101. sorted_indices_in_topk = np.argsort(-topk_values, axis=axis, order=None)
  102. else:
  103. sorted_indices_in_topk = np.argsort(topk_values, axis=axis, order=None)
  104. sorted_topk_values = np.take_along_axis(
  105. topk_values, sorted_indices_in_topk, axis=axis)
  106. sorted_topk_indices = np.take_along_axis(
  107. topk_indices, sorted_indices_in_topk, axis=axis)
  108. return sorted_topk_values, sorted_topk_indices
  109. return topk_values, topk_indices