Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a custom loss function without y_true in Keras

Tags:

python

keras

I'm implementing a triplet loss function in Keras. In general, loss functions take predicted values with ground truth as arguments. But triplet loss doesn't use labels, just the output. I tried to write the function with just one parameter:

def triplet_loss(y_pred):
    margin = 1
    return K.mean(K.square(y_pred[0]) - K.square(y_pred[1]) + margin)

It failed saying triplet_loss() takes 1 argument but two arguments were given (in score_array = fn(y_true, y_pred). When I write the function with two arguments y_true, y_pred, the program runs without error. Why is that? Should I just implement this function with these two arguments although y_true won't be used? Is this correct or is there another way of doing it?

like image 432
T.Poe Avatar asked Oct 17 '22 02:10

T.Poe


2 Answers

Well.... simply don't use the ground truth:

def triplet_loss(y_true,y_pred):
    #all your code as it is.

It's not very usual to have networks trained without ground truth. When we expect it to learn something, there is very very often a ground truth. If you don't, simply ignore it.

Also, if y_true is ignored, what are you passing to the fit method? Just a dummy array?

like image 186
Daniel Möller Avatar answered Oct 21 '22 08:10

Daniel Möller


implement it via K.function method.

output_tensor = your_model(input_tensor)
total_loss = K.mean( K.abs (input_tensor - output_tensor) )

nn_train = K.function ([input_tensor],[total_loss], 
                Adam(lr=5e-5, beta_1=0.5, beta_2=0.999).get_updates(total_loss, your_model.trainable_weights)

loss, = nn_train ([input])
like image 29
iperov Avatar answered Oct 21 '22 09:10

iperov