@@ -77,11 +77,9 @@ cpdef np.ndarray[np.float64_t, ndim=1] dist_membership_vector(
77
77
78
78
if softmax:
79
79
for i in range (vector.shape[0 ]):
80
- if vector[i] != 0 :
81
- result[i] = np.exp(1.0 / vector[i])
82
- else :
83
- result[i] = DBL_MAX / vector.shape[0 ]
84
- sum += result[i]
80
+ result[i] = 1.0 / vector[i]
81
+ result = np.exp(result - np.nanmax(result))
82
+ sum = np.sum(result)
85
83
86
84
else :
87
85
for i in range (vector.shape[0 ]):
@@ -225,8 +223,10 @@ cpdef np.ndarray[np.float64_t, ndim=1] outlier_membership_vector(neighbor,
225
223
if softmax:
226
224
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
227
225
max_lambda_dict, cluster_tree)
228
- result = np.exp(result)
229
- result[~ np.isfinite(result)] = np.finfo(np.double).max
226
+ # Scale for numerical stability, mathematically equivalent with old
227
+ # version due to the scaling with the sum in below.
228
+ result = np.exp(result - np.nanmax(result))
229
+ # result[~np.isfinite(result)] = np.finfo(np.double).max
230
230
else :
231
231
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
232
232
max_lambda_dict, cluster_tree)
@@ -310,8 +310,10 @@ cpdef np.ndarray[np.float64_t, ndim=2] all_points_outlier_membership_vector(
310
310
max_lambda_dict,
311
311
cluster_tree)
312
312
if softmax:
313
- result = np.exp(per_cluster_scores)
314
- result[~ np.isfinite(result)] = np.finfo(np.double).max
313
+ # Scale for numerical stability, mathematically equivalent with old
314
+ # version due to the scaling with the sum in below.
315
+ result = np.exp(per_cluster_scores - np.nanmax(per_cluster_scores))
316
+ # result[~np.isfinite(result)] = np.finfo(np.double).max
315
317
else :
316
318
result = per_cluster_scores
317
319
@@ -354,4 +356,3 @@ cpdef all_points_prob_in_some_cluster(
354
356
result[point] = (heights.max() / max_lambda)
355
357
356
358
return result
357
-
0 commit comments