<p>It is possible to create a custom loss by subclassing <code>tf.keras.losses.Loss</code>. 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).</p>
<pre><code>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
</code></pre>
<p>Further you can pass it directly to the optimizer:</p>
<pre><code>f_loss = focal_loss(2.)
model.compile(loss=f_loss, optimizer='adam')
</code></pre>
<p>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.</p>
Can't input data to custom loss: Inputs to eager execution function cannot be Keras symbolic tensors
<p>When I'm testing my tensorflow keras custom loss(using additional input data to calculate loss), which is as follow:</p>
<pre><code>@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=&quot;transition_prob_to_unlabeled&quot;)
    transition_prob_to_labeled = tf.nn.softmax(tf.transpose(similarity),  name=&quot;transition_prob_to_labeled&quot;)

    roundtrip_prob = tf.matmul(transition_prob_to_unlabeled, transition_prob_to_labeled, name=&quot;roundtrip_prob&quot;)

    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')
</code></pre>
<p>I got a error massage:</p>
<pre><code>    _SymbolicException                        Traceback (most recent call last)
&lt;ipython-input-13-a0b380ce314d&gt; in &lt;module&gt;
     37 print(out)
     38 m = Model([inp,true, sample_weight], out)
---&gt; 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:
--&gt; 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.
--&gt; 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))),
-&gt; 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(
-&gt; 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,
--&gt; 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           &quot;Inputs to eager execution function cannot be Keras symbolic &quot;
---&gt; 74           &quot;tensors, but found {}&quot;.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 [&lt;tf.Tensor 'input_14:0' shape=(None, 10) dtype=float32&gt;, &lt;tf.Tensor 'dense_9/Identity:0' shape=(None, 10) dtype=float32&gt;, &lt;tf.Tensor 'input_15:0' shape=(None, 10) dtype=float32&gt;]
</code></pre>
<p>I follow the answer in <a href="https://stackoverflow.com/questions/62746381/custom-loss-problem-inputs-to-eager-execution-function-cannot-be-keras-symbolic">Custom loss problem: inputs to eager execution function cannot be keras symbolic tensors but found</a>, 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.</p>
<p>I don’t know which step made my function error.
What can I do to add this loss function to my model?</p>