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.

  • $\begingroup$ 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. $\endgroup$
    – aminrd
    Nov 4, 2019 at 23:53
  • $\begingroup$ 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. $\endgroup$ Nov 4, 2019 at 23:59
  • $\begingroup$ You could do something like this stackoverflow.com/questions/51680818/… $\endgroup$ May 2, 2020 at 17:52

1 Answer 1


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):
    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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.