在keras.metrics.TruePositives中TruePositive怎么可能是十进制数?

问题描述 投票:0回答:1

我正在尝试在图像数据集上训练 CNN 模型,但我被获取 TruePositives、TrueNegatives、FalsePositives 和 FalseNegatives 的小数值所困扰。这怎么可能?

ERROR sample
Epoch 1/3
36/36 ━━━━━━━━━━━━━━━━━━━━ 69s 2s/step - false_negatives: 30.1351 - false_positives: 35.3784 - loss: 2.1995 - true_negatives: 389.0540 - true_positives: 437.6487

有一些(tp+tn+fp+tn)不等于样本总数。

完整代码


import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras.layers import Dense,Flatten,InputLayer,Conv2D,MaxPooling2D,Concatenate,Input,BatchNormalization
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.losses import BinaryCrossentropy,CategoricalCrossentropy
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
from tensorflow.keras.models import Model
from sklearn.metrics import classification_report
from tensorflow.keras.callbacks import EarlyStopping
datagen=ImageDataGenerator(rescale=1.0/255.0)
train_gen=datagen.flow_from_directory('train',class_mode='binary',
                                      target_size=(224,224),batch_size=32,shuffle=True)

output
Found 1146 images belonging to 2 classes.
tp = tf.keras.metrics.TruePositives()
tn = tf.keras.metrics.TrueNegatives()
fp = tf.keras.metrics.FalsePositives()
fn = tf.keras.metrics.FalseNegatives()
tp.update_state([0.4, .9, .7, .8], [1.0, 0.0, 1.0, 1.0])
tp.result()
output
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>
model_input=Input(shape=(224,224,3))

x=Conv2D(filters=32, kernel_size=(3,3),activation='relu',padding='valid')(model_input)
x=MaxPooling2D(pool_size=(2,2),strides=2)(x)
x=Conv2D(filters=64, kernel_size=(3,3),activation='relu',padding='valid')(x)
x=MaxPooling2D(pool_size=(2,2),strides=2)(x)
x=BatchNormalization()(x)
x=Conv2D(filters=64, kernel_size=(3,3),activation='relu',padding='valid')(x)
x=MaxPooling2D(pool_size=(2,2),strides=2)(x)
x=BatchNormalization()(x)
x=Flatten()(x)
x=Dense(units=1000,activation='relu')(x)
output=Dense(units=1,activation='sigmoid')(x)
model=Model(inputs=model_input,outputs=output)
model.compile(optimizer=Adam(),loss=BinaryCrossentropy(),metrics=[tp,fp,fn,tn])
early_stopping = EarlyStopping(monitor='val_loss', patience=2,restore_best_weights=True)


history=model.fit(x=train_gen,epochs=3,callbacks=[early_stopping])

十进制值错误

Epoch 1/3
36/36 ━━━━━━━━━━━━━━━━━━━━ 69s 2s/step - false_negatives: 30.1351 - false_positives: 35.3784 - loss: 2.1995 - true_negatives: 389.0540 - true_positives: 437.6487
Epoch 2/3
36/36 ━━━━━━━━━━━━━━━━━━━━ 61s 2s/step - false_negatives: 7.8378 - false_positives: 13.5135 - loss: 0.1692 - true_negatives: 283.1081 - true_positives: 300.4054
Epoch 3/3
36/36 ━━━━━━━━━━━━━━━━━━━━ 65s 2s/step - false_negatives: 2.3243 - false_positives: 3.0811 - loss: 0.0546 - true_negatives: 289.8108 - true_positives: 308.3513

python tensorflow machine-learning keras metrics
1个回答
0
投票

当张量流对批次进行平均并为您提供该指标的“聚合”版本时,可能会发生这种情况。要测试这一点,请尝试使用 1-sample-batchsize。

© www.soinside.com 2019 - 2024. All rights reserved.