Exemplo n.º 1
0
 def _process_labels(self, labels):
   if labels is None:
     raise ValueError(
         'You must provide a labels Tensor. Given: None. '
         'Suggested troubleshooting steps: Check that your data contain '
         'your label feature. Check that your input_fn properly parses and '
         'returns labels.')
   if isinstance(labels, sparse_tensor.SparseTensor):
     if labels.dtype == dtypes.string:
       label_ids_values = lookup_ops.index_table_from_tensor(
           vocabulary_list=tuple(self._label_vocabulary),
           name='class_id_lookup').lookup(labels.values)
       label_ids = sparse_tensor.SparseTensor(
           indices=labels.indices,
           values=label_ids_values,
           dense_shape=labels.dense_shape)
     else:
       label_ids = labels
     return math_ops.to_int64(
         sparse_ops.sparse_to_indicator(label_ids, self._n_classes))
   msg = ('labels shape must be [batch_size, {}]. '
          'Given: ').format(self._n_classes)
   labels_shape = array_ops.shape(labels)
   check_rank_op = control_flow_ops.Assert(
       math_ops.equal(array_ops.rank(labels), 2),
       data=[msg, labels_shape])
   check_label_dim = control_flow_ops.Assert(
       math_ops.equal(labels_shape[-1], self._n_classes),
       data=[msg, labels_shape])
   with ops.control_dependencies([check_rank_op, check_label_dim]):
     return array_ops.identity(labels)
Exemplo n.º 2
0
 def _process_labels(self, labels):
   if isinstance(labels, sparse_tensor.SparseTensor):
     if labels.dtype == dtypes.string:
       label_ids_values = lookup_ops.index_table_from_tensor(
           vocabulary_list=tuple(self._label_vocabulary),
           name='class_id_lookup').lookup(labels.values)
       label_ids = sparse_tensor.SparseTensor(
           indices=labels.indices,
           values=label_ids_values,
           dense_shape=labels.dense_shape)
     else:
       label_ids = labels
     return math_ops.to_int64(
         sparse_ops.sparse_to_indicator(label_ids, self._n_classes))
   msg = ('labels shape must be [batch_size, {}]. '
          'Given: ').format(self._n_classes)
   labels_shape = array_ops.shape(labels)
   check_rank_op = control_flow_ops.Assert(
       math_ops.equal(array_ops.rank(labels), 2),
       data=[msg, labels_shape])
   check_label_dim = control_flow_ops.Assert(
       math_ops.equal(labels_shape[-1], self._n_classes),
       data=[msg, labels_shape])
   with ops.control_dependencies([check_rank_op, check_label_dim]):
     return array_ops.identity(labels)
Exemplo n.º 3
0
  def testInt64(self):
    with self.test_session(use_gpu=False):
      sp_input = self._SparseTensor_5x6(dtypes.int64)
      output = sparse_ops.sparse_to_indicator(sp_input, 50).eval()

      expected_output = np.zeros((5, 50), dtype=np.bool)
      expected_trues = [(0, 0), (1, 10), (1, 13), (1, 14), (3, 32), (3, 33)]
      for expected_true in expected_trues:
        expected_output[expected_true] = True

      self.assertAllEqual(output, expected_output)
Exemplo n.º 4
0
  def testInt64(self):
    with test_util.force_cpu():
      sp_input = self._SparseTensor_5x6(dtypes.int64)
      output = sparse_ops.sparse_to_indicator(sp_input, 50)

      expected_output = np.zeros((5, 50), dtype=np.bool)
      expected_trues = [(0, 0), (1, 10), (1, 13), (1, 14), (3, 32), (3, 33)]
      for expected_true in expected_trues:
        expected_output[expected_true] = True

      self.assertAllEqual(output, expected_output)
Exemplo n.º 5
0
    def testHigherRank(self):
        with self.test_session(use_gpu=False):
            sp_input = self._SparseTensor_2x3x4(types.int64)
            output = sparse_ops.sparse_to_indicator(sp_input, 200).eval()

            expected_output = np.zeros((2, 3, 200), dtype=np.bool)
            expected_trues = [(0, 0, 1), (0, 1, 10), (0, 1, 12), (1, 0, 103), (1, 1, 111), (1, 1, 113), (1, 2, 122)]
            for expected_true in expected_trues:
                expected_output[expected_true] = True

            self.assertAllEqual(output, expected_output)
Exemplo n.º 6
0
    def testInt64(self):
        with test_util.force_cpu():
            sp_input = self._SparseTensor_5x6(dtypes.int64)
            output = sparse_ops.sparse_to_indicator(sp_input, 50)

            expected_output = np.zeros((5, 50), dtype=np.bool)
            expected_trues = [(0, 0), (1, 10), (1, 13), (1, 14), (3, 32),
                              (3, 33)]
            for expected_true in expected_trues:
                expected_output[expected_true] = True

            self.assertAllEqual(output, expected_output)
