예제 #1
0
def _prune_invalid_weights(sparse_ids, sparse_weights):
  """Prune invalid weights (< 0) from the input ids and weights."""
  if sparse_weights is not None:
    is_weights_valid = math_ops.greater(sparse_weights.values, 0)
    sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_weights_valid)
    sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_weights_valid)
  return sparse_ids, sparse_weights
def _prune_invalid_weights(sparse_ids, sparse_weights):
  """Prune invalid weights (< 0) from the input ids and weights."""
  if sparse_weights is not None:
    is_weights_valid = math_ops.greater(sparse_weights.values, 0)
    sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_weights_valid)
    sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_weights_valid)
  return sparse_ids, sparse_weights
예제 #3
0
def _prune_invalid_ids(sparse_ids, sparse_weights):
    """Prune invalid IDs (< 0) from the input ids and weights."""
    is_id_valid = math_ops.greater_equal(sparse_ids.values, 0)
    if sparse_weights is not None:
        is_id_valid = math_ops.logical_and(is_id_valid, math_ops.greater(sparse_weights.values, 0))
    sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_id_valid)
    if sparse_weights is not None:
        sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_id_valid)
    return sparse_ids, sparse_weights
예제 #4
0
def _prune_invalid_ids(sparse_ids, sparse_weights):
  """Prune invalid IDs (< 0) from the input ids and weights."""
  is_id_valid = math_ops.greater_equal(sparse_ids.values, 0)
  if sparse_weights is not None:
    is_id_valid = math_ops.logical_and(
        is_id_valid, math_ops.greater(sparse_weights.values, 0))
  sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_id_valid)
  if sparse_weights is not None:
    sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_id_valid)
  return sparse_ids, sparse_weights
예제 #5
0
def sparse_boolean_mask(sparse_tensor, mask, name="sparse_boolean_mask"):
  """Boolean mask for `SparseTensor`s.

  Args:
    sparse_tensor: a `SparseTensor`.
    mask: a 1D boolean dense`Tensor` whose length is equal to the 0th dimension
      of `sparse_tensor`.
    name: optional name for this operation.
  Returns:
    A `SparseTensor` that contains row `k` of `sparse_tensor` iff `mask[k]` is
    `True`.
  """
  # TODO(jamieas): consider mask dimension > 1 for symmetry with `boolean_mask`.
  with ops.op_scope([sparse_tensor, mask], name):
    mask = ops.convert_to_tensor(mask)
    mask_rows = array_ops.where(mask)
    first_indices = array_ops.squeeze(array_ops.slice(sparse_tensor.indices,
                                                      [0, 0], [-1, 1]))

    # Identify indices corresponding to the rows identified by mask_rows.
    sparse_entry_matches = functional_ops.map_fn(
        lambda x: math_ops.equal(first_indices, x),
        mask_rows,
        dtype=dtypes.bool)
    # Combine the rows of index_matches to form a mask for the sparse indices
    # and values.
    to_retain = array_ops.reshape(
        functional_ops.foldl(math_ops.logical_or, sparse_entry_matches), [-1])

    return sparse_ops.sparse_retain(sparse_tensor, to_retain)
