title: "Tensor conversion requested dtype float64 for Tensor with dtype float32"
body: <p>I'm trying to implement my own loss function with the keras backend.
  My loss function is inspired from this article : <a href="https://www.sciencedirect.com/science/article/pii/S0167865517301666" rel="nofollow noreferrer">https://www.sciencedirect.com/science/article/pii/S0167865517301666</a> and compute the cohen Kappa score</p>
  
  <pre><code>    eye = K.eye(self.number_of_classes)
      n=2
      class_id = 0
      N = K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
      class_id = 1
      N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
      class_id = 2
      N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
      # class_id = 3
      # N += K.sum(((K.argmax(y_true)-class_id)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id])
  
      class_id = 0
      N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
      class_id_2 = 0
      D = K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 1
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 2
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # class_id_2 = 3
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
  
      class_id = 1
      N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
      class_id_2 = 0
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 1
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 2
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # class_id_2 = 3
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
  
      class_id = 2
      N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
      class_id_2 = 0
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 1
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      class_id_2 = 2
      D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # class_id_2 = 3
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
  
      # class_id = 3
      # N_hat = K.sum(y_true*eye[class_id])/K.sum(y_true)
      # class_id_2 = 0
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # class_id_2 = 1
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # class_id_2 = 2
      # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
      # # class_id_2 = 3
      # # D += K.sum(N_hat*((class_id-class_id_2)**n/(self.number_of_classes-1)**n)*y_pred[:, class_id_2])
  
      return(K.log(K.mean(N/D)))
  </code></pre>
  
  <p>My function was doing fine when working with a simple LSTM architecture built with an LSTM layer and two Dense layers.</p>
  
  <p>But when I tried to add an input Convolution layer for feature reduction, I faced this error :</p>
  
  <blockquote>
    <p>ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: 'Tensor("loss/dense_2_loss/strided_slice:0", shape=(?,), dtype=float32)'</p>
  </blockquote>
  
  <p>Which by the way pointed to the 4th line. As suggested in <a href="https://stackoverflow.com/questions/45111136/keras-custom-loss-function-dtype-error">Keras custom loss function dtype error</a>
  I tried to cast <code>y_true</code> and <code>y_pred</code> to <code>float64</code> - which didn't change anything.
  I then tried to cast to float64 <code>y_pred[:, class_id]</code>, but it just changed the line where the error was pointing at.
  Every time I was correcting the error an other one appeared until it pointed to this line:</p>
  
  <blockquote>
    <p>/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in weighted(y_true, y_pred, weights, mask) 451 # apply sample weighting 452 if weights is not None: --> 453 score_array *= weights 454 score_array /= K.mean(K.cast(K.not_equal(weights, 0), K.floatx())) 455 return K.mean(score_array)</p>
  </blockquote>
  
  <p>Does anyone have an idea? I emphasize the fact that my loss function was working properly before adding a Conv2D.</p>
  
  <p>Thanks in advance!</p>
  
  <p>PS : If you have any clue how to make my function clearer without any loop supported by the backend.</p>