训练和测试模型时指标值相等

时间:2018-12-17 05:17:03

标签: tensorflow keras neural-network deep-learning

我正在使用带有TensorFlow后端的Keras使用python开发神经网络模型。数据集包含两个结果为1或0的序列,数据集中的正负比为1到9。模型将这两个序列作为输入,并输出概率。最初,我的模型有一个包含一个隐藏单元和S形激活函数的密集层作为输出,但是后来我将模型的最后一层更改为具有两个隐藏单元和softmax激活函数的密集层,并使用Keras ['SL', '2018-09-30', '89.7', '-2.1644', '5.9884', 'Max Kepler', '596146', '518858', 'field_out', 'hit_into_play', '', '', '', '', '9', 'Max Kepler grounds out, shortstop Leury Garcia to first baseman Matt Davidson. ', 'R', 'L', 'R', 'MIN', 'CWS', 'X', '6', 'ground_ball', '1', '2', '2018', '0.0456', '0.4072', '0.7193', '1.8204', 'null', 'null', 'null', '2', '8', 'Bot', '118.48', '141.29', '', '', '456078', '', '180930_215102', '6.8928', '-130.3241', '-5.0488', '-0.9006', '26.5098', '-26.9291', '3.5258', '1.6280', '60', '63.4', '6.3970', '88.0450', '2186', '5.2660', '531825', '518858', '456078', '571602', '660162', '570560', '544725', '547170', '641477', '594953', '55.2332', '0.176', '0.145', '0.00', '1', '0', '0', '2', '67', '5', 'Slider', '5', '4', '5', '4', '4', '5', '5', '4', 'Strategic', 'Standard'] 函数更改了数据集的结果。经过这些更改后,包含“准确性”,“精度”,“召回率”,“ F1”,“ AUC”的模型指标均相等,并且值较高或较高。这是我用于这些指标的实现

to_categorical

这是训练结果

def recall(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1(y_true, y_pred):
    precisionValue = precision(y_true, y_pred)
    recallValue = recall(y_true, y_pred)
    return 2*((precisionValue*recallValue)/(precisionValue+recallValue+K.epsilon()))

def auc(y_true, y_pred):
    auc = tf.metrics.auc(y_true, y_pred)[1]
    K.get_session().run(tf.local_variables_initializer())
    return auc

此后,我使用Epoch 1/5 4026/4026 [==============================] - 17s 4ms/step - loss: 1.4511 - acc: 0.9044 - f1: 0.9044 - auc: 0.8999 - precision: 0.9044 - recall: 0.9044 Epoch 2/5 4026/4026 [==============================] - 15s 4ms/step - loss: 1.4573 - acc: 0.9091 - f1: 0.9091 - auc: 0.9087 - precision: 0.9091 - recall: 0.9091 Epoch 3/5 4026/4026 [==============================] - 15s 4ms/step - loss: 1.4573 - acc: 0.9091 - f1: 0.9091 - auc: 0.9083 - precision: 0.9091 - recall: 0.9091 Epoch 4/5 4026/4026 [==============================] - 15s 4ms/step - loss: 1.4573 - acc: 0.9091 - f1: 0.9091 - auc: 0.9090 - precision: 0.9091 - recall: 0.9091 Epoch 5/5 4026/4026 [==============================] - 15s 4ms/step - loss: 1.4573 - acc: 0.9091 - f1: 0.9091 - auc: 0.9085 - precision: 0.9091 - recall: 0.9091 测试了模型,并使用sklearn的predict函数计算了指标,再次得到了相同的结果。指标都相等并且具有较高的值(0.93),根据我生成的混淆矩阵,这是错误的 enter image description here

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

由于Keras 2.0的精度,Recall和F1已被删除,因为这些度量标准应该是全局计算的,但它们是按批计算的。您的代码类似于keras 1.X中使用的代码,可能是问题。

尝试使用软件包keras_metrics

import keras
import keras_metrics

model = models.Sequential()
model.add(keras.layers.Dense(1, activation="sigmoid", input_dim=2))
model.add(keras.layers.Dense(1, activation="softmax"))

model.compile(optimizer="sgd",
              loss="binary_crossentropy",
              metrics=[keras_metrics.precision(), keras_metrics.recall()])