DiCE
DiCE copied to clipboard
AttributeError: module 'tensorflow' has no attribute 'get_default_session'
Installed dice-ml from pypi.
import dice_ml
from dice_ml.utils import helpers # helper functions
d = dice_ml.Data(dataframe=helpers.load_adult_income_dataset(),
continuous_features=['age', 'hours_per_week'],
outcome_name='income')
m = dice_ml.Model(model_path=dice_ml.utils.helpers.get_adult_income_modelpath())
exp = dice_ml.Dice(d,m)
Getting the following error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-d3dad7eed1c9> in <module>
8 m = dice_ml.Model(model_path=dice_ml.utils.helpers.get_adult_income_modelpath())
9 # DiCE explanation instance
---> 10 exp = dice_ml.Dice(d,m)
~/anaconda3/envs/diceml/lib/python3.6/site-packages/dice_ml/dice.py in __init__(self, data_interface, model_interface, **kwargs)
15 """
16
---> 17 self.decide_implementation_type(data_interface, model_interface, **kwargs)
18
19 def decide_implementation_type(self, data_interface, model_interface, **kwargs):
~/anaconda3/envs/diceml/lib/python3.6/site-packages/dice_ml/dice.py in decide_implementation_type(self, data_interface, model_interface, **kwargs)
21
22 self.__class__ = decide(data_interface, model_interface)
---> 23 self.__init__(data_interface, model_interface, **kwargs)
24
25 # To add new implementations of DiCE, add the class in explainer_interfaces subpackage and import-and-return the class in an elif loop as shown in the below method.
~/anaconda3/envs/diceml/lib/python3.6/site-packages/dice_ml/explainer_interfaces/dice_tensorflow1.py in __init__(self, data_interface, model_interface)
26
27 # create TensorFLow session if one is not already created
---> 28 if tf.get_default_session() is not None:
29 self.dice_sess = tf.get_default_session()
30 else:
AttributeError: module 'tensorflow' has no attribute 'get_default_session'
My pip freeze result
absl-py==0.11.0
appnope==0.1.0
argon2-cffi==20.1.0
astunparse==1.6.3
async-generator==1.10
attrs==20.2.0
backcall==0.2.0
bleach==3.2.1
cachetools==4.1.1
certifi==2020.6.20
cffi==1.14.3
chardet==3.0.4
dataclasses==0.7
decorator==4.4.2
defusedxml==0.6.0
dice-ml==0.4
entrypoints==0.3
future==0.18.2
gast==0.3.3
google-auth==1.22.1
google-auth-oauthlib==0.4.2
google-pasta==0.2.0
grpcio==1.33.2
h5py==2.10.0
idna==2.10
importlib-metadata==2.0.0
ipykernel==5.3.4
ipython==7.16.1
ipython-genutils==0.2.0
ipywidgets==7.5.1
jedi==0.17.2
Jinja2==2.11.2
joblib==0.17.0
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==6.1.7
jupyter-console==6.2.0
jupyter-core==4.6.3
jupyterlab-pygments==0.1.2
Keras-Preprocessing==1.1.2
lazy-import==0.2.2
Markdown==3.3.3
MarkupSafe==1.1.1
mistune==0.8.4
mkl-fft==1.2.0
mkl-random==1.1.1
mkl-service==2.3.0
nbclient==0.5.1
nbconvert==6.0.7
nbformat==5.0.8
nest-asyncio==1.4.2
notebook==6.1.4
numpy @ file:///opt/concourse/worker/volumes/live/e6a5a904-ea21-4841-4eec-d53e1ac1014c/volume/numpy_and_numpy_base_1603479626870/work
oauthlib==3.1.0
olefile==0.46
opt-einsum==3.3.0
packaging==20.4
pandas==0.25.3
pandocfilters==1.4.3
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow @ file:///opt/concourse/worker/volumes/live/06069510-e277-4aed-54f4-6dfdcb84a461/volume/pillow_1603822272490/work
prometheus-client==0.8.0
prompt-toolkit==3.0.3
protobuf==3.13.0
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
Pygments==2.7.2
pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pytz==2020.1
pyzmq==19.0.2
qtconsole==4.7.7
QtPy==1.9.0
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scikit-learn==0.23.2
scipy==1.5.3
Send2Trash==1.5.0
six==1.15.0
tensorboard==2.3.0
tensorboard-plugin-wit==1.7.0
tensorflow==2.3.1
tensorflow-cpu==2.3.1
tensorflow-estimator==2.3.0
tensorflow-gpu==1.1.0
termcolor==1.1.0
terminado==0.9.1
testpath==0.4.4
threadpoolctl==2.1.0
torch==1.7.0
torchaudio==0.7.0a0+ac17b64
torchvision==0.8.1
tornado==6.0.4
traitlets==4.3.3
typing-extensions @ file:///tmp/build/80754af9/typing_extensions_1598376058250/work
urllib3==1.25.11
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==1.0.1
widgetsnbextension==3.5.1
wrapt==1.12.1
zipp==3.4.0
If I change torch or tensorflow version other issues crop up. .
If fresh dice-ml install done in a new env where dice only install torch and tensorflow, then the error is
AttributeError: module 'torch.jit' has no attribute '_script_if_tracing'
The quick patch i found is to change import tensorflow as tf --> import tensorflow.compat.v1 as tf
in the "dice_tensorflow1.py" file
@CoteDave Now getting AttributeError: module 'tensorflow' has no attribute 'get_default_session'
I'm using Google colab notebook. Try to downgrade the Tensorflow to 1.x .
DiCE internally selects a version of the explainer implementation based on the backend
parameter to dice_ml.Model
. The default version is TensorFlow 1.x so you need to set it manually if you are using TF2 or PyTorch. Please check out the section Loading the ML model in this notebook: https://github.com/interpretml/DiCE/blob/master/docs/source/notebooks/DiCE_getting_started.ipynb.
Specifically, for your question, doing the below will resolve the error:
backend = 'TF'+tf.__version__[0] # TF2 in your case
ML_modelpath = helpers.get_adult_income_modelpath(backend=backend)
m = dice_ml.Model(model_path= ML_modelpath, backend=backend)
This is actually not closed. I used a new environment and install DICE first, which in turn downloads TensorFlow, and PyTorch. Still issues persist.
yeah okay, so did you try using the backend parameter as mentioned in the previous comment?
yes, that resulted in the next issue
If your error is with PyTorch installation and if you are in Windows, then try manually installing PyTorch using something like this: pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
.
In any case, just if your TensorFlow installation is working, then you should be able to run DiCE notebooks using the appropriate backend parameter (backend='TF2'). Can you try with the correct backend parameter for TF and let know if you are able to generate CFs?
I ran into the same issue - new environment, install DiCE, install requirements (which installs TF 2.5), run the example from the README.md. It fails to detect that the model is from TF2 and dies.
It seems that a simple way to accurately determine whether it's a TF1 or TF2 model is to try to initiate a session. If you have TF2, it will error out like with:
AttributeError: module 'tensorflow' has no attribute 'get_default_session'
I can put together a pull request if that would be helpful to close this issue.
Same issue here for the README example and also a new environment with installed everything from the requirements files!
To reproduce error:
>>> python -m venv mynewenvironment
>>> cd mynewenvironment
>>> source bin/activate
>>> git clone https://github.com/interpretml/DiCE.git
>>> cd DiCE
>>> pip install requirements.txt
>>> pip install requirements-deeplearning.txt
>>> pip install ipykernel
>>> python3 -m ipykernel --user --name=mynewenvironment
>>> jupyter-notebook
And finally I chosed mynewenvironment for the notebook kernel.
For the first cell in jupyter notebook (the example from README.md)
import dice_ml
from dice_ml.utils import helpers # helper functions
# Dataset for training an ML model
d = dice_ml.Data(dataframe=helpers.load_adult_income_dataset(),
continuous_features=['age', 'hours_per_week'],
outcome_name='income')
# Pre-trained ML model
m = dice_ml.Model(model_path=dice_ml.utils.helpers.get_adult_income_modelpath())
# DiCE explanation instance
exp = dice_ml.Dice(d,m)
Running this code would gives me error AttributeError: module 'tensorflow' has no attribute 'get_default_session'
.
My System:
- Using windows wsl Ubuntu 20.04.1 LTS
- CPU: I7 9750h
- GPU: GTX 1660Ti (Cuda is not configured for tensorflow)