I am using Keras with TensorFlow backend, and want to define a custom loss function like this:
- It computes the euclidean distance between the first 2 entries of y_true and y_pred
- It computes the absolute error between the rest of the entries
Then it takes the average.
I do this:
def custom_objective(y_true, y_pred):
y_true = backend.get_value(y_true)
y_pred = backend.get_value(y_pred)
a = np.sqrt(np.mean(np.square(y_pred[:2] - y_true[:2]), axis=-1))
b = np.sum(np.abs(y_true[2:] - y_pred[2:]))
return (a + b) / 5
I get an InvalidArgumentError when I compile the model:
model.compile(loss=custom_objective, optimizer='adam')