1

I am trying to use the input layer to compute a residue and have the loss function based on that residue. I am trying to use the method described in this answer, however when i try to call the fit method i get this error:

TypeError: in user code:

    c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function  *
        return step_function(self, iterator)
    c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    c:\python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    c:\python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    c:\python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step  **
        outputs = model.train_step(data)
    c:\python38\lib\site-packages\tensorflow\python\keras\engine\training.py:755 train_step
        loss = self.compiled_loss(
    c:\python38\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:237 __call__
        self._loss_metric.update_state(
    c:\python38\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:90 decorated
        update_op = update_state_fn(*args, **kwargs)
    c:\python38\lib\site-packages\tensorflow\python\keras\metrics.py:177 update_state_fn
        return ag_update_state(*args, **kwargs)
    c:\python38\lib\site-packages\tensorflow\python\keras\metrics.py:363 update_state  **
        sample_weight = weights_broadcast_ops.broadcast_weights(
    c:\python38\lib\site-packages\tensorflow\python\ops\weights_broadcast_ops.py:155 broadcast_weights
        values = ops.convert_to_tensor(values, name="values")
    c:\python38\lib\site-packages\tensorflow\python\profiler\trace.py:163 wrapped
        return func(*args, **kwargs)
    c:\python38\lib\site-packages\tensorflow\python\framework\ops.py:1540 convert_to_tensor
        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    c:\python38\lib\site-packages\tensorflow\python\framework\constant_op.py:339 _constant_tensor_conversion_function
        return constant(v, dtype=dtype, name=name)
    c:\python38\lib\site-packages\tensorflow\python\framework\constant_op.py:264 constant
        return _constant_impl(value, dtype, shape, name, verify_shape=False,
    c:\python38\lib\site-packages\tensorflow\python\framework\constant_op.py:281 _constant_impl
        tensor_util.make_tensor_proto(
    c:\python38\lib\site-packages\tensorflow\python\framework\tensor_util.py:435 make_tensor_proto
        values = np.asarray(values)
    c:\python38\lib\site-packages\numpy\core\_asarray.py:83 asarray
        return array(a, dtype, copy=False, order=order)
    c:\python38\lib\site-packages\tensorflow\python\keras\engine\keras_tensor.py:273 __array__
        raise TypeError(

    TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

This is the loss function i use

def custom_loss(inputs):
    def loss(y_true, y_pred):
        residue = y_true - inputs
        return 1/2 * tf.math.squared_difference(residue, y_pred)
    return loss

And this is the way i compile the model:

opt = tf.keras.optimizers.RMSprop(learning_rate=0.1, clipnorm=1)
model.compile(optimizer=opt,
              loss=custom_loss(inputs), 
              metrics=[PSNR, SSIM])

The error arises when i call model.fit(). I want to point out that i am using a generator for a dataset, but I have been using the same approach with different models, that didn't require a custom loss function, and everything works well. It seems that the problem arises because i am using the keras.Input() layer in the loss function, but i don't know how to circumvent this error.

Ghosty Frosty
  • 159
  • 2
  • 12
  • 1
    Please check what `K.sum` actually does. Hint: It does _not_ add two inputs. – xdurch0 Dec 29 '20 at 12:22
  • @xdurch0 that makes a lot of sense, and it clears up some things, but i get a new issue if i use + or tf.math.add(). I'll update the question to make up for this – Ghosty Frosty Dec 29 '20 at 12:34

0 Answers0