Tensorflow variable initialization inside loss function

左心房为你撑大大i 提交于 2021-02-11 04:52:39

问题


I have an object detection model implemented in tensorflow.keras (version 1.15). I am trying to implement a modified (hybrid) loss function in my model. Basically I need a few variables defined in my loss function because I am processing the y_true and y_pred provided in my classification loss function (a focal loss to be exact). So, I naturally resided in implementing my ops inside the loss function.

I have defined a WrapperClass to initialize my variables:

class LossWrapper(object):
    def __init__(self, num_centers):
        ...
        self.total_loss = tf.Variable(0, dtype=tf.float32)

def loss_funcion(self, y_true, y_pred):
    ...
    self.total_loss = self.total_loss + ...

I amd getting an error:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable

using self.total_loss_cosine = tf.zeros(1)[0] I am getting (a similar message):

tensorflow.python.framework.errors_impl.InvalidArgumentError: Retval[0] does not have value

I came to the conclusion that no matter how I define my variable or where I define it (I have tried inside the __init__ function or in the main function body) I am getting an error stating about attempting to use some uninitialized Variable.

I am starting to think that I cannot initialize variables inside my loss function and probably I should implement them as a typical block outside it. Is this the case? Is the loss functions basically separated from the rest of the network so the typical initialization does not work as expected?

Some remarks:

  1. The loss function seem to work flawless in eager execution mode where the initialization issue obviously does not exist.
  2. In eager execution mode the type of y_true seem to be np.array and not tf.Tensor (or tf.EagerTensor at least). Does this mean that actually y_true and y_pred are propagated as numpy array in general, meaning, that this part is actually detached from the network? (I have tested this on eager execution only though)

来源:https://stackoverflow.com/questions/64102364/tensorflow-variable-initialization-inside-loss-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!