Skip to content

ValueError when using AUC metric with multi label flag #35852

@dmirecki

Description

@dmirecki

System information

  • OS Platform and Distribution: Linux Ubuntu 18.04
  • TensorFlow installed from pip
  • TensorFlow version 2.1.0
  • Python version: 3.7
  • CUDA/cuDNN version: CUDA10.1 and cnDNN7.6
  • GPU model and memory: Tesla K80

Describe the current behavior
During the compilation of a model the below error occurs. The error is duo to the use of the tf.keras.metrics.AUC class with the multi_label=True option used. When multi_label=False, the model compiles without errors.

Describe the expected behavior
Model should compiles without errors.

Code to reproduce the issue

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import SGD

inputs = Input(shape=(10,))
output = Dense(3, activation="sigmoid")(inputs)

model = Model(
    inputs=inputs, 
    outputs=output
)

model.compile(
    loss='binary_crossentropy',
    optimizer=SGD(lr=1e-3, momentum=0.9), 
    metrics=[tf.keras.metrics.AUC(multi_label=True)]
)

Other info / logs

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py in zeros(shape, dtype, name)
   2439         shape = constant_op._tensor_shape_tensor_conversion_function(
-> 2440             tensor_shape.TensorShape(shape))
   2441       except (TypeError, ValueError):

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py in _tensor_shape_tensor_conversion_function(s, dtype, name, as_ref)
    333     raise ValueError(
--> 334         "Cannot convert a partially known TensorShape to a Tensor: %s" % s)
    335   s_list = s.as_list()

ValueError: Cannot convert a partially known TensorShape to a Tensor: (200, None)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-1-dc556804b7cd> in <module>
     15     loss='binary_crossentropy',
     16     optimizer=SGD(lr=1e-3, momentum=0.9),
---> 17     metrics=[tf.keras.metrics.AUC(multi_label=True)]
     18 )

~/miniconda3/envs/autotagging/lib/python3.7/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

~/miniconda3/envs/autotagging/lib/python3.7/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)
    437           targets=self._targets,
    438           skip_target_masks=self._prepare_skip_target_masks(),
--> 439           masks=self._prepare_output_masks())
    440 
    441       # Prepare sample weight modes. List with the same length as model outputs.

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _handle_metrics(self, outputs, targets, skip_target_masks, sample_weights, masks, return_weighted_metrics, return_weighted_and_unweighted_metrics)
   2002           metric_results.extend(
   2003               self._handle_per_output_metrics(self._per_output_metrics[i],
-> 2004                                               target, output, output_mask))
   2005         if return_weighted_and_unweighted_metrics or return_weighted_metrics:
   2006           metric_results.extend(

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _handle_per_output_metrics(self, metrics_dict, y_true, y_pred, mask, weights)
   1953       with K.name_scope(metric_name):
   1954         metric_result = training_utils.call_metric_function(
-> 1955             metric_fn, y_true, y_pred, weights=weights, mask=mask)
   1956         metric_results.append(metric_result)
   1957     return metric_results

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py in call_metric_function(metric_fn, y_true, y_pred, weights, mask)
   1153 
   1154   if y_pred is not None:
-> 1155     return metric_fn(y_true, y_pred, sample_weight=weights)
   1156   # `Mean` metric only takes a single value.
   1157   return metric_fn(y_true, sample_weight=weights)

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/metrics.py in __call__(self, *args, **kwargs)
    194     from tensorflow.python.keras.distribute import distributed_training_utils  # pylint:disable=g-import-not-at-top
    195     return distributed_training_utils.call_replica_local_fn(
--> 196         replica_local_fn, *args, **kwargs)
    197 
    198   @property

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py in call_replica_local_fn(fn, *args, **kwargs)
   1133     with strategy.scope():
   1134       return strategy.extended.call_for_each_replica(fn, args, kwargs)
-> 1135   return fn(*args, **kwargs)
   1136 
   1137 

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/metrics.py in replica_local_fn(*args, **kwargs)
    177     def replica_local_fn(*args, **kwargs):
    178       """Updates the state of the metric in a replica-local context."""
--> 179       update_op = self.update_state(*args, **kwargs)  # pylint: disable=not-callable
    180       with ops.control_dependencies([update_op]):
    181         result_t = self.result()  # pylint: disable=not-callable

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/metrics_utils.py in decorated(metric_obj, *args, **kwargs)
     74 
     75     with tf_utils.graph_context_for_symbolic_tensors(*args, **kwargs):
---> 76       update_op = update_state_fn(*args, **kwargs)
     77     if update_op is not None:  # update_op will be None in eager execution.
     78       metric_obj.add_update(update_op)

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/metrics.py in update_state(self, y_true, y_pred, sample_weight)
   1883     deps = []
   1884     if not self._built:
-> 1885       self._build(y_true.shape)
   1886 
   1887     if self.multi_label or (self.label_weights is not None):

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/metrics.py in _build(self, shape)
   1844         'true_positives',
   1845         shape=variable_shape,
-> 1846         initializer=init_ops.zeros_initializer)
   1847     self.true_negatives = self.add_weight(
   1848         'true_negatives',

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/metrics.py in add_weight(self, name, shape, aggregation, synchronization, initializer, dtype)
    274         collections=[],
    275         synchronization=synchronization,
--> 276         aggregation=aggregation)
    277 
    278   ### End: For use by subclasses ###

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, **kwargs)
    444         synchronization=synchronization,
    445         aggregation=aggregation,
