I'm trying to do loss function in Keras as in Pytorch https://pytorch.org/docs/master/generated/torch.nn.MultiLabelMarginLoss.html
but it's taking a lot of time to do model.compile and after that it takes a very long time to train it (especially the first batch) (I'm using multi gpu)
maybe it's because the loops or the wrong use of Keras.backend,
Here is the code: (L is the number of classes)
def mlm_loss(y_true, y_pred):
loss=float(0)
a = tf.keras.backend.constant(1, dtype='float32')
for s in range(batch_size): # for each sample in batch
for i in range(L):
for j in range(L):
loss=loss + y_true[s][i]*(a-y_true[s][j])*(a-(y_pred[s][i]-y_pred[s][j])) #two conditions
l= tf.keras.backend.constant(L, dtype='float32')
loss=a/l*loss
return loss
thanks for the help