Exemplo n.º 7
0
    def testHigherRank(self):
        with test_util.force_cpu():
            sp_input = self._SparseTensor_2x3x4(dtypes.int64)
            output = sparse_ops.sparse_to_indicator(sp_input, 200)

            expected_output = np.zeros((2, 3, 200), dtype=np.bool)
            expected_trues = [(0, 0, 1), (0, 1, 10), (0, 1, 12), (1, 0, 103),
                              (1, 1, 149), (1, 1, 150), (1, 2, 122)]
            for expected_true in expected_trues:
                expected_output[expected_true] = True

            self.assertAllEqual(output, expected_output)
Exemplo n.º 8
0
    def testInt64(self):
        with self.test_session(use_gpu=False):
            sp_input = self._SparseTensor_5x6(dtypes.int64)
            output = sparse_ops.sparse_to_indicator(sp_input, 50).eval()

            expected_output = np.zeros((5, 50), dtype=np.bool)
            expected_trues = [(0, 0), (1, 10), (1, 13), (1, 14), (3, 32),
                              (3, 33)]
            for expected_true in expected_trues:
                expected_output[expected_true] = True

            self.assertAllEqual(output, expected_output)
Exemplo n.º 9
0
    def testHigherRank(self):
        with self.test_session(use_gpu=False):
            sp_input = self._SparseTensor_2x3x4(types.int64)
            output = sparse_ops.sparse_to_indicator(sp_input, 200).eval()

            expected_output = np.zeros((2, 3, 200), dtype=np.bool)
            expected_trues = [(0, 0, 1), (0, 1, 10), (0, 1, 12), (1, 0, 103),
                              (1, 1, 111), (1, 1, 113), (1, 2, 122)]
            for expected_true in expected_trues:
                expected_output[expected_true] = True

            self.assertAllEqual(output, expected_output)
Exemplo n.º 10
0
  def testHigherRank(self):
    with test_util.force_cpu():
      sp_input = self._SparseTensor_2x3x4(dtypes.int64)
      output = sparse_ops.sparse_to_indicator(sp_input, 200)

      expected_output = np.zeros((2, 3, 200), dtype=np.bool)
      expected_trues = [(0, 0, 1), (0, 1, 10), (0, 1, 12), (1, 0, 103),
                        (1, 1, 149), (1, 1, 150), (1, 2, 122)]
      for expected_true in expected_trues:
        expected_output[expected_true] = True

      self.assertAllEqual(output, expected_output)
Exemplo n.º 11
0
 def _process_labels(self, labels):
     if labels is None:
         raise ValueError(
             'You must provide a labels Tensor. Given: None. '
             'Suggested troubleshooting steps: Check that your data contain '
             'your label feature. Check that your input_fn properly parses and '
             'returns labels.')
     if isinstance(labels, sparse_tensor.SparseTensor):
         if labels.dtype == dtypes.string:
             label_ids_values = lookup_ops.index_table_from_tensor(
                 vocabulary_list=tuple(self._label_vocabulary),
                 name='class_id_lookup').lookup(labels.values)
             label_ids = sparse_tensor.SparseTensor(
                 indices=labels.indices,
                 values=label_ids_values,
                 dense_shape=labels.dense_shape)
             return math_ops.to_int64(
                 sparse_ops.sparse_to_indicator(label_ids, self._n_classes))
         else:
             err_msg = (
                 r'labels must be an integer SparseTensor with values in '
                 r'[0, {})'.format(self._n_classes))
             assert_int = check_ops.assert_integer(labels.values,
                                                   message=err_msg)
             assert_less = check_ops.assert_less(labels.values,
                                                 ops.convert_to_tensor(
                                                     self._n_classes,
                                                     dtype=labels.dtype),
                                                 message=err_msg)
             assert_greater = check_ops.assert_non_negative(labels.values,
                                                            message=err_msg)
             with ops.control_dependencies(
                 [assert_int, assert_less, assert_greater]):
                 return math_ops.to_int64(
                     sparse_ops.sparse_to_indicator(labels,
                                                    self._n_classes))
     err_msg = (
         r'labels must be an integer indicator Tensor with values in [0, 1]'
     )
     return head_lib._assert_range(labels, 2, message=err_msg)  # pylint:disable=protected-access,