--> 446         caching_device=caching_device)
    447     backend.track_variable(variable)
    448 

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
    742         dtype=dtype,
    743         initializer=initializer,
--> 744         **kwargs_for_getter)
    745 
    746     # If we set an initializer and the variable processed it, tracking will not

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in make_variable(name, shape, dtype, initializer, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner)
    140       synchronization=synchronization,
    141       aggregation=aggregation,
--> 142       shape=variable_shape if variable_shape else None)
    143 
    144 

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in __call__(cls, *args, **kwargs)
    256   def __call__(cls, *args, **kwargs):
    257     if cls is VariableV1:
--> 258       return cls._variable_v1_call(*args, **kwargs)
    259     elif cls is Variable:
    260       return cls._variable_v2_call(*args, **kwargs)

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in _variable_v1_call(cls, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint, use_resource, synchronization, aggregation, shape)
    217         synchronization=synchronization,
    218         aggregation=aggregation,
--> 219         shape=shape)
    220 
    221   def _variable_v2_call(cls,

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in <lambda>(**kwargs)
    195                         shape=None):
    196     """Call on Variable class. Useful to force the signature."""
--> 197     previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
    198     for _, getter in ops.get_default_graph()._variable_creator_stack:  # pylint: disable=protected-access
    199       previous_getter = _make_getter(getter, previous_getter)

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py in default_variable_creator(next_creator, **kwargs)
   2594         synchronization=synchronization,
   2595         aggregation=aggregation,
-> 2596         shape=shape)
   2597   else:
   2598     return variables.RefVariable(

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py in __call__(cls, *args, **kwargs)
    260       return cls._variable_v2_call(*args, **kwargs)
    261     else:
--> 262       return super(VariableMetaclass, cls).__call__(*args, **kwargs)
    263 
    264 

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint, distribute_strategy, synchronization, aggregation, shape)
   1409           aggregation=aggregation,
   1410           shape=shape,
-> 1411           distribute_strategy=distribute_strategy)
   1412 
   1413   def _init_from_args(self,

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in _init_from_args(self, initial_value, trainable, collections, caching_device, name, dtype, constraint, synchronization, aggregation, distribute_strategy, shape)
   1540           with ops.name_scope("Initializer"), device_context_manager(None):
   1541             initial_value = ops.convert_to_tensor(
-> 1542                 initial_value() if init_from_fn else initial_value,
   1543                 name="initial_value", dtype=dtype)
   1544           if shape is not None:

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in <lambda>()
    120           (type(init_ops.Initializer), type(init_ops_v2.Initializer))):
    121         initializer = initializer()
--> 122       init_val = lambda: initializer(shape, dtype=dtype)
    123       variable_dtype = dtype.base_dtype
    124   if use_resource is None:

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops.py in __call__(self, shape, dtype, partition_info)
    112     if dtype is None:
    113       dtype = self.dtype
--> 114     return array_ops.zeros(shape, dtype)
    115 
    116   def get_config(self):

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py in zeros(shape, dtype, name)
   2441       except (TypeError, ValueError):
   2442         # Happens when shape is a list with tensor elements
-> 2443         shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
   2444     if not shape._shape_tuple():
   2445       shape = reshape(shape, [-1])  # Ensure it's a vector

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1312 
   1313     if ret is None:
-> 1314       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1315 
   1316     if ret is NotImplemented:

~/miniconda3/envs/autotagging/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py in _tensor_shape_tensor_conversion_function(s, dtype, name, as_ref)
    332   if not s.is_fully_defined():
    333     raise ValueError(
--> 334         "Cannot convert a partially known TensorShape to a Tensor: %s" % s)
    335   s_list = s.as_list()
    336   int64_value = 0

ValueError: Cannot convert a partially known TensorShape to a Tensor: (200, None)

Metadata

Metadata

Assignees

Labels

TF 2.1for tracking issues in 2.1 releasecomp:kerasKeras related issuesstat:awaiting tensorflowerStatus - Awaiting response from tensorflowertype:bugBug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions