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.

Topic keras loss-function neural-network

Category Data Science


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)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.