Model Validation¶
-
class
opacus.dp_model_inspector.
DPModelInspector
(should_throw=True)[source]¶ - Class to validate if a given module meets the requirements for attaching
PrivacyEngine
.
Active checks are listed in the
DPModelInspector.inspectors
attribute.- Parameters
should_throw (
bool
) – Whether the inspector should throw an exception or return False in case of validation error
-
validate
(model)[source]¶ Runs the validation on the model and all its submodules.
Validation comprises a series of individual
ModelInspectors
, each checking one predicate. Depending onshould_throw
flag in the constructor, will either return False or throwIncompatibleModuleException
in case of validation failure.Notes
This method is called in
opacus.privacy_engine.PrivacyEngine.attach()
.- Parameters
model (
Module
) – The model to validate.- Return type
- Returns
True if successful. False if validation fails and
should_throw == False
- Raises
IncompatibleModuleException – If the validation fails and
should_throw == True
. Exception message will contain the details of validation failure reason.
Example
>>> inspector = DPModelInspector() >>> valid_model = nn.Linear(16, 32) >>> is_valid = inspector.validate(valid_model) >>> is_valid True >>> invalid_model = nn.BatchNorm1d(2) >>> is_valid = inspector.validate(invalid_model) # IncompatibleModuleException is thrown.