0

I was writing the loss function assuming that the losses are calculated for doubles, not for tensors. Here is my function:

def prediction_loss(a,b):
    IGNORE=.0025
    EPSILON=.00001  

    if IGNORE > abs(a) and IGNORE > abs(b) and np.sign(a)==np.sign(b):
        return 0

    scale=min(abs(a),abs(b))

    distance=abs(a-b)

    if abs(scale)<EPSILON:
        scale=max(abs(a),abs(b))
        if abs(scale)<EPSILON:
            scale=1
            distance**=2

    return min(distance,distance/scale)

when I use it in model.compile, I get following error:

OperatorNotAllowedInGraphError            Traceback (most recent call last)
<ipython-input-44-92af3f50a682> in <module>()
      9     keras.layers.Dense(1)
     10 ])
---> 11 model.compile(loss=prediction_loss, optimizer=keras.optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True))

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, distribute, **kwargs)
    371 
    372       # Creates the model loss and weighted metrics sub-graphs.
--> 373       self._compile_weights_loss_and_weighted_metrics()
    374 
    375       # Functions for train, test and predict will

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _compile_weights_loss_and_weighted_metrics(self, sample_weights)
   1651       #                   loss_weight_2 * output_2_loss_fn(...) +
   1652       #                   layer losses.
-> 1653       self.total_loss = self._prepare_total_loss(masks)
   1654 
   1655   def _prepare_skip_target_masks(self):

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _prepare_total_loss(self, masks)
   1711 
   1712           if hasattr(loss_fn, 'reduction'):
-> 1713             per_sample_losses = loss_fn.call(y_true, y_pred)
   1714             weighted_losses = losses_utils.compute_weighted_loss(
   1715                 per_sample_losses,

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/keras/losses.py in call(self, y_true, y_pred)
    219       y_pred, y_true = tf_losses_util.squeeze_or_expand_dimensions(
    220           y_pred, y_true)
--> 221     return self.fn(y_true, y_pred, **self._fn_kwargs)
    222 
    223   def get_config(self):

<ipython-input-43-4630edd6290a> in prediction_loss(a, b)
     14     EPSILON=.00001
     15 
---> 16     if IGNORE > abs(a) and IGNORE > abs(b) and np.sign(a)==np.sign(b):
     17         return 0
     18 

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __bool__(self)
    763       `TypeError`.
    764     """
--> 765     self._disallow_bool_casting()
    766 
    767   def __nonzero__(self):

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_bool_casting(self)
    532     else:
    533       # Default: V1-style Graph execution.
--> 534       self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
    535 
    536   def _disallow_iteration(self):

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_in_graph_mode(self, task)
    521     raise errors.OperatorNotAllowedInGraphError(
    522         "{} is not allowed in Graph execution. Use Eager execution or decorate"
--> 523         " this function with @tf.function.".format(task))
    524 
    525   def _disallow_bool_casting(self):

OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

Aparently, tensorflow is passing tf.Tensor as params a and b, and those cannot be used in logic operations. What should I change for the function to work properly? I want to ignore a and b of small sizes that have the same sign

1
  • What does a and b mean in your code? It should be y_true and y_pred as required. Commented Oct 4, 2019 at 3:39

1 Answer 1

1

Yes, tf.tensor cannot use python bool. Use keras.backend.switch() for conditional statement.

Please refer to the below link for the usage :

https://keras.io/backend/

It has all the functions listed which you can use to fit in your equation like greater, greater_than, equal etc.

Change your statement "if IGNORE > abs(a) and IGNORE > abs(b) and np.sign(a)==np.sign(b):" using the keras backend functionalities and it should solve your issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.