我预测超市的每日销售额,作为亏损函数,我使用的是体积加权的mape。
总和超过输出节点。
我在tensorflow中实现了这个:
import tensorflow as tf
def weighted_mape_tf(y_true,y_pred):
tot = tf.reduce_sum(y_true)
wmape = tf.realdiv(tf.reduce_sum(tf.abs(tf.subtract(y_true,y_pred))),tot)*100
return(wmape)
不幸的是我的输出是:
Epoch 4/800
0s - loss: 69.3939 - mean_squared_error: 819.6549 - mean_absolute_error: 14.0599
Epoch 5/800
0s - loss: 66.0676 - mean_squared_error: 768.5440 - mean_absolute_error: 13.4120
Epoch 6/800
0s - loss: 63.3000 - mean_squared_error: 728.7665 - mean_absolute_error: 12.8934
Epoch 7/800
0s - loss: 62.0189 - mean_squared_error: 704.7637 - mean_absolute_error: 12.5851
Epoch 8/800
0s - loss: 60.4229 - mean_squared_error: 682.0646 - mean_absolute_error: 12.2814
Epoch 9/800
0s - loss: 59.6329 - mean_squared_error: 674.8835 - mean_absolute_error: 12.1172
Epoch 10/800
0s - loss: 58.5069 - mean_squared_error: 656.2922 - mean_absolute_error: 11.9073
Epoch 11/800
0s - loss: 58.0447 - mean_squared_error: 643.9082 - mean_absolute_error: 11.7542
Epoch 12/800
0s - loss: 56.9352 - mean_squared_error: 628.5248 - mean_absolute_error: 11.5936
Epoch 13/800
0s - loss: 56.3520 - mean_squared_error: 620.7517 - mean_absolute_error: 11.4170
Epoch 14/800
0s - loss: 55.8395 - mean_squared_error: 610.4476 - mean_absolute_error: 11.2979
Epoch 15/800
0s - loss: inf - mean_squared_error: 611.3271 - mean_absolute_error: 11.2931
Epoch 16/800
0s - loss: nan - mean_squared_error: nan - mean_absolute_error: nan
Epoch 17/800
0s - loss: nan - mean_squared_error: nan - mean_absolute_error: nan
Epoch 18/800
0s - loss: nan - mean_squared_error: nan - mean_absolute_error: nan
Epoch 19/800
经过一段时间后,您会看到 NaN 。 我猜错误是当tot == 0时,但是当我插入一个简单的if转换时 在0时我仍然得到NaNs。
您对此问题有经验吗?
提前谢谢
答案 0 :(得分:2)
几分钟后,我找到了问题的答案:
import tensorflow as tf
def weighted_mape_tf(y_true,y_pred):
tot = tf.reduce_sum(y_true)
tot = tf.clip_by_value(tot, clip_value_min=1,clip_value_max=1000)
wmape = tf.realdiv(tf.reduce_sum(tf.abs(tf.subtract(y_true,y_pred))),tot)*100#/tot
return(wmape)
我使用了clip_by_value来纠正0s