pythontensorflowkerasdeep-learningeager-execution

Can't input data to custom loss: Inputs to eager execution function cannot be Keras symbolic tensors


When I'm testing my tensorflow keras custom loss(using additional input data to calculate loss), which is as follow:

@tf.function
def build_walker_loss(labeled_output_t, unlabeled_output_t, label):
    similarity = tf.matmul(labeled_output_t, unlabeled_output_t, transpose_b=True)
    transition_prob_to_unlabeled = tf.nn.softmax(similarity,  name="transition_prob_to_unlabeled")
    transition_prob_to_labeled = tf.nn.softmax(tf.transpose(similarity),  name="transition_prob_to_labeled")

    roundtrip_prob = tf.matmul(transition_prob_to_unlabeled, transition_prob_to_labeled, name="roundtrip_prob")

    label = tf.reshape(label, [-1, 1])
    target_distribution = tf.cast(tf.equal(label, tf.transpose(label)),dtype=tf.float32)
    num_class = tf.compat.v1.reduce_sum(target_distribution, axis=1, keep_dims=True)
    target_distribution = target_distribution / num_class
    loss = tf.keras.losses.categorical_crossentropy(from_logits=False,
        y_true = target_distribution,
        y_pred = tf.math.log(1e-8 + roundtrip_prob),
    )
    print(loss)
    return loss

X = np.random.uniform(0,1, (1000,10))
y = np.random.uniform(0,1, 1000)
W = np.random.uniform(1,2, 1000)

inp = Input((10,))
true = Input((10,))
sample_weight = Input((10,))
x = Dense(32, activation='relu')(inp)
out = Dense(10)(x)
print(true)
print(out)
m = Model([inp,true, sample_weight], out)
m.add_loss( build_walker_loss( true, out, sample_weight ) )
m.compile(loss=None, optimizer='adam')

I got a error massage:

    _SymbolicException                        Traceback (most recent call last)
<ipython-input-13-a0b380ce314d> in <module>
     37 print(out)
     38 m = Model([inp,true, sample_weight], out)
---> 39 m.add_loss( build_walker_loss( true, out, sample_weight ) )
     40 m.compile(loss=None, optimizer='adam')
     41 # history = m.fit([X, y, W], y=None, epochs=10)

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
    578         xla_context.Exit()
    579     else:
--> 580       result = self._call(*args, **kwds)
    581 
    582     if tracing_count == self._get_tracing_count():

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
    648               *args, **kwds)
    649       # If we did not create any variables the trace we have is good enough.
--> 650       return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds)  # pylint: disable=protected-access
    651 
    652     def fn_with_cond(*inner_args, **inner_kwds):

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\function.py in _filtered_call(self, args, kwargs)
   1663          if isinstance(t, (ops.Tensor,
   1664                            resource_variable_ops.BaseResourceVariable))),
-> 1665         self.captured_inputs)
   1666 
   1667   def _call_flat(self, args, captured_inputs, cancellation_manager=None):

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1744       # No tape is watching; skip to running the function.
   1745       return self._build_call_outputs(self._inference_function.call(
-> 1746           ctx, args, cancellation_manager=cancellation_manager))
   1747     forward_backward = self._select_forward_and_backward_functions(
   1748         args,

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    596               inputs=args,
    597               attrs=attrs,
--> 598               ctx=ctx)
    599         else:
    600           outputs = execute.execute_with_cancellation(

E:\Anaconda3\envs\lrc\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     72       raise core._SymbolicException(
     73           "Inputs to eager execution function cannot be Keras symbolic "
---> 74           "tensors, but found {}".format(keras_symbolic_tensors))
     75     raise e
     76   # pylint: enable=protected-access

_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'input_14:0' shape=(None, 10) dtype=float32>, <tf.Tensor 'dense_9/Identity:0' shape=(None, 10) dtype=float32>, <tf.Tensor 'input_15:0' shape=(None, 10) dtype=float32>]

I follow the answer in Custom loss problem: inputs to eager execution function cannot be keras symbolic tensors but found, but without considering the correctness of the input data, when I change the mse loss to my own loss function, I still got this error.

I don’t know which step made my function error. What can I do to add this loss function to my model?


Solution

  • It is possible to create a custom loss by subclassing tf.keras.losses.Loss. The loss created in this way can be passed directly to the optimizer. Let me demonstrate this on an example of the focal loss (arXiv:1708.02002).

    class focal_loss(tf.keras.losses.Loss):
    
        # function to initilize loss parameters
        def __init__(self, gamma):
            super().__init__()
            self.gamma = gamma
        
        # function to evaluate loss 
        # must accept exectly 3 parameters: true labels, predicted labels, and, possible,samples weights
        # must return loss value
        def __call__(self, y_true, y_pred, sample_weight=None):
            entropy = tf.keras.losses.binary_crossentropy( y_true, y_pred )
            focal_weight = tf.reduce_sum( y_true*tf.math.pow((1-y_pred),self.gamma), axis=-1 )
            loss = tf.math.multiply(entropy,focal_weight)
            # use sample weights, if provided
            if sample_weight is not None:
                sample_weight = tf.squeeze(sample_weight)
                loss = tf.math.multiply(loss,sample_weight)
            loss = tf.math.reduce_sum( loss )
        return loss
    

    Further you can pass it directly to the optimizer:

    f_loss = focal_loss(2.)
    model.compile(loss=f_loss, optimizer='adam')
    

    If rewriting your loss in this way would not work, then it is clearly an implementation error (in a way you compute loss). A more carefull study will be needed.