AttributeError raised: 'list' object has no attribute 'dtype' when running the official example of SparseCategoricalAccuracy, TopKCategoricalAccuracy, SparseTopKCategoricalAccuracy
Star9daisy opened this issue · 2 comments
Star9daisy commented
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 commented
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:
- In
accuracy
function, the data type ofy_true
are determined byy_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)
...
- 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)
...
- In
categorical_accuracy
,y_true
is first passed through theops.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 commented
Thanks for the report, this is fixed at HEAD.