def test_dense_input_wrong_shape_fails(self): x = np.array([[3, 2, 1], [5, 4, 4]], dtype=np.int32) weights = np.array([[3, 2], [5, 4], [4, 3]]) # Note: Eager mode and graph mode throw different errors here. Graph mode # will fail with a ValueError from the shape checking logic, while Eager # will fail with an InvalidArgumentError from the kernel itself. if context.executing_eagerly(): with self.assertRaisesRegexp(errors.InvalidArgumentError, "must have the same shape"): self.evaluate( bincount.sparse_bincount(x, weights=weights, axis=-1)) else: with self.assertRaisesRegexp(ValueError, "both shapes must be equal"): self.evaluate( bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_dense_input_ragged_weights_fails(self): x = np.array([[3, 2, 1], [5, 4, 4]], dtype=np.int32) weights = ragged_factory_ops.constant([[6, 0.5, 2], [14], [10, 0.25, 5, 3]]) with self.assertRaisesRegexp(ValueError, "must be a tf.Tensor"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_ragged_input_different_shape_fails(self): x = ragged_factory_ops.constant([[6, 1, 2], [14], [10, 1, 5, 3]]) weights = ragged_factory_ops.constant([[6, 0.5, 2], [], [10, 0.25, 5, 3]]) with self.assertRaisesRegexp(errors.InvalidArgumentError, "must have the same row splits"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_ragged_input_sparse_weights_fails(self): x = ragged_factory_ops.constant([[6, 1, 2], [14], [10, 1, 5, 3]]) weights = sparse_ops.from_dense( np.array([[3, 0, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4]], dtype=np.int32)) with self.assertRaisesRegexp(ValueError, "must be a RaggedTensor"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_sparse_input_dense_weights_fails(self): x = sparse_ops.from_dense( np.array([[3, 0, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4]], dtype=np.int32)) weights = np.array([[3, 2, 1], [5, 4, 4]], dtype=np.int32) with self.assertRaisesRegexp(ValueError, "must be a SparseTensor"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_sparse_input_wrong_shape_fails(self): x = sparse_ops.from_dense( np.array([[3, 0, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4]], dtype=np.int32)) weights = sparse_ops.from_dense( np.array([[3, 0, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4], [0, 0, 0, 0]], dtype=np.int32)) with self.assertRaisesRegexp(errors.InvalidArgumentError, "must have the same dense shape"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_sparse_input_too_many_indices_fails(self): x = sparse_ops.from_dense( np.array([[3, 0, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4]], dtype=np.int32)) weights = sparse_ops.from_dense( np.array([[3, 1, 1, 0], [0, 0, 0, 0], [5, 0, 4, 4]], dtype=np.int32)) with self.assertRaisesRegexp(errors.InvalidArgumentError, "Incompatible shapes"): self.evaluate(bincount.sparse_bincount(x, weights=weights, axis=-1))
def test_dense_input(self, x, expected_indices, expected_values, expected_shape, minlength=None, maxlength=None, binary_output=False, weights=None, axis=-1): y = bincount.sparse_bincount(x, weights=weights, minlength=minlength, maxlength=maxlength, binary_output=binary_output, axis=axis) self.assertAllEqual(expected_indices, y.indices) self.assertAllEqual(expected_values, y.values) self.assertAllEqual(expected_shape, y.dense_shape)
def test_ragged_input(self, x, expected_indices, expected_values, expected_shape, maxlength=None, minlength=None, binary_count=False, weights=None, axis=-1): x_ragged = ragged_factory_ops.constant(x) y = bincount.sparse_bincount(x_ragged, weights=weights, minlength=minlength, maxlength=maxlength, binary_count=binary_count, axis=axis) self.assertAllEqual(expected_indices, y.indices) self.assertAllEqual(expected_values, y.values) self.assertAllEqual(expected_shape, y.dense_shape)
def test_sparse_input(self, x, expected_indices, expected_values, expected_shape, maxlength=None, minlength=None, binary_output=False, weights=None, axis=-1): x_sparse = sparse_ops.from_dense(x) w_sparse = sparse_ops.from_dense( weights) if weights is not None else None y = bincount.sparse_bincount(x_sparse, weights=w_sparse, minlength=minlength, maxlength=maxlength, binary_output=binary_output, axis=axis) self.assertAllEqual(expected_indices, y.indices) self.assertAllEqual(expected_values, y.values) self.assertAllEqual(expected_shape, y.dense_shape)