Keras网络的归零和损失非常低

时间:2018-06-11 11:54:27

标签: python tensorflow keras deep-learning recurrent-neural-network

我有网络:

Tensor("input_1:0", shape=(?, 5, 1), dtype=float32)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 5, 1)              0         
_________________________________________________________________
bidirectional_1 (Bidirection (None, 5, 64)             2176      
_________________________________________________________________
activation_1 (Activation)    (None, 5, 64)             0         
_________________________________________________________________
bidirectional_2 (Bidirection (None, 5, 128)            16512     
_________________________________________________________________
activation_2 (Activation)    (None, 5, 128)            0         
_________________________________________________________________
bidirectional_3 (Bidirection (None, 1024)              656384    
_________________________________________________________________
activation_3 (Activation)    (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 1025      
_________________________________________________________________
p_re_lu_1 (PReLU)            (None, 1)                 1         
=================================================================
Total params: 676,098
Trainable params: 676,098
Non-trainable params: 0
_________________________________________________________________
None
Train on 27496 samples, validate on 6875 samples

我适合并编译它:

model.compile(loss='mse',optimizer=Adamx,metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=10,validation_data=(x_test,y_test),verbose=2)

当我运行它并在未看到的数据上对其进行评估时,它返回0.0精度,损失非常低。我无法弄清楚问题是什么。

Epoch 10/10
 - 29s - loss: 1.6972e-04 - acc: 0.0000e+00 - val_loss: 1.7280e-04 - val_acc: 0.0000e+00

1 个答案:

答案 0 :(得分:3)

预计会得到什么。您的模型工作正常,这是衡量指标的不正确之处。损失函数的目的是最小化损失,而不是提高准确性。

由于您使用PRelu作为最后一层的激活功能,因此您始终可以从网络获得浮动输出。将这些浮动输出与实际标签进行比较以测量精度似乎不是正确的选择。这是因为如果模型预测值非常接近真实标签值,则模型精度仍然为零,除非模型预测与真实标签完全相同的值。

例如,如果y_true为1.0且模型预测为0.99999,则此值不会增加模型精度的值,因为1.0!= 0.99999

<强>更新 度量函数的选择取决于问题的类型。 Keras还提供了实施custom metrics的功能。 假设问题是线性回归,如果两个值之间的差异小于0.01,则两个值相等,自定义损失度量可以定义为: -

import keras.backend as K
import tensorflow as tf
accepted_diff = 0.01
def linear_regression_equality(y_true, y_pred):
    diff = K.abs(y_true-y_pred)
    return K.mean(K.cast(diff < accepted_diff, tf.float32))

现在,您可以将此指标用于模型

model.compile(loss='mse',optimizer=Adamx,metrics=[linear_regression_equality])