问题
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:
- The loss function seem to work flawless in eager execution mode where the initialization issue obviously does not exist.
- In eager execution mode the type of
y_true
seem to benp.array
and nottf.Tensor
(ortf.EagerTensor
at least). Does this mean that actuallyy_true
andy_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