utils_numpy.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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=None, 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=None, epsilon=1e-12, 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. epsilon: float, optional
  58. A small value such as to avoid division by zero.
  59. copy : bool, optional
  60. Copy x or not.
  61. """
  62. if copy:
  63. x = np.copy(x)
  64. minval = np.min(x, axis=axis, keepdims=True)
  65. maxval = np.max(x, axis=axis, keepdims=True)
  66. maxval -= minval
  67. maxval = np.maximum(maxval, epsilon)
  68. x -= minval
  69. x /= maxval
  70. return x
  71. def zscore_normalize(x, mean=None, std=None, axis=None, epsilon=1e-12, copy=True):
  72. """z-score normalize an array along a given axis.
  73. Args:
  74. x : array_like of floats
  75. Input data.
  76. mean: array_like of floats, optional
  77. mean for z-score
  78. std: array_like of floats, optional
  79. std for z-score
  80. axis : None or int or tuple of ints, optional
  81. Axis or axes along which to operate.
  82. epsilon: float, optional
  83. A small value such as to avoid division by zero.
  84. copy : bool, optional
  85. Copy x or not.
  86. """
  87. if copy:
  88. x = np.copy(x)
  89. if mean is None:
  90. mean = np.mean(x, axis=axis, keepdims=True)
  91. if std is None:
  92. std = np.std(x, axis=axis, keepdims=True)
  93. mean = np.asarray(mean, dtype=x.dtype)
  94. std = np.asarray(std, dtype=x.dtype)
  95. std = np.maximum(std, epsilon)
  96. x -= mean
  97. x /= std
  98. return x
  99. def get_order_of_magnitude(number):
  100. number = np.where(number == 0, 1, number)
  101. oom = np.floor(np.log10(np.abs(number)))
  102. return oom.astype(np.int32)
  103. def top_k(x, k, axis=-1, largest=True, sorted=True):
  104. """Finds values and indices of the k largest/smallest
  105. elements along a given axis.
  106. Args:
  107. x: numpy ndarray
  108. 1-D or higher with given axis at least k.
  109. k: int
  110. Number of top elements to look for along the given axis.
  111. axis: int
  112. The axis to sort along.
  113. largest: bool
  114. Controls whether to return largest or smallest elements
  115. sorted: bool
  116. If true the resulting k elements will be sorted by the values.
  117. Returns:
  118. topk_values:
  119. The k largest/smallest elements along the given axis.
  120. topk_indices:
  121. The indices of the k largest/smallest elements along the given axis.
  122. """
  123. if axis is None:
  124. axis_size = x.size
  125. else:
  126. axis_size = x.shape[axis]
  127. assert 1 <= k <= axis_size
  128. x = np.asanyarray(x)
  129. if largest:
  130. index_array = np.argpartition(x, axis_size-k, axis=axis)
  131. topk_indices = np.take(index_array, -np.arange(k)-1, axis=axis)
  132. else:
  133. index_array = np.argpartition(x, k-1, axis=axis)
  134. topk_indices = np.take(index_array, np.arange(k), axis=axis)
  135. topk_values = np.take_along_axis(x, topk_indices, axis=axis)
  136. if sorted:
  137. sorted_indices_in_topk = np.argsort(topk_values, axis=axis)
  138. if largest:
  139. sorted_indices_in_topk = np.flip(sorted_indices_in_topk, axis=axis)
  140. sorted_topk_values = np.take_along_axis(
  141. topk_values, sorted_indices_in_topk, axis=axis)
  142. sorted_topk_indices = np.take_along_axis(
  143. topk_indices, sorted_indices_in_topk, axis=axis)
  144. return sorted_topk_values, sorted_topk_indices
  145. return topk_values, topk_indices