Add L2 weight decay ops to network Adds L2 weight decay ops. L2WeightDecay = reg_coeff * parameter Args: param: parameter variable for which regularization is applied block: block in which variable is to be created Returns: new v
(
self,
param: paddle.Tensor,
grad: paddle.Tensor,
block: pir.Block,
)
| 237 | self._coeff = coeff |
| 238 | |
| 239 | def __call__( |
| 240 | self, |
| 241 | param: paddle.Tensor, |
| 242 | grad: paddle.Tensor, |
| 243 | block: pir.Block, |
| 244 | ): |
| 245 | """Add L2 weight decay ops to network |
| 246 | |
| 247 | Adds L2 weight decay ops. |
| 248 | L2WeightDecay = reg_coeff * parameter |
| 249 | |
| 250 | Args: |
| 251 | param: parameter variable for which regularization is applied |
| 252 | block: block in which variable is to be created |
| 253 | |
| 254 | Returns: |
| 255 | new variable for weight decay |
| 256 | """ |
| 257 | assert isinstance( |
| 258 | param, (framework.Variable, pir.Value, pir.core.ParameterMeta) |
| 259 | ) |
| 260 | assert isinstance(block, (framework.Block, pir.Block)) |
| 261 | |
| 262 | if in_dynamic_or_pir_mode(): |
| 263 | return _C_ops.scale(param, self._coeff, 0.0, True) |
| 264 | else: |
| 265 | decay = block.create_var( |
| 266 | dtype=param.dtype, shape=param.shape, lod_level=param.lod_level |
| 267 | ) |
| 268 | |
| 269 | # Append Op to calculate decay |
| 270 | block.append_op( |
| 271 | type='scale', |
| 272 | inputs={"X": param}, |
| 273 | outputs={"Out": decay}, |
| 274 | attrs={"scale": self._coeff}, |
| 275 | ) |
| 276 | |
| 277 | return decay |
| 278 | |
| 279 | def __str__(self) -> str: |
| 280 | return f"L2Decay, coeff={self._coeff:f}" |
nothing calls this directly
no test coverage detected