IBM / federated-learning-lib

A library for federated learning (a distributed machine learning process) in an enterprise environment.
Other
493 stars 134 forks source link

Run demo project, module 'tensorflow' has no attribute 'get_default_graph' #96

Closed lmaxeniro closed 2 years ago

lmaxeniro commented 2 years ago

Hello, I am following the guide (https://github.com/IBM/federated-learning-lib/blob/main/quickstart.md) to run the example project: The aggregator server start fine, while when I am trying to start parties, I meet below error: Python = 3.7.9 tensorflow = 2.1.0 keras = 2.2.4

python -m ibmfl.party.party examples/configs/iter_avg/keras/config_party0.yml 
2022-04-28 04:01:38,511 | 1.0.6 | INFO | ibmfl.util.config                             | Getting config from file
2022-04-28 04:01:38,511 | 1.0.6 | INFO | ibmfl.util.config                             | Getting details from config file.
2022-04-28 04:01:39,012 | 1.0.6 | INFO | ibmfl.util.config                             | No metrics recorder config provided for this setup.
Using TensorFlow backend.
2022-04-28 04:01:40,356 | 1.0.6 | INFO | ibmfl.util.config                             | No metrics config provided for this setup.
2022-04-28 04:01:40,356 | 1.0.6 | INFO | ibmfl.util.config                             | No evidencia recordeer config provided for this setup.
2022-04-28 04:01:40,356 | 1.0.6 | INFO | ibmfl.util.data_handlers.mnist_keras_data_handler | Loaded training data from examples/data/mnist/random/data_party0.npz
2022-04-28 04:01:40,383 | 1.0.6 | INFO | __main__                                      | Error occurred while loading party configuration
2022-04-28 04:01:40,383 | 1.0.6 | ERROR | __main__                                      | module 'tensorflow' has no attribute 'get_default_graph'
Traceback (most recent call last):
  File "/home/ubuntu/miniconda3/envs/ibm_ffl/lib/python3.7/site-packages/ibmfl/party/party.py", line 84, in __init__
    self.fl_model = model_cls_ref(model_name, spec, info=model_info)
  File "/home/ubuntu/miniconda3/envs/ibm_ffl/lib/python3.7/site-packages/ibmfl/model/keras_fl_model.py", line 59, in __init__
    self.graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

search round it looks the get_default_graph had been removed from tf2.0 afterwards.. so this demo code won't work for TF2.1.0? is there any workaround?

chalianwar commented 2 years ago

Thanks for trying IBMFL. Keras is part of TensorFlow from TF version 2.0 or greater. Please use tf instead of keras and everything will run as expected. If you want to use standalone keras, you need to install TF version 1.15. Please let us know if you still face issue after trying tf.

lmaxeniro commented 2 years ago

I did not modify any code, simply run the original code from github. Just to be clear, the install guidance mentioned that TF2.1.0 is supported that is why I install TF2.1.0 -- do you mean for this example it does not support TF2.1.0? -- or do you mean I shall remove Keras from the TF2.1.0 environment?

chalianwar commented 2 years ago

It does support TF 2.1.0. What I suggested was to use tf instead of keras when generating configs as Keras is part of TF now:

python examples/generate_configs.py -n 2 -f iter_avg -m tf -d mnist -p examples/data/mnist/random/

python -m ibmfl.aggregator.aggregator examples/configs/iter_avg/tf/config_agg.yml

python -m ibmfl.party.party examples/configs/iter_avg/tf/config_party0.yml

OR following in detail:

python examples/generate_data.py -n 2 -d mnist -pp 200 python examples/generate_configs.py -n 2 -f iter_avg -m tf -d mnist -p examples/data/mnist/random

python -m ibmfl.aggregator.aggregator examples/configs/iter_avg/tf/config_agg.yml

START

python -m ibmfl.party.party examples/configs/iter_avg/tf/config_party0.yml

START REGISTER

python -m ibmfl.party.party examples/configs/iter_avg/tf/config_party1.yml

START REGISTER

% At aggregator terminal start training TRAIN

lmaxeniro commented 2 years ago

Thanks a lot, this indeed works. I would suggest that you put a little more explanation in the guide about this difference (tf vs. keras)