Browse Source

awg northChina

anweiguo 1 week ago
parent
commit
de722b7ac9
1 changed files with 35 additions and 4 deletions
  1. 35 4
      models_processing/model_tf/losses.py

+ 35 - 4
models_processing/model_tf/losses.py

@@ -195,10 +195,41 @@ class SouthLoss(Loss):
 
 
 
+class NorthChina(Loss):
+    """Root Mean Squared Error 损失函数(兼容单变量/多变量回归)"""
+
+    def __init__(self,
+                 name="north_china_loss",
+                 reduction="sum_over_batch_size",  # 默认自动选择 'sum_over_batch_size' (等效于 mean)
+                 **kwargs):
+        super().__init__(name=name, reduction=reduction)
+
+    def call(self, y_true, y_pred):
+        # 计算误差 e = y_true - y_pred
+        error = y_true - y_pred
+        abs_error = tf.abs(error)
+
+        # 加上 epsilon 避免除以 0
+        epsilon = 1e-8
+        weight = abs_error / (tf.reduce_sum(abs_error) + epsilon)
+
+        weighted_squared_error = tf.square(error) * weight
+        loss = tf.sqrt(tf.reduce_sum(weighted_squared_error))
+        return loss
+
+    def get_config(self):
+        """支持序列化配置(用于模型保存/加载)"""
+        base_config = super().get_config()
+        return base_config
+
+
+
+
 region_loss_d = {
     'northeast': lambda region: RMSE(region),
     'south': lambda cap, region: SouthLoss(cap, region),
-    'zone': lambda region: MSE_ZONE(region) # 分区建模损失:MSE + 分区总和一致性约束
+    'zone': lambda region: MSE_ZONE(region), # 分区建模损失:MSE + 分区总和一致性约束
+    'northchina': lambda region: NorthChina(region) #华北损失函数
 }
 
 
@@ -224,18 +255,18 @@ if __name__ == '__main__':
     mse = tf.keras.losses.MeanSquaredError()(y_true, y_pred).numpy()
 
     # 自定义损失(权重=1时等效于MSE)
-    custom_mse = MSE(name='test')(y_true, y_pred).numpy()
+    custom_mse = NorthChina(name='test')(y_true, y_pred).numpy()
 
     print("标准 MSE:", mse)  # 输出: 0.25
     print("自定义 MSE:", custom_mse)  # 应输出: 0.25
-    assert abs(mse - custom_mse) < 1e-6
+    # assert abs(mse - custom_mse) < 1e-6
 
     # 定义变量和优化器
     y_pred_var = tf.Variable([[1.5], [2.5], [3.5]], dtype=tf.float32)
     optimizer = tf.keras.optimizers.Adam()
 
     with tf.GradientTape() as tape:
-        loss = MSE(name='test')(y_true, y_pred_var)
+        loss = NorthChina(name='test')(y_true, y_pred_var)
     grads = tape.gradient(loss, y_pred_var)
 
     # 理论梯度公式:2*(y_pred - y_true)/N (N=3)