Runs num_predictions times the prediction of the classifier on the input X and puts the predictions in a list. Args: classifier: The classifier for which the labels are to be queried. X: The pool of samples to query from. dropout_layer_i
(classifier: BaseEstimator, X: modALinput, dropout_layer_indexes: list = [],
num_predictions: int = 50, sample_per_forward_pass: int = 1000,
logits_adaptor: Callable[[torch.tensor, modALinput], torch.tensor] = default_logits_adaptor)
| 214 | |
| 215 | |
| 216 | def get_predictions(classifier: BaseEstimator, X: modALinput, dropout_layer_indexes: list = [], |
| 217 | num_predictions: int = 50, sample_per_forward_pass: int = 1000, |
| 218 | logits_adaptor: Callable[[torch.tensor, modALinput], torch.tensor] = default_logits_adaptor): |
| 219 | """ |
| 220 | Runs num_predictions times the prediction of the classifier on the input X |
| 221 | and puts the predictions in a list. |
| 222 | |
| 223 | Args: |
| 224 | classifier: The classifier for which the labels are to be queried. |
| 225 | X: The pool of samples to query from. |
| 226 | dropout_layer_indexes: Indexes of the dropout layers which should be activated |
| 227 | Choose indices from : list(torch_model.modules()) |
| 228 | num_predictions: Number of predictions which should be made |
| 229 | sample_per_forward_pass: max. sample number for each forward pass. |
| 230 | The allocated RAM does mainly depend on this. |
| 231 | Small number --> small RAM allocation |
| 232 | logits_adaptor: Callable which can be used to adapt the output of a forward pass |
| 233 | to the required vector format for the vectorised metric functions |
| 234 | Return: |
| 235 | prediction: list with all predictions |
| 236 | """ |
| 237 | |
| 238 | assert num_predictions > 0, 'num_predictions must be larger than zero' |
| 239 | assert sample_per_forward_pass > 0, 'sample_per_forward_pass must be larger than zero' |
| 240 | |
| 241 | predictions = [] |
| 242 | # set dropout layers to train mode |
| 243 | set_dropout_mode(classifier.estimator.module_, |
| 244 | dropout_layer_indexes, train_mode=True) |
| 245 | |
| 246 | split_args = [] |
| 247 | |
| 248 | if isinstance(X, Mapping): # check for dict |
| 249 | for k, v in X.items(): |
| 250 | |
| 251 | v.detach() |
| 252 | split_v = torch.split(v, sample_per_forward_pass) |
| 253 | # create sub-dictionary split for each forward pass with same keys&values |
| 254 | for split_idx, split in enumerate(split_v): |
| 255 | if len(split_args) <= split_idx: |
| 256 | split_args.append({}) |
| 257 | split_args[split_idx][k] = split |
| 258 | |
| 259 | elif torch.is_tensor(X): # check for tensor |
| 260 | X.detach() |
| 261 | split_args = torch.split(X, sample_per_forward_pass) |
| 262 | else: |
| 263 | raise RuntimeError( |
| 264 | "Error in model data type, only dict or tensors supported") |
| 265 | |
| 266 | for i in range(num_predictions): |
| 267 | |
| 268 | probas = [] |
| 269 | |
| 270 | for samples in split_args: |
| 271 | # call Skorch infer function to perform model forward pass |
| 272 | # In comparison to: predict(), predict_proba() the infer() |
| 273 | # does not change train/eval mode of other layers |
no test coverage detected
searching dependent graphs…