Skip to content

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

Description

@karlzhang-hhg

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): CentOS Linux release 7.4.1708 (Core)
  • TensorFlow version and how it was installed (source or binary): 2.1.0
  • TensorFlow-Addons version and how it was installed (source or binary): 0.9.1
  • Python version: 3.7.4
  • Is GPU used? (yes/no): no

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedNeeds help as a contributionmetrics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions