Home > Net >  Tensorflow loss function no gradient provided
Tensorflow loss function no gradient provided

Time:12-04

Currently I try to code my own loss function, but when returning the result (a tensor that consists of a list with the loss values) I get the following error:

ValueError: No gradients provided for any variable: ['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'dense/kernel:0', 'dense/bias:0', 'dense_1/kernel:0', 'dense_1/bias:0', 'dense_2/kernel:0', 'dense_2/bias:0'].

However in tutorials and in their docs they also use tf.recude_mean and when using it like them (they showed how to code mse loss function) I dont get the error, so it seems that I am missing something

My code:

gl = tfa.losses.GIoULoss()
def loss(y_true, y_pred):
        batch_size = y_true.shape[0]
        # now contains 32 lists (a batch) of bbxs -> shape is (32, 7876)
        bbx_true = y_true.numpy()

        # now contains 32 lists (a batch) of bbxs here we have to double access [0] in order to get the entry itself 
        # -> shape is (32, 1, 1, 7876)
        bbx_pred = y_pred.numpy()

        losses = []
        curr_true = []
        curr_pred = []
        for i in range(batch_size):
            curr_true = bbx_true[i] 
            curr_pred = bbx_pred[i][0][0]


            curr_true = [curr_true[x:x 4] for x in range(0, len(curr_true), 4)]
            curr_pred = [curr_pred[x:x 4] for x in range(0, len(curr_pred), 4)]

            if len(curr_true) == 0:
                curr_true.append([0., 0.,0.,0.])

            curr_loss = gl(curr_true, curr_pred)

            losses.append(curr_loss)

        return tf.math.reduce_mean(losses, axis=-1)

Basically I want to achive bounding box regression and because of that I want to use the GIoUloss loss function. Because my model outputs 7896 neurons (the max amount of bounding boxes I want to predict according to my training set times 4) and the gioloss function needs the input as an array of lists with 4 elements each, I have to perform this transformation.

How do I have to change my code in order to also build up a gradient

CodePudding user response:

Numpy don't provide autograd functions so you need to have Tensorflow tensors exclusively in your loss (otherwise the gradient is lost during backpropagation). So avoid using .numpy() and use the tensorflow operators and slicing on tensoflow tensors instead.

  • Related