Почему среднеквадратичная ошибка увеличивается с течением эпох?

#machine-learning #keras #tensorflow2.0

Вопрос:

Я тренирую нейронную сеть, и я сталкиваюсь с этим явлением, когда потери уменьшаются, в то время как показатель mse увеличивается. Я до сих пор не могу понять, в чем проблема. введите описание изображения здесь

Вот мой пользовательский среднеквадратичный код ошибки

 class custom_MSE(tf.keras.metrics.Metric):

  def __init__(self, name='custom_mse', **kwargs):
    super(custom_MSE, self).__init__(name=name, **kwargs)
    self.true_positives = self.add_weight(name='tp', initializer='zeros')

  def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = tf.convert_to_tensor(y_true)
    y_pred = tf.convert_to_tensor(y_pred)
    
    batch_size = tf.shape(y_true)[0]
    y_h = int(y_true.shape[1]//4)
    
    y_true_reshape = tf.reshape(y_true,shape=(batch_size,y_h,4))
    y_pred_reshape = tf.reshape(y_pred,shape=(batch_size,y_h,4))
    y_true_ = y_true_reshape[:,:,:2] # shape = (16,7,2) for example y_true_test_h_l[:,8:] = np.nan
    y_pred_ = y_pred_reshape[:,:,:2]
    
    y_true_ = tf.cast(y_true_, tf.float32)
    y_pred_ = tf.cast(y_pred_, tf.float32)

    # y_true_reg = y_true[:,:2]
    # y_pred_reg = y_pred[:,:2]

    loss = K.square(y_true_ - y_pred_)  
    
    loss = tf.experimental.numpy.nanmean(loss,axis=1)
    # loss = tf.experimental.numpy.nanmean(loss,axis=0)
    # tf.print(loss)
    if sample_weight is not None:
        sample_weight = tf.cast(sample_weight, self.dtype)
        values = tf.multiply(values, sample_weight)
    
    self.true_positives.assign_add(tf.reduce_mean(loss))

  def result(self):
    return self.true_positives

  def reset_state(self):
    self.true_positives.assign(0)
 

Ответ №1:

Проблема в том, в какой строке кода self.true_positives.assign_add(tf.reduce_mean(loss)) она должна быть self.true_positives.assign(tf.reduce_mean(loss))