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?
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?
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With