I'm using a custom loss function in Keras. This is the function:

def custom_loss(groups_id_count):
  def listnet_loss(real_labels, predicted_labels):
    losses = tf.placeholder(shape=[None], dtype=tf.float32) # Tensor of rank 1
    for group in groups_id_count:
      start_range = 0
      end_range = (start_range + group[1])
      batch_real_labels = tf.slice(real_labels, [start_range, 1, None], [end_range, 1, None])
      batch_predicted_labels = tf.slice(predicted_labels, [start_range, 0, 0], [end_range, 0, 0])
      loss = -K.sum(get_top_one_probability(batch_real_labels)) * tf.math.log(get_top_one_probability(batch_predicted_labels))
      losses = tf.concat([losses, loss], axis=0)
      start_range = end_range
    return K.mean(losses)
  return listnet_loss

I would get real_labelsand predicted_labelsitems from start_range to end_range, but the current code returns an exception:


TypeError: Failed to convert object of type <class 'list'> to Tensor.
Contents: [0, 1, None]. Consider casting elements to a supported type.

I don't know what to do because it's my first experience with TensorFlowand Keras. How can I get the items using indexing on tensor? Thanks in advance.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.