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

AttributeError raised: 'list' object has no attribute 'dtype' when running the official example of SparseCategoricalAccuracy, TopKCategoricalAccuracy, SparseTopKCategoricalAccuracy #19709

Closed
Star9daisy opened this issue May 11, 2024 · 2 comments
Assignees

Comments

@Star9daisy
Copy link

Star9daisy commented May 11, 2024

Hi developers.

When I follow the example of SparseCategoricalAccuracy, an AttributeError is raised. Here's the code:

import keras

m = keras.metrics.SparseCategoricalAccuracy()
m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]])
print(m.result())
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], [line 2](vscode-notebook-cell:?execution_count=2&line=2)
      [1](vscode-notebook-cell:?execution_count=2&line=1) m = keras.metrics.SparseCategoricalAccuracy()
----> [2](vscode-notebook-cell:?execution_count=2&line=2) m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]])
      [3](vscode-notebook-cell:?execution_count=2&line=3) m.result()

File ~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:203, in MeanMetricWrapper.update_state(self, y_true, y_pred, sample_weight)
    [201](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:201) mask = getattr(y_pred, "_keras_mask", None)
    [202](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:202) # print("!!!", type(y_true), type(y_pred))
--> [203](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:203) values = self._fn(y_true, y_pred, **self._fn_kwargs)
    [204](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:204) if sample_weight is not None and mask is not None:
    [205](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:205)     sample_weight = losses.loss.apply_mask(
    [206](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:206)         sample_weight, mask, dtype=self.dtype, reduction="sum"
    [207](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/reduction_metrics.py:207)     )

File ~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:232, in sparse_categorical_accuracy(y_true, y_pred)
    [230](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:230) reshape_matches = False
    [231](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:231) y_pred = ops.convert_to_tensor(y_pred)
--> [232](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:232) y_true = ops.convert_to_tensor(y_true, dtype=y_true.dtype)
    [233](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:233) y_true_org_shape = ops.shape(y_true)
    [234](https://vscode-remote+ssh-002dremote-002bworkspace2.vscode-resource.vscode-cdn.net/root/workspace_ssd/projects/hep-ml-lab/playground/v0.5.0/survey_keras_metric_input/~/miniconda3/envs/uni-env-py39/lib/python3.9/site-packages/keras/src/metrics/accuracy_metrics.py:234) y_pred_rank = len(y_pred.shape)

AttributeError: 'list' object has no attribute 'dtype'
@Star9daisy Star9daisy changed the title AttributeError raised: 'list' object has no attribute 'dtype' when running the official example of SparseCategoricalAccuracy AttributeError raised: 'list' object has no attribute 'dtype' when running the official example of SparseCategoricalAccuracy, TopKCategoricalAccuracy, SparseTopKCategoricalAccuracy May 11, 2024
@Star9daisy
Copy link
Author

After checking all examples listed on Accuracy metrics, there exists the same error for SparseCategoricalAccuracy, TopKCategoricalAccuracy, SparseTopKCategoricalAccuracy.

They share the same codes in the their own functions as following:

...
    reshape_matches = False
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_true.dtype)
...

The y_true and y_pred are both lists in the examples. But they aren't properly converted into tensors like:

  1. In accuracy function, the data type of y_true are determined by y_pred which is first converted into a tensor:
...
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
...
  1. In binary_accuracy, they just call the conversion function without specifying the data type:
...
    y_true = ops.convert_to_tensor(y_true)
    y_pred = ops.convert_to_tensor(y_pred)
...
  1. In categorical_accuracy, y_true is first passed through the ops.argmax and is converted into a tensor:
...
    y_true = ops.argmax(y_true, axis=-1)

    reshape_matches = False
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_true.dtype)
...

So the three problematic functions could be handled either as the way of accuracy or binary_accuracy.

@fchollet
Copy link
Member

Thanks for the report, this is fixed at HEAD.

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

3 participants