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

[BUG] - Metrics not working with Tensor inputs #128

Closed
carreremax opened this issue Apr 26, 2023 · 0 comments · Fixed by #141
Closed

[BUG] - Metrics not working with Tensor inputs #128

carreremax opened this issue Apr 26, 2023 · 0 comments · Fixed by #141
Assignees

Comments

@carreremax
Copy link

carreremax commented Apr 26, 2023

Subject
Metrics

Describe the bug
The metrics inheriting from CausalFidelity (like Deletion for example) do not work with EagerTensor.

Screenshots

...
File ***\venv\lib\site-packages\xplique\metrics\fidelity.py:179, in CausalFidelity.__init__(self, model, inputs, targets, batch_size, causal_mode, baseline_mode, steps, max_percentage_perturbed)
    176 self.baseline_mode = baseline_mode
    178 self.nb_features = np.prod(inputs.shape[1:-1])
--> 179 self.inputs_flatten = inputs.reshape((len(inputs), self.nb_features, inputs.shape[-1]))
    181 assert 0.0 < max_percentage_perturbed <= 1.0, "`max_percentage_perturbed` must be" \
    182                                               "in ]0, 1]."
    183 self.max_nb_perturbed = tf.math.floor(self.nb_features * max_percentage_perturbed)

File ***\venv\lib\site-packages\tensorflow\python\framework\ops.py:437, in Tensor.__getattr__(self, name)
    433 def __getattr__(self, name):
    434   if name in {"T", "astype", "ravel", "transpose", "reshape", "clip", "size",
    435               "tolist", "data"}:
    436     # TODO(wangpeng): Export the enable_numpy_behavior knob
--> 437     raise AttributeError(
    438         f"{type(self).__name__} object has no attribute '{name}'. " + """
    439       If you are looking for numpy-related methods, please run the following:
    440       from tensorflow.python.ops.numpy_ops import np_config
    441       np_config.enable_numpy_behavior()
    442     """)
    443   self.__getattribute__(name)

AttributeError: EagerTensor object has no attribute 'reshape'. 
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()

To Reproduce
Minimal example :

import tensorflow as tf
from xplique.metrics import Deletion
import numpy as np

inputs = tf.convert_to_tensor(np.zeros((1,10,10,3)))
targets= tf.convert_to_tensor([1])
model = tf.keras.Model()
Deletion(None, inputs, targets)

Expected behavior
According to the documentation, inputs provided as tensor should work :

inputs: Union[tf.data.Dataset, tf.Tensor, np.ndarray],

Additional context
In my code, it works with a numpy array, but will stop working if i convert the inputs to a tensorflow Tensor of same shape and content.

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

Successfully merging a pull request may close this issue.

2 participants