-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from WenjieDu/extract_latent_from_clustering_…
…models Extract latent from clustering models
- Loading branch information
Showing
4 changed files
with
89 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
# Created by Wenjie Du <[email protected]> | ||
# License: GLP-v3 | ||
|
||
from typing import Union, Optional | ||
from typing import Union, Optional, Tuple | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -419,7 +419,8 @@ def cluster( | |
self, | ||
X: Union[dict, str], | ||
file_type: str = "h5py", | ||
) -> np.ndarray: | ||
return_latent: bool = False, | ||
) -> Union[np.ndarray, Tuple[np.ndarray, dict]]: | ||
self.model.eval() # set the model as eval status to freeze it. | ||
test_set = DatasetForCRLI(X, return_labels=False, file_type=file_type) | ||
test_loader = DataLoader( | ||
|
@@ -428,15 +429,27 @@ def cluster( | |
shuffle=False, | ||
num_workers=self.num_workers, | ||
) | ||
latent_collector = [] | ||
clustering_latent_collector = [] | ||
imputation_collector = [] | ||
|
||
with torch.no_grad(): | ||
for idx, data in enumerate(test_loader): | ||
inputs = self._assemble_input_for_testing(data) | ||
inputs = self.model.forward(inputs, training=False) | ||
latent_collector.append(inputs["fcn_latent"]) | ||
clustering_latent_collector.append(inputs["fcn_latent"]) | ||
imputation_collector.append(inputs["imputation"]) | ||
|
||
latent_collector = torch.cat(latent_collector).cpu().detach().numpy() | ||
clustering = self.model.kmeans.fit_predict(latent_collector) | ||
imputation = torch.cat(imputation_collector).cpu().detach().numpy() | ||
clustering_latent = ( | ||
torch.cat(clustering_latent_collector).cpu().detach().numpy() | ||
) | ||
clustering_results = self.model.kmeans.fit_predict(clustering_latent) | ||
latent_collector = { | ||
"clustering_latent": clustering_latent, | ||
"imputation": imputation, | ||
} | ||
|
||
if return_latent: | ||
return clustering_results, latent_collector | ||
|
||
return clustering | ||
return clustering_results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters