Tensorflow Accuracy Metrics #887
-
The following is a very simple TensorFlow 2 image classification model. Note that the loss function is not the usual SparseCategoricalCrossentropy. Also, the last layer has only 1 output, so this is not the usual classification setting. The accuracy here does not have meaning, but I am just curious. So this code does not work well as we expected, but still produces outputs with an accuracy of around 10%, which seems reasonable. My question is how this accuracy is calculated? The prediction from this model is a continuous value and the y_true is an integer value. It is not impossible to have an x.0 for the prediction, then the accuracy is too high. `import tensorflow as tf (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() model = tf.keras.Sequential([ model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, epochs=10)` So, I have searched the TensorFlow API document to find the following example. And it makes sense. === 0.75` The following is a very simple TensorFlow 2 image classification model. Note that the loss function is not the usual SparseCategoricalCrossentropy. Also, the last layer has only 1 output, so this is not the usual classification setting. The accuracy here does not have meaning, but I am just curious. So this code does not work well as we expected, but still produces outputs with an accuracy of around 10%, which seems reasonable. My question is how this accuracy is calculated? The prediction from this model is a continuous value and the y_true is an integer value. It is not impossible to have an x.0 for the prediction, then the accuracy is too high.
So, I have searched the TensorFlow API document to find the following example. And it makes sense.
=== 0.75 `m = tf.keras.metrics.Accuracy() === 0.0` please explain me |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In your code, you're using a regression-like model with Mean Squared Error (MSE) as the loss function. if you encounter situations where y_true and y_pred have shapes like [n, 1] (2-dimensional arrays with a single column), you may need to reshape them to the expected shape [n] (1-dimensional arrays with one entry for each instance) when calculating metrics like accuracy to avoid issues and ensure compatibility with the metric calculation.
|
Beta Was this translation helpful? Give feedback.
In your code, you're using a regression-like model with Mean Squared Error (MSE) as the loss function.
When you use
metrics=['accuracy']
in this context, it calculates unusual accuracy values because it doesn't make sense for regression problems.If you're doing classification, ensure your model architecture, loss function (use categorical cross-entropy), and output layer are set up correctly.
If you're doing regression, use appropriate regression metrics like Mean Absolute Error (MAE) or Root Mean Squared Error (RMSE) for evaluation, not accuracy.
if you encounter situations where y_true and y_pred have shapes like [n, 1] (2-dimensional arrays with a single column), you may need to resha…