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

  keras, loss-function, python, tensorflow

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 ]] [Op:__inference_keras_scratch_graph_1129]
Function call stack:

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.

Source: Python Questions