declearn.model.tensorflow.TensorflowOptiModule
Bases: OptiModule
Framework-specific OptiModule to wrap tensorflow-keras optimizers.
This tensorflow-only OptiModule enables wrapping a tensorflow-keras
keras.optimizers.Optimizer
to make it part of a declearn Optimizer
pipeline, where it may be combined with other, framework-agnostic
tools (notably FL-specific ones such as the FedProx loss regularizer).
The wrapped keras Optimizer states will be placed on a device (CPU
or GPU) selected automatically based on the global device policy.
This device will also be used to place all wrapped computations.
The reset
and set_state
methods both result in consulting the
policy anew and therefore updating the placement of internal states
and computations. reset
also drops internal states' values.
Please note that this relies on a hack that may have unforeseen side effects on the optimization algorithm if used carelessly and will at any rate cause some memory overhead. Thus it should be used sparingly, taking into account the following constraints and limitations:
- The wrapped optimizer's learning rate will be forced to 1.0, so that updates' scaling remains the responsibility of the wrapping declearn Optimizer.
- The wrapped optimizer should not make use of the updated variables' values, only of their gradients, because it will in fact operate on artificial, zero-valued variables at each step.
- If the module is to be used by the clients, the wrapped optimizer class must have been imported from a third-party package that is also available to the clients (e.g. tensorflow).
This class is mostly provided for experimental use of algorithms that are not natively available in declearn, for users that do not want to put in (or reserve for later) the effort of writing a custom, dedicated, framework-agnostic OptiModule subclass implementing that algorithm. If you encounter issues, please report to the declearn developers, and we will be happy to assist with debugging the present module and/or implementing the desired algorithm as a proper OptiModule.
Finally, please note that some keras optimizers use different formulas than other reference implementations, including the declearn ones (e.g. for Adam, Adagrad or RMSProp). As a result, switching a keras optimizer instead of a declearn one can lead to diverging results.
Source code in declearn/model/tensorflow/_optim.py
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 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
__init__(optim)
Instantiate a hacky tensorflow optimizer plug-in module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optim |
Union[tf_keras.optimizers.Optimizer, str, Dict[str, Any]]
|
Keras optimizer instance that needs wrapping, or configuration
dict or string identifier of one, enabling its retrieval using
|
required |
Note that the wrapped optimizer's base learning rate will be forced
to be 1.0 and be constant. EMA and weight decay will also be forced
not to be used due to the wrapped optimizer not accessing the actual
model parameters; to implement these, please use the weight_decay
parameter of declearn.optimizer.Optimizer
and/or the EWMAModule
plug-in.
Source code in declearn/model/tensorflow/_optim.py
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 |
|
reset()
Reset this module to its uninitialized state.
Discard the wrapped tensorflow Variables (that define a required
specification of input gradients), and replace the optimizer with
a new, uninitialized one. As a consequence, the next call to run
will result in setting a new required input specification.
This method also updates the device-placement policy of the states
and computations wrapped by this OptiModule, based on the global
policy accessed via declearn.utils.get_device_policy
.
Source code in declearn/model/tensorflow/_optim.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
run(gradients)
Run input gradients through the wrapped keras Optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gradients |
Vector
|
Input gradients that are to be processed and updated. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If |
KeyError
|
If |
Returns:
Name | Type | Description |
---|---|---|
gradients |
TensorflowVector
|
Modified input gradients. The output Vector should be fully compatible with the input one - only the values of the wrapped coefficients may have changed. |
Source code in declearn/model/tensorflow/_optim.py
125 126 127 128 129 130 131 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 |
|