Joe Urc
Joe Urc

Reputation: 487

Keras/Tensorflow custom loss function

I have a neural network built with Keras that I'm attempting to train. The output layer has 4 nodes. For the problem I'm trying to solve, I only want to compute the gradient on a single one of the output nodes based upon the true value. Basically, y_true will look like this [0,0,2,0] where the zeros represent nodes that should be ignored. y_pred however will be of the form [1.2,3.2,4.5,6]. I'd like to make it such that only the third index is taken into account in mse. This would require that I zero out index 0, 1, and 3 in y_pred. I haven't found a proper way to do this.

Below is code I've tried, but which returns NaN from the loss function.

def custom_mse(y_true, y_pred):

    return K.mean(K.square(tf.truediv(y_pred*y_true,y_true)-y_pred), axis=-1)

Is there a way to do this simple operation on these Tensor objects?

Upvotes: 0

Views: 2000

Answers (1)

David
David

Reputation: 41

doing it like this:

[1.2,3.2,4.5,6]*[0,0,2,0] = [0,0,9,0]
[0,0,9,0]/2 = [0,0,4.5,0]

and then continue normally.

This is the code to do that:

def custom_mse(y_true, y_pred):
     return K.mean(K.square(tf.divide(tf.multiply(y_pred, y_true),tf.reduce_max(y_true))-y_true), axis=-1)

Upvotes: 3

Related Questions