Search code examples
pythontensorflowkerasloss-function

Create keras tensor with shape as same as model output for custom loss function


I have a keras model with output shape of the last layer is (None,574,6), which None is my batch size feed into the model.

I also have a 2D numpy array called anchors with shape (574,6).

What I want is my output of each data minus that numpy array element wise.

import keras.backend as K

anchor_tensor = K.cast(anchors, tf.float32)
print(K.int_shape(anchor_tensor))
#(576, 4)
print(K.int_shape(y_pred))
#(None, 574, 6)
y_pred - anchor_tensor

The above code occured the following error due to the batch_size is unknown.

InvalidArgumentError: Dimensions must be equal, but are 574 and 576 for 'sub_6' (op: 'Sub') with input shapes: [?,574,6], [576,4].

During handling of the above exception, another exception occurred:

How can I repeat anchor_tensor None times to make its shape as the same as y_pred?


Solution

  • Tensorflow will easily do what it calls "broadcasting", which is automatically repeating the missing elements if possible. But for this to happen, it must confirm that the shapes allow that first.

    The safest way to assure the shapes are compatible, is to make them have the same length, and have value 1 in the dimension you want it to repeat.

    So, it's as simple as:

    anchor_tensor = K.expand_dims(anchor_tensor, axis=0) #new shape is (1, 576, 4)   
    result = y_pred - anchor tensor
    

    Now Tensorflow can match the shapes and will repeat the tensor for the entire batch size.