3.2. Momentum Operator
The momentum_operator
module contains the main operators for the momentum version ConFIG algorithm.
Operator Classes¤
conflictfree.momentum_operator.PseudoMomentumOperator
¤
Bases: MomentumOperator
The major momentum version. In this operator, the second momentum is estimated by a pseudo gradient based on the result of the gradient operator. NOTE: The momentum-based operator, e.g., Adam, is not recommend when using this operator. Please consider using SGD optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_vectors |
int
|
The number of gradient vectors. |
required |
beta_1 |
float
|
The moving average constant for the first momentum. |
0.9
|
beta_2 |
float
|
The moving average constant for the second momentum. |
0.999
|
gradient_operator |
GradientOperator
|
The base gradient operator. Defaults to ConFIGOperator(). |
ConFIGOperator()
|
loss_recorder |
LossRecorder
|
The loss recorder object. If you want to pass loss information to "update_gradient" method or "apply_gradient" method, you need to specify a loss recorder. Defaults to None. |
None
|
Methods:
Name | Description |
---|---|
calculate_gradient |
Calculates the gradient based on the given indexes, gradients, and losses. |
update_gradient |
Updates the gradient of the given network with the calculated gradient. |
Examples:
from conflictfree.momentum_operator import PseudoMomentumOperator
from conflictfree.utils import get_gradient_vector,apply_gradient_vector
optimizer=torch.Adam(network.parameters(),lr=1e-3)
operator=PseudoMomentumOperator(num_vector=len(loss_fns)) # initialize operator, the only difference here is we need to specify the number of gradient vectors.
for input_i in dataset:
grads=[]
for loss_fn in loss_fns:
optimizer.zero_grad()
loss_i=loss_fn(input_i)
loss_i.backward()
grads.append(get_gradient_vector(network))
g_config=operator.calculate_gradient(grads) # calculate the conflict-free direction
apply_gradient_vector(network) # or simply use `operator.update_gradient(network,grads)` to calculate and set the condlict-free direction to the network
optimizer.step()
Source code in conflictfree/momentum_operator.py
132 133 134 135 136 137 138 139 140 141 142 143 144 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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
update_gradient
¤
update_gradient(
network: torch.nn.Module,
indexes: Union[int, Sequence[int]],
grads: Union[torch.Tensor, Sequence[torch.Tensor]],
losses: Optional[Union[float, Sequence]] = None,
) -> None
Updates the gradient of the given network with the calculated gradient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
network |
Module
|
The network to update the gradient. |
required |
indexes |
Union[int, Sequence[int]]
|
The indexes of the gradient vectors and losses to be updated. The momentum with the given indexes will be updated based on the given gradients. |
required |
grads |
Union[Tensor, Sequence[Tensor]]
|
The gradients to update. It can be a stack of gradient vectors (at dim 0) or a sequence of gradient vectors. |
required |
losses |
Optional[Sequence]
|
The losses associated with the gradients. The losses will be passed to base gradient operator. If the base gradient operator doesn't require loss information, you can set this value as None. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented in a subclass. |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in conflictfree/momentum_operator.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
__init__
¤
__init__(
num_vectors: int,
beta_1: float = 0.9,
beta_2: float = 0.999,
gradient_operator: GradientOperator = ConFIGOperator(),
loss_recorder: Optional[LossRecorder] = None,
) -> None
Source code in conflictfree/momentum_operator.py
172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
calculate_gradient
¤
calculate_gradient(
indexes: Union[int, Sequence[int]],
grads: Union[torch.Tensor, Sequence[torch.Tensor]],
losses: Optional[Union[float, Sequence]] = None,
) -> torch.Tensor
Calculates the gradient based on the given indexes, gradients, and losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indexes |
Union[int, Sequence[int]]
|
The indexes of the gradient vectors and losses to be updated. The momentum with the given indexes will be updated based on the given gradients. |
required |
grads |
Union[Tensor, Sequence[Tensor]]
|
The gradients to update. It can be a stack of gradient vectors (at dim 0) or a sequence of gradient vectors. |
required |
losses |
Optional[Sequence]
|
The losses associated with the gradients. The losses will be passed to base gradient operator. If the base gradient operator doesn't require loss information, you can set this value as None. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented in a subclass. |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The calculated gradient. |
Source code in conflictfree/momentum_operator.py
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
conflictfree.momentum_operator.SeparateMomentumOperator
¤
Bases: MomentumOperator
In this operator, each gradient has its own second gradient. The gradient operator is applied on the rescaled momentum. NOTE: Please consider using the PseudoMomentumOperator since this operator does not give good performance according to our research. The momentum-based operator, e.g., Adam, is not recommend when using this operator. Please consider using SGD optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_vectors |
int
|
The number of gradient vectors. |
required |
beta_1 |
float
|
The moving average constant for the first momentum. |
0.9
|
beta_2 |
float
|
The moving average constant for the second momentum. |
0.999
|
gradient_operator |
GradientOperator
|
The base gradient operator. Defaults to ConFIGOperator(). |
ConFIGOperator()
|
loss_recorder |
LossRecorder
|
The loss recorder object. If you want to pass loss information to "update_gradient" method or "apply_gradient" method, you need to specify a loss recorder. Defaults to None. |
None
|
Methods:
Name | Description |
---|---|
calculate_gradient |
Calculates the gradient based on the given indexes, gradients, and losses. |
update_gradient |
Updates the gradient of the given network with the calculated gradient. |
Source code in conflictfree/momentum_operator.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
update_gradient
¤
update_gradient(
network: torch.nn.Module,
indexes: Union[int, Sequence[int]],
grads: Union[torch.Tensor, Sequence[torch.Tensor]],
losses: Optional[Union[float, Sequence]] = None,
) -> None
Updates the gradient of the given network with the calculated gradient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
network |
Module
|
The network to update the gradient. |
required |
indexes |
Union[int, Sequence[int]]
|
The indexes of the gradient vectors and losses to be updated. The momentum with the given indexes will be updated based on the given gradients. |
required |
grads |
Union[Tensor, Sequence[Tensor]]
|
The gradients to update. It can be a stack of gradient vectors (at dim 0) or a sequence of gradient vectors. |
required |
losses |
Optional[Sequence]
|
The losses associated with the gradients. The losses will be passed to base gradient operator. If the base gradient operator doesn't require loss information, you can set this value as None. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented in a subclass. |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in conflictfree/momentum_operator.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
__init__
¤
__init__(
num_vectors: int,
beta_1: float = 0.9,
beta_2: float = 0.999,
gradient_operator: GradientOperator = ConFIGOperator(),
loss_recorder: Optional[LossRecorder] = None,
) -> None
Source code in conflictfree/momentum_operator.py
265 266 267 268 269 270 271 272 273 274 275 |
|
calculate_gradient
¤
calculate_gradient(
indexes: Union[int, Sequence[int]],
grads: Union[torch.Tensor, Sequence[torch.Tensor]],
losses: Optional[Union[float, Sequence]] = None,
) -> torch.Tensor
Calculates the gradient based on the given indexes, gradients, and losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indexes |
Union[int, Sequence[int]]
|
The indexes of the gradient vectors and losses to be updated. The momentum with the given indexes will be updated based on the given gradients. |
required |
grads |
Union[Tensor, Sequence[Tensor]]
|
The gradients to update. It can be a stack of gradient vectors (at dim 0) or a sequence of gradient vectors. |
required |
losses |
Optional[Sequence]
|
The losses associated with the gradients. The losses will be passed to base gradient operator. If the base gradient operator doesn't require loss information, you can set this value as None. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented in a subclass. |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The calculated gradient. |
Source code in conflictfree/momentum_operator.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
Base Class of Operators¤
conflictfree.momentum_operator.LatestLossRecorder
¤
Bases: LossRecorder
A loss recorder return the latest losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_losses |
int
|
The number of losses to record |
required |
Source code in conflictfree/loss_recorder.py
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 |
|
record_all_losses
¤
record_all_losses(losses: Sequence) -> list
Records all the losses and returns the recorded losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses |
Tensor
|
The losses to record. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
The recorded losses. |
Source code in conflictfree/loss_recorder.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
__init__
¤
__init__(num_losses: int) -> None
Source code in conflictfree/loss_recorder.py
75 76 |
|
record_loss
¤
record_loss(
losses_indexes: Union[int, Sequence[int]],
losses: Union[float, Sequence],
) -> list
Records the given loss and returns the recorded loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses_indexes |
Union[int, Sequence[int]]
|
The index of the loss. |
required |
losses |
Tensor
|
The loss to record. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
The recorded loss. |
Source code in conflictfree/loss_recorder.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|