【问题标题】:Why is the mean squared error increasing over epochs?为什么均方误差会随着时间的推移而增加?
【发布时间】:2021-11-29 14:07:40
【问题描述】:

我正在训练一个神经元网络,我遇到了这种现象,即损失在减少,而 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)

【问题讨论】:

    标签: machine-learning keras tensorflow2.0


    【解决方案1】:

    问题出在self.true_positives.assign_add(tf.reduce_mean(loss))这行代码中 应该是self.true_positives.assign(tf.reduce_mean(loss))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2021-11-22
      相关资源
      最近更新 更多