Closed zedoul closed 1 year ago
Thanks for raising this issue. I am not sure this really depends on Opacus: when the model is trained, it is no longer an "Opacus model" but just a regular "Pytorch model". I would recommend to try Opacus with various levels of noise and/or clipping value to see if that changes the uncertainty prediction.
Hi @zedoul As Alex mentioned, this issue isn't related to DP-SGD training process, but rather with the model itself. Although, Opacus do play a part here, since it switches BatchNorm to GroupNorm.
So what happens is:
1) ModuleValidator
replaces BatchNorm1d(4)
layer with GroupNorm(4,4)
2) When applied to batched 1-dimensional input, GroupNorm(4,4)
effectively erases the data, as it applies the normalization over the array of size 1 for every feature.
3) Then, model is not really training, since every input is erased to an array of zeros after the first normalization layer.
You can fix that from your side by manually replacing BatchNorms with GroupNorms with more sensible number of groups, or with other type of normalization.
There's one thing that we can improve in opacus - let user specify number of groups when applying .fix()
method, now it's defaulted to gcd(32, module.num_features)
@alexandresablayrolles @ffuuugor I appreciate the assistance from both of you. After implementing the updated model code, it functions correctly. Finally I can compare Opacus and TF Privacy in better way. Once again, thank you.
🐛 Bug
Hi,
First of all, thank you for a great open source product! Here I think I found a potential issue on Opacus, so I would like to report the bug.
A short summary: An opacus-based classfication model generates the same uncertainty value for every input, when the number of feature is equal to 4, on Wine dataset.
My hypothesis at this moment is that this error is able to be reproduced when 1) a classification model 2) a tabular data and 3) the number of X is 4. The last condition may sound very strange and I try to reproduce the bug with another conditions, but that's where my conclusion is ended up right now. I am pretty sure that the number of features is not something related to a root cause and there must be another datasets that may cause the same error but without the third condition. However, at least I hope my sample code is something that is reproduciable on your local. Also, the data works fine on TensorFlow privacy equivalent implementation.
Here is my reproduced result. I tried to use Colab as you recommended but I unfortunately do not know how to use it. Instead, I copied and pasted the code and attached dataset. The dataset that I used is the kaggle Wine data. But, as I said, I faced the error on another data too.
My sample code is not really great, e.g., you may find some hyperparamters could be further improved, etc. Regardless, I expect that any output from Opacus-enhanced version of PyTorch should produce different uncertainty values if the inputs are different, especially when a normal PyTorch, without Opacus, generates different uncertainty values.
wine.csv
Please reproduce using our template Colab and post here the link
To Reproduce
Expected behavior
A classification model based on Opacus-enhanced PyTorch classification model should generate different uncertainty values for different input.
Environment
Here is a requirements.txt file for the environment.
conda
,pip
, source): pip