title: Weighted mse custom loss function in keras
body: <p>I'm working with time series data, outputting 60 predicted days ahead.</p>
  
  <p>I'm currently using mean squared error as my loss function and the results are bad</p>
  
  <p>I want to implement a weighted mean squared error such that the early outputs are much more important than later ones.</p>
  
  <p><strong>Weighted Mean Square Root formula:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/ufpow.png" alt="Weighted Mean Square Root formula"></p>
  
  <p>So I need some way to iterate over a tensor's elements, with an index (since I need to iterate over both the predicted and the true values at the same time, then write the results to a tensor with only one element. They're both (?,60) but really (1,60) lists.</p>
  
  <p>And nothing I'm trying is working. Here's the code for the broken version</p>
  
  <pre><code>def weighted_mse(y_true,y_pred):
      wmse = K.cast(0.0,'float')
  
      size = K.shape(y_true)[0]
      for i in range(0,K.eval(size)):
          wmse += 1/(i+1)*K.square((y_true[i]-y_pred)[i])
  
      wmse /= K.eval(size)
      return wmse
  </code></pre>
  
  <p>I am currently getting this error as a result:</p>
  
  <pre><code>InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_2_target' with dtype float
   [[Node: dense_2_target = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  </code></pre>
  
  <p>Having read the replies to similar posts, I don't think a mask can accomplish the task, and looping over elements in one tensor would also not work since I'd not be able to access the corresponding element in the other tensor.</p>
  
  <p>Any suggestions would be appreciated</p>