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,
device: device | None = 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
-
device
(device | None
, default:None
) –The device to run the model on
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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
compute_metric ¶
Wrapper to compute the performance metric.
Source code in src/stimulus/learner/predict.py
139 140 141 142 143 |
|
compute_metrics ¶
Wrapper to compute the performance metrics.
Source code in src/stimulus/learner/predict.py
135 136 137 |
|
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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
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
129 130 131 132 133 |
|
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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|