0

First, I would like to say that I only have little experience in Keras/Tensorflow and probably lack some understanding on tensors manipulations.

I am using a model which input is an "oversized" matrix (NxN). That is, I feed it with data that can be smaller (ie. (KxK), K <= N) where "missing" data (to fit the NxN shape) is filled with zeros. The output is an encoded version (Nx2) of the input.

I'm using a custom loss function that I would like to be computed only on the (Kx2) first values of the model's output. To do so, I think the solution is to "slice" the y_pred tensor in my loss function since I don't want to simply mask it with a boolean tensor. However, I can't figure out how to pass K as a dynamic argument to my custom loss.

  • Wrapping the function within another function that takes an argument does not fit my needs since the K value will change on each data sample
  • Passing K in the model's input and getting it back through a function wrapp (eg. https://stackoverflow.com/a/55445837/6315123) as mentionned in the first point does not work either, since slices cannot be computed from Tensor (as far as I understand); and evaluate the tensor within the loss function doesn't seem possible.

How can I pass such an argument to my loss function ?

Thanks !

Loann Gio
  • 65
  • 5
  • what do you mean: slices can not be computed from tensor ? It is not a problem to slice tensor: new_tensor = old_tensor[:K, :K] – Andrey Nov 09 '20 at 18:18
  • I mean that you can't do new_tensor = old_tensor[:K, :K] if K is a Tensor (eg.if K is the result of a K.sum operation, or passed from a Model layer (such as Input)). The problem is to define K dynamically (ie. for each data sample) in the loss function in a way that enables slice, which is not possible when K is a tensor. – Loann Gio Nov 09 '20 at 23:53

0 Answers0