Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: choose lighter dtype for distance and error stored values #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/macest/classification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,17 @@ def calc_dist_to_neighbours(
neighbours = np.array(
self.graph[cls].knnQueryBatch( # type: ignore
x_star, k=self._num_neighbours, num_threads=num_threads_available
)
),
dtype='float32',
)
class_dist = neighbours[:, 1, :].clip(min=10 ** -15)
class_ind = neighbours[:, 0, :].astype(int)
if self.training_preds_by_class is None:
raise ValueError("training_preds_by_class has already been cached")
class_preds = self.training_preds_by_class[cls]
class_error = np.array(
[class_preds[class_ind[j]] != cls for j in range(x_star.shape[0])]
[class_preds[class_ind[j]] != cls for j in range(x_star.shape[0])],
dtype='bool',
)
else:
if self.distance_to_neighbours is None:
Expand Down Expand Up @@ -507,7 +509,8 @@ def _precompute_neighbours(self) -> PrecomputedNeighbourInfo:
max_neighbours = np.array(
self.model.graph[class_num].knnQueryBatch( # type: ignore
self.x_cal, k=max_nbrs, num_threads=num_threads_available
)
),
dtype='float32',
)
max_dist = max_neighbours[x_cal_len_array, 1]
max_ind = max_neighbours[x_cal_len_array, 0]
Expand All @@ -519,7 +522,8 @@ def _precompute_neighbours(self) -> PrecomputedNeighbourInfo:
[
cls_preds[ind[j].astype(int)] != class_num
for j in range(self.x_cal.shape[0])
]
],
dtype='bool',
) # type: ignore

dist_dict[k] = dist
Expand Down
6 changes: 4 additions & 2 deletions src/macest/regression/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def calc_nn_dist(self, x_star: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
neighbours = np.array(
self.prec_graph.knnQueryBatch(
x_star, k=self._num_neighbours, num_threads=num_threads_available
)
),
dtype='float32',
)
dist = neighbours[:, 1, :]
ind = neighbours[:, 0, :].astype(int)
Expand Down Expand Up @@ -437,7 +438,8 @@ def _prec_neighbours(self) -> Tuple[Dict[int, np.ndarray], Dict[int, np.ndarray]
max_neighbours = np.array(
self.prec_graph.knnQueryBatch(
self.x_cal, k=int(max_nbrs), num_threads=num_threads_available
)
),
dtype='float32',
)

max_dist = max_neighbours[x_cal_len_array, 1]
Expand Down