PyTorchBaseAlgo
PytorchBaseAlgorithm
Bases: BaseAlgorithm
Source code in sequel/algos/pytorch/pytorch_base_algo.py
18 19 20 21 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 60 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
__init__(backbone, benchmark, optimizer, callbacks=[], loggers=None, lr_decay=None, grad_clip=None, reinit_optimizer=True, device='cuda:0', min_lr=5e-05)
Inits the PytorchBaseAlgorithm class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backbone |
PytorchBaseBackbone
|
The backbone model, e.g., a CNN. |
required |
benchmark |
Benchmark
|
The benchmark, e.g., SplitMNIST. |
required |
optimizer |
torch.optim.Optimizer
|
The optimizer used to update the backbone weights. |
required |
callbacks |
Iterable[BaseCallback]
|
A list of callbacks. At least one instance of MetricCallback should be given. Defaults to []. |
[]
|
loggers |
Optional[Logger]
|
A list of logger, e.g. for Weights&Biases logging functionality. Defaults to None. |
None
|
lr_decay |
Optional[float]
|
A learning rate decay used for every new task. Defaults to None. |
None
|
grad_clip |
Optional[float]
|
The gradient clipping norm. Defaults to None. |
None
|
reinit_optimizer |
bool
|
Indicates whether the optimizer state is reinitialized before fitting a new task. Defaults to True. |
True
|
device |
str
|
description. Defaults to "cuda:0". |
'cuda:0'
|
min_lr |
float
|
description. Defaults to 0.00005. |
5e-05
|
Note
- the
_configure_optimizers
method will be moved to a dedicated Callback.
Source code in sequel/algos/pytorch/pytorch_base_algo.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 60 61 62 63 64 65 66 67 68 69 |
|
forward(*args, **kwargs)
Calls the forward function of the model.
Source code in sequel/algos/pytorch/pytorch_base_algo.py
94 95 96 97 98 |
|
test_step(*args, **kwargs)
Performs the testing step. Callbacks are offered for each step of the process.
Source code in sequel/algos/pytorch/pytorch_base_algo.py
126 127 128 |
|
valid_step(*args, **kwargs)
Performs the validation step. Callbacks are offered for each step of the process.
Source code in sequel/algos/pytorch/pytorch_base_algo.py
120 121 122 123 124 |
|
PytorchRegularizationBaseAlgorithm
Bases: PytorchBaseAlgorithm
Source code in sequel/algos/pytorch/pytorch_base_algo.py
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
calculate_parameter_importance()
Calculcates the per-parameter importance. Should return a dictionary with keys in the format
"{name}_importance" where name corresponds the torch.nn.Parameter
the importance is attached to.
Raises:
Type | Description |
---|---|
NotImplementedError
|
Should be implemented according to each algorithm. |
Source code in sequel/algos/pytorch/pytorch_base_algo.py
206 207 208 209 210 211 212 213 |
|
calculate_regularization_loss()
Calculates the regularization loss:
where \Omega_i is the importance of parameter i, \theta_i and \theta_{i, \textrm{old}} are the current and previous task's parameters.
The parameter importances \Omega_i are calculated by the method calculate_parameter_importance
.
Source code in sequel/algos/pytorch/pytorch_base_algo.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
compute_loss(predictions, targets, task_ids, *args, **kwargs)
Computes the loss. For tasks excluding the initial one, the loss also includes the regularization term.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Tensor
|
Model predictions. |
required |
targets |
Tensor
|
Targets of the current batch. |
required |
task_ids |
Tensor
|
Task ids of the current batch. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
the overall loss. |
Source code in sequel/algos/pytorch/pytorch_base_algo.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|