tensorflow / privacy

Library for training machine learning models with privacy for training data
Apache License 2.0
1.91k stars 443 forks source link

ModuleNotFoundError: No module named 'tensorflow_privacy.privacy.membership_inference_attack' #151

Open fraboeni opened 3 years ago

fraboeni commented 3 years ago

Following the README on the membership inference page yields an error.

I am running TensorFlow Privacy in a Google Colab notebook.

After adding the module with pip through !pip install tensorflow-privacy, I try running the lines specified in the README:

from tensorflow_privacy.privacy.membership_inference_attack import membership_inference_attack as mia

This yields the following error message:

---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-10-43809957f96c> in <module>()
      1 # tensorflow privacy mia imports
----> 2 from tensorflow_privacy.privacy.membership_inference_attack import membership_inference_attack as mia
      3 from tensorflow_privacy.privacy.membership_inference_attack.data_structures import AttackInputData
      4 from tensorflow_privacy.privacy.membership_inference_attack.data_structures import SlicingSpec
      5 from tensorflow_privacy.privacy.membership_inference_attack.data_structures import AttackType

ModuleNotFoundError: No module named 'tensorflow_privacy.privacy.membership_inference_attack'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

TensorFlow Privacy seems to be successfully installed before:

Requirement already satisfied: tensorflow-privacy in /usr/local/lib/python3.6/dist-packages (0.2.2)

I would be happy to know how I can keep on using the module. Thank you in advance.

hallojs commented 3 years ago

Hi,

you can try pip install -U TensorFlow-privacy, after that you may need to restart the environment and than it should work :)

fraboeni commented 3 years ago

Thank you very much, it works indeed.

fraboeni commented 3 years ago

Could you maybe also help me out with a follow-up question?

If I want to use the attack like in the README

attacks_result = mia.run_attacks(input,
                                 SlicingSpec(
                                     entire_dataset = True,
                                     by_class = True,
                                     by_classification_correctness = True
                                 ),
                                 attack_types = [
                                     AttackType.THRESHOLD_ATTACK,
                                     AttackType.MULTI_LAYERED_PERCEPTRON])

I get the error

AttributeError                            Traceback (most recent call last)

<ipython-input-36-b37c276e7f20> in <module>()
----> 1 attacks_result = mia.run_attacks(input,
      2                                  SlicingSpec(
      3                                      entire_dataset = True,
      4                                      by_class = True,
      5                                      by_classification_correctness = True

AttributeError: module 'tensorflow_privacy.privacy.membership_inference_attack.membership_inference_attack' has no attribute 'run_attacks'

Clearly, when I look inside the membership inference attack code, the function is there.

However, when I want to use mia in my notebook, it doesn't find it. Instead, if I type mia. and look at the completion options that are offered to me, I see functions like

mia.run_all_attacks
mia.run_all_attacks_and_create_summary
mia.run_attack

which are definitly not in the repo file. So I am wondering what exacly am I importing and using here. Or how am I supposed to use the library instead now (if not the way specified in the README)?

hallojs commented 3 years ago

If you want to use the latest version of TF-Privacy from GitHub you can use !pip install -U git+https://github.com/tensorflow/privacy. The version from above is the latest pypi release :)

fraboeni commented 3 years ago

Thank you again for your answer. I will do so. :)

fraboeni commented 3 years ago

Just wondering from a software-design perspective: I have some scripts I wrote in the past based on the old 0.5.1 version of the library. It would be nice if I could just keep on using them as is. Is this new (and apparently quite different) interface to the membership inference attack going to have a new version number somewhen? I now downloaded version 0.5.0 to check it contains the code that I have been working with but it looks totally different. Therefore, to me, it currently looks like the version I have been working on for a while is kind of in between. If the old 0.5.0 is different, and my version 0.5.1 that I have been working with is different from the current version 0.5.1 (for which the entire README is already updated), do you have any suggestion on how to preserve my work in the old 0.5.1 version in the future?

I hope I could express my questioin understandably.

hallojs commented 3 years ago

Unfortunately, I can't help with that. I think the interface for the attacks can still change a bit, because this part of the library is relatively young :(

Mamunahmed33 commented 3 years ago

Unfortunately it didn't work for me

`#@test {"skip": true} !pip install --quiet --upgrade tensorflow-federated-nightly !pip install --quiet --upgrade nest-asyncio

import nest_asyncio nest_asyncio.apply()`

ERROR: tensorflow-privacy 0.6.1 has requirement attrs>=21.2.0, but you'll have attrs 19.3.0 which is incompatible. ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.

!pip install tensorflow-privacy

Requirement already satisfied: tensorflow-privacy in /usr/local/lib/python3.7/dist-packages (0.6.1) Collecting attrs>=21.2.0 Using cached https://files.pythonhosted.org/packages/20/a9/ba6f1cd1a1517ff022b35acd6a7e4246371dfab08b8e42b829b6d07913cc/attrs-21.2.0-py2.py3-none-any.whl Requirement already satisfied: scipy>=0.17 in /usr/local/lib/python3.7/dist-packages (from tensorflow-privacy) (1.4.1) Requirement already satisfied: dm-tree~=0.1.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow-privacy) (0.1.6) Requirement already satisfied: mpmath in /usr/local/lib/python3.7/dist-packages (from tensorflow-privacy) (1.2.1) Requirement already satisfied: tensorflow-estimator>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow-privacy) (2.5.0) Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from scipy>=0.17->tensorflow-privacy) (1.19.5) Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.7/dist-packages (from dm-tree~=0.1.1->tensorflow-privacy) (1.15.0) ERROR: tensorflow-federated-nightly 0.19.0.dev20210703 has requirement attrs~=19.3.0, but you'll have attrs 21.2.0 which is incompatible. ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible. Installing collected packages: attrs Found existing installation: attrs 19.3.0 Uninstalling attrs-19.3.0: Successfully uninstalled attrs-19.3.0 Successfully installed attrs-21.2.0 WARNING: The following packages were previously imported in this runtime: [attr] You must restart the runtime in order to use newly installed versions.

`import collections import attr import functools import numpy as np import tensorflow as tf import tensorflow_federated as tff

np.random.seed(0)`

__AttributeError Traceback (most recent call last)

in () 4 import numpy as np 5 import tensorflow as tf ----> 6 import tensorflow_federated as tff 7 8 np.random.seed(0) 7 frames /usr/local/lib/python3.7/dist-packages/tensorflow_federated/python/aggregators/differential_privacy.py in DifferentiallyPrivateFactory() 203 query: tfp.DPQuery, 204 record_aggregation_factory: Optional[ --> 205 factory.UnweightedAggregationFactory] = None): 206 """Initializes `DifferentiallyPrivateFactory`. 207 AttributeError: module 'tensorflow_privacy' has no attribute 'DPQuery'__