title: Weighted Mape in Tensorflow
body: <p>I'm forecasting daily sales in a supermarket and as a loss function I use the volume weighted mape. </p>
  
  <p><a href="https://i.stack.imgur.com/e6WuS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e6WuS.png" alt="Volume Weighted Mape"></a></p>
  
  <p>The sum is over the output nodes. </p>
  
  <p>I implemented this in tensorflow: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Unfortunately my output is: </p>
  
  <pre><code>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
  </code></pre>
  
  <p>As you see after a while it becomes always <strong>NaN</strong>. 
  I guess the error is when tot==0, but when I insert a simple if transforming 
  tot when 0 I still get NaNs. </p>
  
  <p>Do you have any experience with this issue? </p>
  
  <p>Thank you in advance</p>