Karl Alexius
Karl Alexius

Reputation: 353

Keras: averaging different loss functions

I am using Keras with TensorFlow backend, and want to define a custom loss function like this:

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')

Upvotes: 1

Views: 239

Answers (1)

nuric
nuric

Reputation: 11225

You are using NumPy on Keras tensors, unfortunately that is a deadly combination. What you are looking for is something along the lines of:

def custom_objective(y_true, y_pred):
  a = K.sqrt(K.mean(K.square(y_pred[:2] - y_true[:2]), axis=-1))
  b = K.sum(K.abs(y_true[2:] - y_pred[2:]))
  return (a + b) / 5 # these operators work on tensors

Upvotes: 1

Related Questions