1

I am having trouble with Keras Custom loss function. I want to be able to access truth as a numpy array. Because it is a callback function, I think I am not in eager execution, which means I can't access it using the backend.get_value() function. i also tried different methods, but it always comes back to the fact that this 'Tensor' object doesn't exist.

Do I need to create a session inside the custom loss function ?

I am using Tensorflow 2.2, which is up to date.

def custom_loss(y_true, y_pred):

    # 4D array that has the label (0) and a multiplier input dependant
    truth = backend.get_value(y_true)

    loss = backend.square((y_pred - truth[:,:,0]) * truth[:,:,1])
    loss = backend.mean(loss, axis=-1)  

    return loss

 model.compile(loss=custom_loss, optimizer='Adam')
 model.fit(X, np.stack(labels, X[:, 0], axis=3), batch_size = 16)


I want to be able to access truth. It has two components (Label, Multiplier that his different for each item. I saw a solution that is input dependant, but I am not sure how to access the value. Custom loss function in Keras based on the input data

  • What do you want to do with the numpy array inside the custom loss function? You don't do anything with `truth` there. What are `weight_building` and `weight_space` actually doing? Can you show us how you compile the model? Do you get the error if you remove `truth=backend.get_value(y_true)`? – Tinu May 12 '20 at 21:37
  • I simplified it and edited it. – Mathieu Châteauvert May 12 '20 at 21:56

1 Answers1

1

I think you can do this by enabling run_eagerly=True in model.compile as shown below.

model.compile(loss=custom_loss(weight_building, weight_space),optimizer=keras.optimizers.Adam(), metrics=['accuracy'],run_eagerly=True)

I think you also need to update custom_loss as shown below.

def custom_loss(weight_building, weight_space):
  def loss(y_true, y_pred):
    truth = backend.get_value(y_true)
    error = backend.square((y_pred - y_true))
    mse_error = backend.mean(error, axis=-1) 
    return mse_error
  return loss

I am demonstrating the idea with a simple mnist data. Please take a look at the code here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vishnuvardhan Janapati
  • 3,088
  • 1
  • 16
  • 25
  • This looks like a good answer to me. A couple of tips: (a) we prefer posts here not to ask for acceptance/upvotes. If the material is good it will get votes organically. It is OK to add a comment to explain _how_ to use the voting/acceptance system, or a gentle reminder if someone has forgotten, but go easy on it. (b) there's no need to add invitations to comment, readers should know this. Keep your material succinct if you can - this keeps it nice and readable for future readers too. – halfer May 13 '20 at 11:55
  • 1
    Thanks @halfer edits and suggestions. Will follow from now onwards. – Vishnuvardhan Janapati May 13 '20 at 12:30
  • This is what I was expecting – Mathieu Châteauvert May 13 '20 at 13:21