Changing the tensor in a custom loss function

I have a problem similar to this question . I am trying to create a loss function in cerams, indicated as:

def depth_loss_func(lr):
    def loss(actual_depth,pred_depth):
        actual_shape = actual_depth.get_shape().as_list()
        dim = np.prod(actual_shape[1:])
        actual_vec = K.reshape(actual_depth,[-1,dim])
        pred_vec = K.reshape(pred_depth,[-1,dim])
        di = K.log(pred_vec)-K.log(actual_vec)
        di_mean = K.mean(di)
        sq_mean = K.mean(K.square(di))

        return (sq_mean - (lr*di_mean*di_mean))
    return loss

based on the answer given in this question . However, I get an error message:

 TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

In particular, this operator gives the following conclusion

(Pdb) actual_depth.get_shape()
TensorShape([Dimension(None), Dimension(None), Dimension(None)])

Backend - TensorFlow. Thank you for your help.

+4
source share
1 answer

I was able to reproduce the exception with the form tensor (None, None, None, 9)when called np.prod()as follows:

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:])

, None, actual_shape ( 1 , None). TypeError None int, -.

, , , , :

, 1 undefined, tf.shape() tf.reduce_prod().

, API Keras, K.shape() (docs) K.prod() (docs), :

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

, , undefined K.int_shape(z) K.get_variable_shape(z) get_shape(), (docs). , .

+1

Source: https://habr.com/ru/post/1681718/


All Articles