Exemplo n.º 12
0
  def _processed_labels(self, logits, labels):
    """Converts labels to integer id space."""
    if labels is None:
      raise ValueError(base_head._LABEL_NONE_ERR_MSG)  # pylint:disable=protected-access
    if isinstance(labels, sparse_tensor.SparseTensor):
      label_values = labels.values
      if labels.dtype == dtypes.string:
        label_ids_values = self._class_id_table.lookup(label_values)
        label_ids = sparse_tensor.SparseTensor(
            indices=labels.indices,
            values=label_ids_values,
            dense_shape=labels.dense_shape)
        processed_labels = sparse_ops.sparse_to_indicator(
            label_ids, self._n_classes)
      else:
        if not label_values.dtype.is_integer:
          raise ValueError('Labels dtype should be integer. Instead got {}.'.
                           format(label_values.dtype))
        err_msg = (
            r'labels must be an integer SparseTensor with values in '
            r'[0, {})'.format(self._n_classes))
        label_values = base_head.check_label_range(
            labels.values, self._n_classes, message=err_msg)
        if context.executing_eagerly():
          processed_labels = sparse_ops.sparse_to_indicator(
              labels, self._n_classes)
        else:
          with ops.control_dependencies([label_values]):
            processed_labels = sparse_ops.sparse_to_indicator(
                labels, self._n_classes)
      processed_labels = math_ops.to_int64(processed_labels)
    else:
      err_msg = (
          r'labels must be an integer indicator Tensor with values in [0, 1]')
      processed_labels = base_head.check_label_range(labels, 2, message=err_msg)

    return base_head.check_dense_labels_match_logits_and_reshape(
        labels=processed_labels, logits=logits,
        expected_labels_dimension=self.logits_dimension)
Exemplo n.º 13
0
 def _process_labels(self, labels):
   if labels is None:
     raise ValueError(
         'You must provide a labels Tensor. Given: None. '
         'Suggested troubleshooting steps: Check that your data contain '
         'your label feature. Check that your input_fn properly parses and '
         'returns labels.')
   if isinstance(labels, sparse_tensor.SparseTensor):
     if labels.dtype == dtypes.string:
       label_ids_values = lookup_ops.index_table_from_tensor(
           vocabulary_list=tuple(self._label_vocabulary),
           name='class_id_lookup').lookup(labels.values)
       label_ids = sparse_tensor.SparseTensor(
           indices=labels.indices,
           values=label_ids_values,
           dense_shape=labels.dense_shape)
       return math_ops.to_int64(
           sparse_ops.sparse_to_indicator(label_ids, self._n_classes))
     else:
       err_msg = (
           r'labels must be an integer SparseTensor with values in '
           r'[0, {})'.format(self._n_classes))
       assert_int = check_ops.assert_integer(
           labels.values, message=err_msg)
       assert_less = check_ops.assert_less(
           labels.values,
           ops.convert_to_tensor(self._n_classes, dtype=labels.dtype),
           message=err_msg)
       assert_greater = check_ops.assert_non_negative(
           labels.values, message=err_msg)
       with ops.control_dependencies(
           [assert_int, assert_less, assert_greater]):
         return math_ops.to_int64(
             sparse_ops.sparse_to_indicator(labels, self._n_classes))
   err_msg = (
       r'labels must be an integer indicator Tensor with values in [0, 1]')
   return head_lib._assert_range(labels, 2, message=err_msg)  # pylint:disable=protected-access,
Exemplo n.º 14
0
 def _process_labels(self, labels):
   if isinstance(labels, sparse_tensor.SparseTensor):
     return math_ops.to_int64(
         sparse_ops.sparse_to_indicator(labels, self._n_classes))
   msg = ('labels shape must be [batch_size, {}]. '
          'Given: ').format(self._n_classes)
   labels_shape = array_ops.shape(labels)
   check_rank_op = control_flow_ops.Assert(
       math_ops.equal(array_ops.rank(labels), 2),
       data=[msg, labels_shape])
   check_label_dim = control_flow_ops.Assert(
       math_ops.equal(labels_shape[-1], self._n_classes),
       data=[msg, labels_shape])
   with ops.control_dependencies([check_rank_op, check_label_dim]):
     return array_ops.identity(labels)
Exemplo n.º 15
0
 def _process_labels(self, labels):
     if isinstance(labels, sparse_tensor.SparseTensor):
         return math_ops.to_int64(
             sparse_ops.sparse_to_indicator(labels, self._n_classes))
     msg = ('labels shape must be [batch_size, {}]. '
            'Given: ').format(self._n_classes)
     labels_shape = array_ops.shape(labels)
     check_rank_op = control_flow_ops.Assert(math_ops.equal(
         array_ops.rank(labels), 2),
                                             data=[msg, labels_shape])
     check_label_dim = control_flow_ops.Assert(math_ops.equal(
         labels_shape[-1], self._n_classes),
                                               data=[msg, labels_shape])
     with ops.control_dependencies([check_rank_op, check_label_dim]):
         return array_ops.identity(labels)