예제 #6
0
def sparse_boolean_mask(sparse_tensor, mask, name="sparse_boolean_mask"):
    """Boolean mask for `SparseTensor`s.

  Args:
    sparse_tensor: a `SparseTensor`.
    mask: a 1D boolean dense`Tensor` whose length is equal to the 0th dimension
      of `sparse_tensor`.
    name: optional name for this operation.
  Returns:
    A `SparseTensor` that contains row `k` of `sparse_tensor` iff `mask[k]` is
    `True`.
  """
    # TODO(jamieas): consider mask dimension > 1 for symmetry with `boolean_mask`.
    with ops.op_scope([sparse_tensor, mask], name):
        mask = ops.convert_to_tensor(mask)
        mask_rows = array_ops.where(mask)
        first_indices = array_ops.squeeze(
            array_ops.slice(sparse_tensor.indices, [0, 0], [-1, 1]))

        # Identify indices corresponding to the rows identified by mask_rows.
        sparse_entry_matches = functional_ops.map_fn(
            lambda x: math_ops.equal(first_indices, x),
            mask_rows,
            dtype=dtypes.bool)
        # Combine the rows of index_matches to form a mask for the sparse indices
        # and values.
        to_retain = array_ops.reshape(
            functional_ops.foldl(math_ops.logical_or, sparse_entry_matches),
            [-1])

        return sparse_ops.sparse_retain(sparse_tensor, to_retain)
    def testRetainNone(self):
        with test_util.force_cpu():
            sp_input = self._SparseTensor_5x6()
            to_retain = np.zeros((6, ), dtype=np.bool)
            sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

            output = self.evaluate(sp_output)

            self.assertAllEqual(output.indices, np.array([]).reshape((0, 2)))
            self.assertAllEqual(output.values, [])
            self.assertAllEqual(output.dense_shape, [5, 6])
예제 #8
0
    def testRetainNone(self):
        with self.test_session(use_gpu=False) as sess:
            sp_input = self._SparseTensor_5x6()
            to_retain = np.zeros((6, ), dtype=np.bool)
            sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

            output = sess.run(sp_output)

            self.assertAllEqual(output.indices, np.array([]).reshape((0, 2)))
            self.assertAllEqual(output.values, [])
            self.assertAllEqual(output.shape, [5, 6])
예제 #9
0
  def testRetainNone(self):
    with self.test_session(use_gpu=False) as sess:
      sp_input = self._SparseTensor_5x6()
      to_retain = np.zeros((6,), dtype=np.bool)
      sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

      output = sess.run(sp_output)

      self.assertAllEqual(output.indices, np.array([]).reshape((0, 2)))
      self.assertAllEqual(output.values, [])
      self.assertAllEqual(output.shape, [5, 6])
예제 #10
0
  def testBasic(self):
    with test_util.force_cpu():
      for sp_input in (self._SparseTensorValue_5x6(), self._SparseTensor_5x6()):
        to_retain = np.array([1, 0, 0, 1, 1, 0], dtype=np.bool_)
        sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

        output = self.evaluate(sp_output)

        self.assertAllEqual(output.indices, [[0, 0], [1, 4], [3, 2]])
        self.assertAllEqual(output.values, [0, 14, 32])
        self.assertAllEqual(output.dense_shape, [5, 6])
예제 #11
0
  def testRetainNone(self):
    with test_util.force_cpu():
      sp_input = self._SparseTensor_5x6()
      to_retain = np.zeros((6,), dtype=np.bool)
      sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

      output = self.evaluate(sp_output)

      self.assertAllEqual(output.indices, np.array([]).reshape((0, 2)))
      self.assertAllEqual(output.values, [])
      self.assertAllEqual(output.dense_shape, [5, 6])
예제 #12
0
  def testBasic(self):
    with test_util.force_cpu():
      for sp_input in (self._SparseTensorValue_5x6(), self._SparseTensor_5x6()):
        to_retain = np.array([1, 0, 0, 1, 1, 0], dtype=np.bool)
        sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

        output = self.evaluate(sp_output)

        self.assertAllEqual(output.indices, [[0, 0], [1, 4], [3, 2]])
        self.assertAllEqual(output.values, [0, 14, 32])
        self.assertAllEqual(output.dense_shape, [5, 6])
예제 #13
0
    def testBasic(self):
        with self.test_session(use_gpu=False) as sess:
            sp_input = self._SparseTensor_5x6()
            to_retain = np.array([1, 0, 0, 1, 1, 0], dtype=np.bool)
            sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

            output = sess.run(sp_output)

            self.assertAllEqual(output.indices, [[0, 0], [1, 4], [3, 2]])
            self.assertAllEqual(output.values, [0, 14, 32])
            self.assertAllEqual(output.shape, [5, 6])
