I have a multilabel classification problem with K
labels and also I have a function, let's call it f
that for each example in the dataset takes in two matrices, let's call them H
and P
. Both matrices are part of the input data.
For each vector of labels y
(for one example), i.e. y
is a vector with dimension (K \times 1)
, I compute a scalar value f_out = f(H, P, y)
.
I want to define a loss function that minimizes the mean absolute percentage error between the two vectors formed by the values f_out_true = f(H, P, y_true)
and f_out_pred = f(H, P, y_pred)
for all examples.
Seeing the documentation of Keras, I know that customized loss function comes in the form custmLoss(y_pred, y_true)
, however, the loss function I want to define depends on the input data and these values f_out_true
and f_out_pred
need to be computed example by example to form the two vectors that I want to minimize the mean absolute percentage error.
As far as I am aware, there is no way to make a loss function that takes anything other than the model output and the corresponding ground truth. So, the only way to do what you want is to make the input part of your model's output. To do this, simply build your model with the functional API, and then add the input tensor to the list of outputs:
input = Input(input_shape)
# build the rest of your model with the standard functional API here
# this example model was taken from the Keras docs
x = Dense(100, activation='relu')(input)
x = Dense(100, activation='relu')(x)
x = Dense(100, activation='relu')(x)
output = Dense(10, activation='softmax')(x)
model = Model(inputs=[input], outputs=[output, input])
Then, make y_true
a combination of your input data and the original ground truth.
I don't have a whole lot of experience with the functional API so it's hard to be more specific, but hopefully this points you in the right direction.