tensorflow / addons

Useful extra functionality for TensorFlow 2.x maintained by SIG-addons
Apache License 2.0
1.69k stars 610 forks source link

tfa.metrics.RSquare: ValueError: Shapes must be equal rank, but are 0 and 1 for 'AssignAddVariableOp' (op: 'AssignAddVariableOp') with input shapes: [], [1]. #1755

Open karlzhang-hhg opened 4 years ago

karlzhang-hhg commented 4 years ago

System information

Describe the bug Find a bug in tensorflow_addons: In version 0.9.1, the following code wour report error:

                import tensorflow as tf
                import tensorflow_addons as tfa
                actuals = tf.constant([[1], [4], [3]], dtype=tf.float32)
                preds = tf.constant([[2], [4], [4]], dtype=tf.float32)
                print(actuals, preds)
                result = tfa.metrics.RSquare(dtype=tf.float32)
                result.update_state(actuals, preds)
                print('R^2 score is: ', result, result.result().numpy()) # 0.57142866
ValueError: Shapes must be equal rank, but are 0 and 1 for 'AssignAddVariableOp' (op: 'AssignAddVariableOp') with input shapes: [], [1].

However in version 0.8.3, the above code works fine.

A clear and concise description of what the bug is.

Code to reproduce the issue

                import tensorflow as tf
                import tensorflow_addons as tfa
                actuals = tf.constant([[1], [4], [3]], dtype=tf.float32)
                preds = tf.constant([[2], [4], [4]], dtype=tf.float32)
                print(actuals, preds)
                result = tfa.metrics.RSquare(dtype=tf.float32)
                result.update_state(actuals, preds)
                print('R^2 score is: ', result, result.result().numpy()) # 0.57142866

Provide a reproducible test case that is the bare minimum necessary to generate the problem.

Other info / logs

>>> result.update_state(actuals, preds)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/metrics_utils.py", line 76, in decorated
    update_op = update_state_fn(*args, **kwargs)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 568, in __call__
    result = self._call(*args, **kwds)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 615, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 497, in _initialize
    *args, **kwds))
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 439, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 968, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

    /home/user/.local/lib/python3.7/site-packages/tensorflow_addons/metrics/r_square.py:115 update_state  *
        self.sum.assign_add(tf.reduce_sum(weighted_y_true, axis=0))
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:786 assign_add
        name=name)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_resource_variable_ops.py:56 assign_add_variable_op
        "AssignAddVariableOp", resource=resource, value=value, name=name)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py:742 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py:595 _create_op_internal
        compute_device)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:3322 _create_op_internal
        op_def=op_def)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1786 __init__
        control_input_ops)
    /home/user/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1622 _create_c_op
        raise ValueError(str(e))

    ValueError: Shapes must be equal rank, but are 0 and 1 for 'AssignAddVariableOp' (op: 'AssignAddVariableOp') with input shapes: [], [1].

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

failure-to-thrive commented 4 years ago

The correct line of code:

result = tfa.metrics.RSquare(dtype=tf.float32, y_shape=(1,))

https://github.com/tensorflow/addons/pull/1310 provides a bit more info why.

harahu commented 4 years ago

@failure-to-thrive describes the intended use. To ease the use of this metric one could possibly implement some opportunistic flattening where this does not lead to ambiguity, like in this case. Not sure if this is worth it. I recognize that this implementation requires some thinking to use correctly, so I raised an issue in tensorflow core that should hopefully in time lead to a more user friendly implementation of RSquare. https://github.com/tensorflow/tensorflow/issues/40195

ydennisy commented 3 years ago

Hi @harahu @failure-to-thrive I am having a similar issue even with the extra args being passed in:

ValueError: Shapes must be equal rank, but are 1 and 0 for '{{node ranking_14/AssignAddVariableOp_2}} = AssignAddVariableOp[dtype=DT_FLOAT](ranking_14/AssignAddVariableOp_2/resource, ranking_14/Sum_1)' with input shapes: [], [].
GF-Huang commented 3 years ago

So how to solve it?

ValueError: Shapes must be equal rank, but are 0 and 1 for '{{node AssignAddVariableOp_2}} = AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_2/resource, Sum_2)' with input shapes: [], [?].

And the docs does not say what the y_shape mean.

GF-Huang commented 3 years ago

Nobody can solve this?

failure-to-thrive commented 3 years ago

Guys, please create a New Issue strictly following the guidelines to provide us a code to reproduce the issue, TF/TFA versions, etc. If you feel your issue somehow related to this one, just refer to it. Simply throwing us error messages will not help in any way, sorry.

Jichen66 commented 2 years ago

So how to solve it?

ValueError: Shapes must be equal rank, but are 0 and 1 for '{{node AssignAddVariableOp_2}} = AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_2/resource, Sum_2)' with input shapes: [], [?].

And the docs does not say what the y_shape mean.

I just came across this issue, and have no idea how to solve it at the moment. Have you solved it and could you give me some hints? Many thx in advance!

montali commented 2 years ago

The correct line of code:

result = tfa.metrics.RSquare(dtype=tf.float32, y_shape=(1,))

1310 provides a bit more info why.

@Jichen66 this fixed it for me! Usage in a model:

model.compile(loss='mse', optimizer='adam', metrics=[tfa.metrics.RSquare(dtype=tf.float32, y_shape=(1,))])