예제 #14
0
  def testBasic(self):
    with self.test_session(use_gpu=False) as sess:
      sp_input = self._SparseTensor_5x6()
      to_retain = np.array([1, 0, 0, 1, 1, 0], dtype=np.bool)
      sp_output = sparse_ops.sparse_retain(sp_input, to_retain)

      output = sess.run(sp_output)

      self.assertAllEqual(output.indices, [[0, 0], [1, 4], [3, 2]])
      self.assertAllEqual(output.values, [0, 14, 32])
      self.assertAllEqual(output.shape, [5, 6])
예제 #15
0
def sparse_put(sp_tensor, sp_updates, name="sparse_put"):
    """Changes a given SparseTensor according to the updates specified in a SparseTensor.

    Creates a new tensor where the values of the updates override the
    values in the original tensor. The input tensors must have the same
    ``dense_shape``.

    Args:
        sp_tensor: a ``SparseTensor`` we which to set some indices to given values
        sp_updates: a ``SparseTensor`` with the indices to be changed and the respective values
        name: name for this operation (optional)

    Returns:
        ``SparseTensor``: a ``SparseTensor`` with the updated values.
    """
    with ops.name_scope(name):
        if sp_updates.dtype != sp_tensor.dtype:
            sp_updates = math_ops.cast(sp_updates, sp_tensor.dtype)

        # 1 concat indices and establish final tensor shape
        update_shape = sp_updates.values.get_shape()
        zero_updates = SparseTensor(
            sp_updates.indices,
            array_ops.zeros(update_shape, dtype=dtypes.float32),
            sp_updates.dense_shape)
        proto_result = sp_ops.sparse_add(sp_tensor, zero_updates)

        # shape of resulting values tensor
        proto_shape = array_ops.shape(proto_result.values)

        # 2 get mask for input tensor
        proto_ones = SparseTensor(proto_result.indices,
                                  array_ops.ones(proto_shape, dtypes.int8),
                                  proto_result.dense_shape)

        # mask_ones = math_ops.scalar_mul(-1, array_ops.ones(update_shape))
        sp_mask = SparseTensor(
            sp_updates.indices,
            array_ops.constant(-1, dtypes.int8, update_shape),
            sp_updates.dense_shape)

        to_retain = sp_ops.sparse_add(proto_ones, sp_mask)
        to_retain = math_ops.not_equal(to_retain.values, 0)

        # get tensor with masked values
        tensor_masked = sp_ops.sparse_retain(proto_result, to_retain)

        # add values to entries previously set to 0
        new_tensor = sp_ops.sparse_add(tensor_masked, sp_updates)
        return new_tensor
예제 #16
0
def _prune_invalid_ids(sparse_ids):
    """Prune invalid IDs (< 0) from the input ids."""
    is_id_valid = tf.greater_equal(sparse_ids.values, 0)
    sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_id_valid)
    return sparse_ids
예제 #17
0
 def testMismatchedRetainShape(self):
   with test_util.force_cpu():
     sp_input = self._SparseTensor_5x6()
     to_retain = np.array([1, 0, 0, 1, 0], dtype=np.bool)
     with self.assertRaises(ValueError):
       sparse_ops.sparse_retain(sp_input, to_retain)
예제 #18
0
 def testMismatchedRetainShape(self):
   with self.test_session(use_gpu=False):
     sp_input = self._SparseTensor_5x6()
     to_retain = np.array([1, 0, 0, 1, 0], dtype=np.bool)
     with self.assertRaises(ValueError):
       sparse_ops.sparse_retain(sp_input, to_retain)
예제 #19
0
 def testMismatchedRetainShape(self):
     with self.test_session(use_gpu=False):
         sp_input = self._SparseTensor_5x6()
         to_retain = np.array([1, 0, 0, 1, 0], dtype=np.bool)
         with self.assertRaises(ValueError):
             sparse_ops.sparse_retain(sp_input, to_retain)
 def testMismatchedRetainShape(self):
     with test_util.force_cpu():
         sp_input = self._SparseTensor_5x6()
         to_retain = np.array([1, 0, 0, 1, 0], dtype=np.bool)
         with self.assertRaises(ValueError):
             sparse_ops.sparse_retain(sp_input, to_retain)