predict ¶
A module for making predictions with PyTorch models using DataLoaders.
Classes:
-
PredictWrapper
–A wrapper to predict the output of a model on a datset loaded into a torch DataLoader.
PredictWrapper ¶
PredictWrapper(
model: Module,
dataloader: DataLoader,
loss_dict: Optional[dict[str, Any]] = None,
)
A wrapper to predict the output of a model on a datset loaded into a torch DataLoader.
It also provides the functionalities to measure the performance of the model.
Parameters:
-
model
(Module
) –The PyTorch model to make predictions with
-
dataloader
(DataLoader
) –DataLoader containing the evaluation data
-
loss_dict
(Optional[dict[str, Any]]
, default:None
) –Optional dictionary of loss functions
Methods:
-
compute_loss
–Compute the loss.
-
compute_metric
–Wrapper to compute the performance metric.
-
compute_metrics
–Wrapper to compute the performance metrics.
-
compute_other_metric
–Compute the performance metric.
-
handle_predictions
–Handle the model outputs from forward pass, into a dictionary of tensors, just like y.
-
predict
–Get the model predictions.
Source code in src/stimulus/learner/predict.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
compute_loss ¶
compute_loss() -> float
Compute the loss.
The current implmentation basically computes the loss for each batch and then averages them. TODO we could potentially summarize the los across batches in a different way. Or sometimes we may potentially even have 1+ losses.
Source code in src/stimulus/learner/predict.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
compute_metric ¶
Wrapper to compute the performance metric.
Source code in src/stimulus/learner/predict.py
92 93 94 95 96 |
|
compute_metrics ¶
Wrapper to compute the performance metrics.
Source code in src/stimulus/learner/predict.py
88 89 90 |
|
compute_other_metric ¶
Compute the performance metric.
TODO currently we computes the average performance metric across target y, but maybe in the future we want something different¶
Source code in src/stimulus/learner/predict.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
handle_predictions ¶
Handle the model outputs from forward pass, into a dictionary of tensors, just like y.
Source code in src/stimulus/learner/predict.py
82 83 84 85 86 |
|
predict ¶
predict(*, return_labels: bool = False) -> Union[
dict[str, Tensor],
tuple[dict[str, Tensor], dict[str, Tensor]],
]
Get the model predictions.
Basically, it runs a foward pass on the model for each batch, gets the predictions and concatenate them for all batches. Since the returned current_predictions
are formed by tensors computed for one batch, the final predictions
are obtained by concatenating them.
At the end it returns predictions
as a dictionary of tensors with the same keys as y
.
If return_labels if True, then the labels
will be returned as well, also as a dictionary of tensors.
Parameters:
-
return_labels
(bool
, default:False
) –Whether to also return the labels
Returns:
-
Union[dict[str, Tensor], tuple[dict[str, Tensor], dict[str, Tensor]]]
–Dictionary of predictions, and optionally labels
Source code in src/stimulus/learner/predict.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|