Borut Flis
Borut Flis

Reputation: 16415

How to add example based parameter to custom keras loss function?

I want to have custom loss function in keras, which has a parameter that is different for each training example.

from keras import backend as K

def my_mse_loss_b(b):
     def mseb(y_true, y_pred):
         return K.mean(K.square(y_pred - y_true)) + b
     return mseb

I read here that y_true and y_pred are always passed to the loss function so you need to create wrapper function.

model.compile(loss=my_mse_loss_b(df.iloc[:,2]), optimizer='adam', metrics=['accuracy'])

The problem is that when I fit the model there is an error as the function assumes the passed parameters will be as long as the batch. I on the other hand want each example to have there own parameter.

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [20] vs. [10000]
     [[node gradients/loss_2/dense_3_loss/mseb/weighted_loss/mul_grad/BroadcastGradientArgs (defined at C:\Users\flis1\Miniconda3\envs\Automate\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_keras_scratch_graph_1129]
Function call stack:
keras_scratch_graph

Incompatible shapes it says. 20 is the batch size and 10000 is the size of my train dataset and the size of all the parameters.

I can fit the model if I the parameter I add is the size of the batch, but as I said I want the parameter to be passed on an example basis.

Upvotes: 0

Views: 87

Answers (1)

Lescurel
Lescurel

Reputation: 11651

In your case, because your parameter b is tightly coupled to its training example, it would make sense to make it part of the ground truth. You could rewrite your loss function like the following:

def mseb(y_true, y_pred):
    y_t, b = y_true[0], y_true[1]
    return K.mean(K.square(y_pred - y_t)) + b

and then train your model with

model.compile(loss=mseb)
b = df.iloc[:,2]
model.fit(X,(y,b))

Upvotes: 1

Related Questions