TypeError: To be compatible with tf.contrib.eager.defun, Python functions must return zero or more Tensors; in compilation of <function tf_if_stmt.<locals>.error_checking_body at 0x7f55400e3c80>, found return value of type <class 'tensorflow.python.keras.losses.MeanSquaredError'>, which is not a Tensor.
Author: Misoknisky
Pull request: no
Labels: name: wontfix

INFO:tensorflow:Error reported to Coordinator: in converted code:

    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/transformers/trainer_tf.py:511 _forward  *
        per_example_loss, _ = self._run_model(features, labels, True)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/transformers/trainer_tf.py:534 _run_model  *
        outputs = self.model(features, labels=labels, training=training)[:2]
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:778 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/transformers/modeling_tf_roberta.py:530 call  *
        loss = self.compute_loss(labels, reshaped_logits)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/transformers/modeling_tf_utils.py:135 compute_loss  *
        if shape_list(logits)[1] == 1:
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/autograph/operators/control_flow.py:918 if_stmt
        basic_symbol_names, composite_symbol_names)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/autograph/operators/control_flow.py:956 tf_if_stmt
        error_checking_orelse)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/util/deprecation.py:507 new_func
        return func(*args, **kwargs)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/control_flow_ops.py:1174 cond
        return cond_v2.cond_v2(pred, true_fn, false_fn, name)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/cond_v2.py:83 cond_v2
        op_return_value=pred)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py:983 func_graph_from_py_func
        expand_composites=True)
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py:568 map_structure
        structure[0], [func(*x) for x in entries],
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py:568 <listcomp>
        structure[0], [func(*x) for x in entries],
    /data0/liuyongkang/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py:943 convert
        (str(python_func), type(x)))

    TypeError: To be compatible with tf.contrib.eager.defun, Python functions must return zero or more Tensors; in compilation of <function tf_if_stmt.<locals>.error_checking_body at 0x7f55400e3c80>, found return value of type <class 'tensorflow.python.keras.losses.MeanSquaredError'>, which is not a Tensor.

Comments:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


I encounter one case: `if object: do something` will trigger this error, while if you use the condition `if object is not None` you will get through it.

Did you solved it?

> I encounter one case: `if object: do something` will trigger this error, while if you use the condition `if object is not None` you will get through it.

Thanks, you are the hero.