Skip to content

Commit 7cd414c

Browse files
authored
Fix division by 0 warning (#481)
1 parent 36b9cfd commit 7cd414c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

emukit/bayesian_optimization/acquisitions/local_penalization.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ def _distance_calculation(x_1, x_2):
109109

110110
def _distance_with_gradient(x_1, x_2):
111111
distances = _distance_calculation(x_1, x_2)
112-
inv_distance = np.where(distances != 0.0, 1 / distances, 0)
112+
# This line is quite tricky and needs explanation
113+
# First, "~" negates boolean numpy array
114+
# Second, divide is only doing division in indexes
115+
# where provided condition is true
116+
# otherwise corresponding member of "out" arg is returned
117+
inv_distance = np.divide(1, distances, out=np.zeros_like(distances), where=~np.isclose(distances, 0))
113118
dx = x_1[:, None, :] - x_2[None, :, :]
114119
d_dist_dx = 2 * dx * inv_distance[:, :, None]
115120
return distances, d_dist_dx

0 commit comments

Comments
 (0)