为什么我的损失极低,但准确度却是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
tf.keras.metrics.accuracy()
工作不同。它检查预测值是否与相应的标签匹配。这是在引擎盖下,
tf.keras.metrics.accuracy()
计算tf.equal(y_true,y_pred)
。在您的情况下,y_pred
包含浮点号,而标签(y_true
)是一个热编码的整数。考虑情况:
好吧,如果您检查
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()
calculatestf.equal(y_true, y_pred)
. In your casey_pred
contains floating point numbers whereas the labels (y_true
) are one hot encoded integers.Consider the case:
Well, if you check the result of
tf.equal(y_true, y_pred)
, it will yield onlyFalse
values.In your case, you should either use
metrics = ['accuracy']
ormetrics = [tf.keras.metrics.CategoricalAccuracy()]
.