Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors while running classification_with_tfdf from keras-io #51

Open
Humbulani1234 opened this issue Dec 21, 2024 · 0 comments
Open

Errors while running classification_with_tfdf from keras-io #51

Humbulani1234 opened this issue Dec 21, 2024 · 0 comments

Comments

@Humbulani1234
Copy link
Owner

The script has the following error:

Traceback (most recent call last):
  File "/home/humbulani/tensorflow-env/keras-io-master/examples/structured_data/classification_with_tfdf.py", line 290, in <module>
    gbt_model = create_gbt_model()
                ^^^^^^^^^^^^^^^^^^
  File "/home/humbulani/tensorflow-env/keras-io-master/examples/structured_data/classification_with_tfdf.py", line 282, in create_gbt_model
    gbt_model.compile(metrics=[keras.metrics.BinaryAccuracy(name="accuracy")])
  File "/home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tensorflow_decision_forests/keras/core_inference.py", line 852, in compile
    super(InferenceCoreModel, self).compile(
  File "/home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tensorflow/python/distribute/distribute_lib.py", line 4029, in variable_created_in_scope
    return v._distribute_strategy is None  # pylint: disable=protected-access
           ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Variable' object has no attribute '_distribute_strategy'

I have traced the error to the following section of code from the function tf_keras.src.engine.training._validate_compile:

strategy = self.distribute_strategy
        for metric in tf.nest.flatten(metrics):
            for v in getattr(metric, "variables", []): # ERROR IS HERE
                if not strategy.extended.variable_created_in_scope(v):
                    raise ValueError(
                        f"Metric ({metric}) passed to `model.compile` was "
                        "created inside a different distribution strategy "
                        "scope than the model. All metrics must be created "
                        "in the same distribution strategy "
                        f"scope as the model (in this case {strategy}). "
                        "If you pass in a string identifier for a metric to "
                        "compile, the metric will automatically be created "
                        "in the correct distribution strategy scope."
                    )

and pdb has produced the following trace:

(Pdb) n
> /home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tf_keras/src/engine/training.py(3927)_validate_compile()
-> strategy = self.distribute_strategy
(Pdb) n
> /home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tf_keras/src/engine/training.py(3928)_validate_compile()
-> for metric in tf.nest.flatten(metrics):
(Pdb) p strategy
<tensorflow.python.distribute.distribute_lib._DefaultDistributionStrategy object at 0x7eda39f63750>
(Pdb) list
3923 	                    )
3924 	
3925 	        # Model metrics must be created in the same distribution strategy scope
3926 	        # as the model.
3927 	        strategy = self.distribute_strategy
3928 ->	        for metric in tf.nest.flatten(metrics):
3929 	            for v in getattr(metric, "variables", []):
3930 	                if not strategy.extended.variable_created_in_scope(v):
3931 	                    raise ValueError(
3932 	                        f"Metric ({metric}) passed to `model.compile` was "
3933 	                        "created inside a different distribution strategy "
(Pdb) n
> /home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tf_keras/src/engine/training.py(3929)_validate_compile()
-> for v in getattr(metric, "variables", []):
(Pdb) p metric
<BinaryAccuracy name=accuracy>
(Pdb) n
> /home/humbulani/tensorflow-env/env/lib/python3.11/site-packages/tf_keras/src/engine/training.py(3930)_validate_compile()
-> if not strategy.extended.variable_created_in_scope(v):
(Pdb) p v
<Variable path=accuracy/total, shape=(), dtype=float32, value=0.0>
(Pdb) p v._distribute_strategy
*** AttributeError: 'Variable' object has no attribute '_distribute_strategy'
(Pdb) p metric.variables
[<Variable path=accuracy/total, shape=(), dtype=float32, value=0.0>, <Variable path=accuracy/count, shape=(), dtype=float32, value=0.0>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant