MCPcopy
hub / github.com/meta-pytorch/opacus / make_private

Method make_private

opacus/privacy_engine.py:307–475  ·  view source on GitHub ↗

Add privacy-related responsibilities to the main PyTorch training objects: model, optimizer, and the data loader. All of the returned objects act just like their non-private counterparts passed as arguments, but with added DP tasks. - Model is wrapped to al

(
        self,
        *,
        module: nn.Module,
        optimizer: optim.Optimizer,
        criterion=nn.CrossEntropyLoss(),  # Added deafult for backward compatibility
        data_loader: DataLoader,
        noise_multiplier: float,
        max_grad_norm: Union[float, List[float]],
        batch_first: bool = True,
        loss_reduction: str = "mean",
        poisson_sampling: bool = True,
        clipping: str = "flat",
        noise_generator=None,
        grad_sample_mode: str = "hooks",
        wrap_model: bool = True,
        rand_on_empty: bool = False,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

305 return module
306
307 def make_private(
308 self,
309 *,
310 module: nn.Module,
311 optimizer: optim.Optimizer,
312 criterion=nn.CrossEntropyLoss(), # Added deafult for backward compatibility
313 data_loader: DataLoader,
314 noise_multiplier: float,
315 max_grad_norm: Union[float, List[float]],
316 batch_first: bool = True,
317 loss_reduction: str = "mean",
318 poisson_sampling: bool = True,
319 clipping: str = "flat",
320 noise_generator=None,
321 grad_sample_mode: str = "hooks",
322 wrap_model: bool = True,
323 rand_on_empty: bool = False,
324 **kwargs,
325 ) -> Union[
326 Tuple[
327 Union[AbstractGradSampleModule, GradSampleHooks], DPOptimizer, DataLoader
328 ],
329 Tuple[
330 Union[AbstractGradSampleModule, GradSampleHooks],
331 DPOptimizer,
332 DPLossFastGradientClipping,
333 DataLoader,
334 ],
335 ]:
336 """
337 Add privacy-related responsibilities to the main PyTorch training objects:
338 model, optimizer, and the data loader.
339
340 All of the returned objects act just like their non-private counterparts
341 passed as arguments, but with added DP tasks.
342
343 - Model is wrapped to also compute per sample gradients.
344 - Optimizer is now responsible for gradient clipping and adding noise to the gradients.
345 - Criterion is a wrapper around the original criterion that packages the two backward passes for fast gradient clipping.
346 - DataLoader is updated to perform Poisson sampling.
347
348 Notes:
349 Using any other models, optimizers, or data sources during training
350 will invalidate stated privacy guarantees.
351
352 Args:
353 module: PyTorch module to be used for training
354 optimizer: Optimizer to be used for training
355 data_loader: DataLoader to be used for training
356 noise_multiplier: The ratio of the standard deviation of the Gaussian noise to
357 the L2-sensitivity of the function to which the noise is added
358 (How much noise to add)
359 max_grad_norm: The maximum norm of the per-sample gradients. Any gradient with norm
360 higher than this will be clipped to this value.
361 batch_first: Flag to indicate if the input tensor to the corresponding module
362 has the first dimension representing the batch. If set to True, dimensions on
363 input tensor are expected be ``[batch_size, ...]``, otherwise
364 ``[K, batch_size, ...]``

Calls 7

_prepare_modelMethod · 0.95
_prepare_data_loaderMethod · 0.95
_prepare_optimizerMethod · 0.95
_prepare_criterionMethod · 0.95
attach_step_hookMethod · 0.80
get_optimizer_hook_fnMethod · 0.80