Skip to content Skip to sidebar Skip to footer

Reshape Tensor In Custom Loss Function

I have a problem similar to this question. I am trying to devise a loss function in keras given as: def depth_loss_func(lr): def loss(actual_depth,pred_depth): actual_s

Solution 1:

I managed to reproduce you exception with a Tensor of shape (None, None, None, 9), when calling np.prod() like this:

from keras import backend as K

#create tensor placeholder
z = K.placeholder(shape=(None, None, None, 9))
#obtain its static shape with int_shape from Keras
actual_shape = K.int_shape(z)
#obtain product, error fires here... TypeError between None and None
dim = np.prod(actual_shape[1:])

This happens because you are trying to multiply two elements of type None, even though you sliced your actual_shape (as more than 1 elements where None). In some cases you can even get TypeError between None and int, if only one none-type element remains after slicing.

Taking a look at the answer you mentioned, they specify what to do in those situations, quoting from it:

For the cases where more than 1 dimension are undefined, we can use tf.shape() with tf.reduce_prod() alternatively.

Based on that, we can translate those operations to the Keras API, by using K.shape() (docs) and K.prod() (docs), respectively:

z = K.placeholder(shape=(None, None, None, 9))
#obtain Real shape and calculate dim with prod, no TypeError this timedim = K.prod(K.shape(z)[1:])
#reshapez2 = K.reshape(z, [-1,dim])

Also, for the case where only one dimension is undefined remember to use K.int_shape(z) or its wrapper K.get_variable_shape(z) instead of just get_shape(), as also defined in the backend (docs). Hope this solves your problem.

Post a Comment for "Reshape Tensor In Custom Loss Function"