为什么我的损失极低,但准确度却是 0?

发布于 2025-01-19 04:11:10 字数 1451 浏览 1 评论 0原文

我正在培训一个模型,以使用TensorFlow对3类图像进行分类。目前,我正在使用框标准resnet50定义:

input_layer = Input(shape=(203,147,256))
model = tf.keras.applications.resnet50.ResNet50(weights=None, input_tensor=input_layer, classes=3

这是我的模型汇编:

model.compile(optimizer=tf.keras.optimizers.Adam(), #default learning_rate=0.001
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.Accuracy()])

使用sklearn.model_selection.train_test_split ,我实例化tensorflow数据集对象:

train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train))
val_ds = tf.data.Dataset.from_tensor_slices((X_val, y_val))

train_ds = train_ds.batch(2)
val_ds = val_ds.batch(2

并开始培训:

history = model.fit(train_ds,
                    validation_data=val_ds,
                    epochs=200,
                    verbose=2)#verbopsity 2 means no one line per epoch

当训练时:过于拟合,但这是以后的问题。我现在的问题是我的训练损失极小,但是我的训练准确性为0或接近零。这是最终的训练时期:

Epoch 200/200
33/33 - 3s - loss: 4.4143e-06 - accuracy: 0.0000e+00 - val_loss: 1.5500 - val_accuracy: 0.0000e+00

我认为张力流根据训练损失计算准确性。我绝对不知道为什么我的损失看起来很好,为什么我的准确性为零。我是否正在使用适当的损失和指标?有人以前看过这种行为吗?任何帮助都受到赞赏

热门编码,我的培训和验证混乱矩阵分别在下面:

[[23  0  0]
 [ 0 21  0]
 [ 0  0 22]]

[[0 0 7]
 [0 7 2]
 [0 0 7]]

一个

我的培训标签是 度量本身错误,因为我认为培训分类看起来还不错

I am training a model to classify 3 classes of images using Tensorflow. Currently I am using the box standard ResNet50 definition:

input_layer = Input(shape=(203,147,256))
model = tf.keras.applications.resnet50.ResNet50(weights=None, input_tensor=input_layer, classes=3

Here is my model compilation:

model.compile(optimizer=tf.keras.optimizers.Adam(), #default learning_rate=0.001
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.Accuracy()])

Having split my data using sklearn.model_selection.train_test_split, I instantiate Tensorflow Dataset objects:

train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train))
val_ds = tf.data.Dataset.from_tensor_slices((X_val, y_val))

train_ds = train_ds.batch(2)
val_ds = val_ds.batch(2

And begin training:

history = model.fit(train_ds,
                    validation_data=val_ds,
                    epochs=200,
                    verbose=2)#verbopsity 2 means no one line per epoch

When training, I am severely overfitting, but that is an issue for later. My issue now is that my training loss is extremely small, but my training accuracy is 0 or near zero. Here is the final training epoch:

Epoch 200/200
33/33 - 3s - loss: 4.4143e-06 - accuracy: 0.0000e+00 - val_loss: 1.5500 - val_accuracy: 0.0000e+00

I thought that Tensorflow calculates accuracy based on the training loss. I have absolutely no idea why my accuracy is zero if my loss seems so well. Am I using the proper loss and metrics? Has anyone seen this behaviour before? Any help is appreciated

Addendum:

My training labels are one hot encoded, and my training and validation confusion matrices are below, respectively:

[[23  0  0]
 [ 0 21  0]
 [ 0  0 22]]

[[0 0 7]
 [0 7 2]
 [0 0 7]]

(I know I am super overfitting, but that is a problem for later)

I am starting to think there is something wrong with the metric itself, because I think the training classification looks pretty good

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2025-01-26 04:11:10

这是因为tf.keras.metrics.accuracy()工作不同。它检查预测值是否与相应的标签匹配。这是

在引擎盖下,tf.keras.metrics.accuracy()计算tf.equal(y_true,y_pred)。在您的情况下,y_pred包含浮点号,而标签(y_true)是一个热编码的整数。

考虑情况:

y_pred = array([0.5403488 , 0.24064924, 0.219002  ], dtype=float32)

y_true = array([1., 0., 0.], dtype=float32)

好吧,如果您检查tf.equal(y_true,y_pred)的结果,它将仅产生false值。

在您的情况下,您应该使用量表= ['cercecy']量表= [tf.keras.metrics.categoricalaccuracy()]

That's because tf.keras.metrics.Accuracy() works different. It checks if predicted values match with the corresponding labels. Here is the source code if you want to check that.

Under the hood, tf.keras.metrics.Accuracy() calculates tf.equal(y_true, y_pred). In your case y_pred contains floating point numbers whereas the labels (y_true) are one hot encoded integers.

Consider the case:

y_pred = array([0.5403488 , 0.24064924, 0.219002  ], dtype=float32)

y_true = array([1., 0., 0.], dtype=float32)

Well, if you check the result of tf.equal(y_true, y_pred), it will yield only False values.

In your case, you should either use metrics = ['accuracy'] or metrics = [tf.keras.metrics.CategoricalAccuracy()].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文