# Keras Custom Loss Function

I am looking to design a custom loss function for Keras model. The model itself is neural network that accepts a set of images and is supposed to run a regression to get an output, which is a value. Due to the physical conditions of the problem, I need to add a regularization term which would calculate the $$cos(y_{pred})*f(X_i)$$, where $$y_{pred}$$ is the output of the neural network, $$X_i$$ is the training example used to calculate $$y_{pred}$$, $$f$$ is some function which would calculate a value based on the image.

My problem is how to get the $$X_i$$ from the model? Loss function is supposed to accept just two inputs: $$y_{pred}$$ and $$y_{true}$$ which are tensors.

• So your loss function is not equal to zero when $Y_{pred} = Y_{true}$? And also, if you have a function to calculate the loss using $X_i$, then you don't need a neural network. You can find a mathematical model that always generates expected output. Nov 4, 2019 at 23:53
• I am sorry for the confusion. It is not exactly like that. For $y_{true}$ from the data we have that $cos(y_{true})*f(X_i)$ is supposed to be approximately zero. So everything should work. I just need a way to somehow access $X_i$ from within the loss function. Nov 4, 2019 at 23:59
• You could do something like this stackoverflow.com/questions/51680818/… May 2, 2020 at 17:52

You should insert Xi into Y_true or Y_pred.

pseudocode is here.

Xi: np.array (10,256,256,1) Y_pred: np.array (10,256,256,1) Y_true: np.array (10,256,256,1)

input_Xi_Y_pred: np.array (10,256,256,2)

• [:,:,:,0] is Xi
• [:,:,:,1] is Y_pred

.

input_Xi_Y_pred = np.concatenate((Xi, Y_pred), axis=3))

def cosine_f_loss2(Y_true, input_Xi_Y_pred):
np_Y_true=np.copy(Y_true)
np_input_Xi_Y_pred=np.copy(input_Xi_Y_pred)
Xi=np_input_Xi_Y_pred[:,:,:,0]
return Your_calculation_cos_f (y_pred, Xi)


Data type of input is tf tensor. If You need to do numpy calculation, You have to change tf to numpy array. return should be numpy array

def cosine_f_loss1(Y_true, input_Xi_Y_pred):
return tf.py_function (cosine_f_loss2, inp=[Y_true, input_Xi_Y_pred], Tout=[tf.float32])

model.compile (loss = cosine_f_loss1)
model.fit (input_Xi_Y_pred, Y_true)