예제 #1
0
    def testCwiseAdd(self):
        with self.test_session(use_gpu=False):
            # Identity(2) + AllOnes(2,2).  Should be equal to 2 * Identity(2).
            indices = [[0, 0], [1, 1]]
            vals = [1, 1]
            shape = (2, 2)

            sp_t = tf.SparseTensor(indices, vals, shape)
            dense_t = tf.ones(shape, dtype=dtypes.int32)
            self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t), np.identity(2) * 2, sp_t)

            # Variant of above, but broadcasts the dense side.
            dense_t = tf.ones([1], dtype=dtypes.int32)
            self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t), np.identity(2) * 2, sp_t)
예제 #2
0
def _SparseSoftmaxGrad(op, grad):
    """Gradients for SparseSoftmax.

  The calculation is the same as SoftmaxGrad:

    grad_x = grad_softmax * softmax - sum(grad_softmax * softmax) * softmax

  where we now only operate on the non-zero values present in the SparseTensors.

  Args:
    op: the SparseSoftmax op.
    grad: the upstream gradient w.r.t. the non-zero SparseSoftmax output values.

  Returns:
    Gradients w.r.t. the input (sp_indices, sp_values, sp_shape).
  """
    indices, shape = op.inputs[0], op.inputs[2]
    out_vals = op.outputs[0]
    sp_output = sparse_tensor.SparseTensor(indices, out_vals, shape)
    sp_grad = sparse_tensor.SparseTensor(indices, grad, shape)
    sp_product = sparse_tensor.SparseTensor(indices,
                                            sp_output.values * sp_grad.values,
                                            shape)

    # [..., B, 1], dense.
    sum_reduced = -sparse_ops.sparse_reduce_sum(sp_product, [-1],
                                                keepdims=True)
    # sparse [..., B, C] + dense [..., B, 1] with broadcast; outputs sparse.
    sp_sum = sparse_ops.sparse_dense_cwise_add(sp_grad, sum_reduced)

    grad_x = sp_sum.values * sp_output.values
    return [None, grad_x, None]
    def testCwiseAdd(self):
        with test_util.force_cpu():
            # Identity(2) + AllOnes(2,2).  Should be equal to 2 * Identity(2).
            indices = [[0, 0], [1, 1]]
            vals = [1, 1]
            shape = (2, 2)

            sp_t = sparse_tensor.SparseTensor(indices, vals, shape)
            dense_t = array_ops.ones(shape, dtype=dtypes.int32)
            self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
                        np.identity(2) * 2, sp_t)

            # Variant of above, but broadcasts the dense side.
            dense_t = array_ops.ones([1], dtype=dtypes.int32)
            self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
                        np.identity(2) * 2, sp_t)
예제 #4
0
  def testCwiseAdd(self):
    with self.test_session(use_gpu=False):
      # Identity(2) + AllOnes(2,2).  Should be equal to 2 * Identity(2).
      indices = [[0, 0], [1, 1]]
      vals = [1, 1]
      shape = (2, 2)

      sp_t = tf.SparseTensor(indices, vals, shape)
      dense_t = tf.ones(shape, dtype=dtypes.int32)
      self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
                  np.identity(2) * 2, sp_t)

      # Variant of above, but broadcasts the dense side.
      dense_t = tf.ones([1], dtype=dtypes.int32)
      self._check(sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
                  np.identity(2) * 2, sp_t)
예제 #5
0
def _SparseSoftmaxGrad(op, grad):
  """Gradients for SparseSoftmax.

  The calculation is the same as SoftmaxGrad:

    grad_x = grad_softmax * softmax - sum(grad_softmax * softmax) * softmax

  where we now only operate on the non-zero values present in the SparseTensors.

  Args:
    op: the SparseSoftmax op.
    grad: the upstream gradient w.r.t. the non-zero SparseSoftmax output values.

  Returns:
    Gradients w.r.t. the input (sp_indices, sp_values, sp_shape).
  """
  indices, shape = op.inputs[0], op.inputs[2]
  out_vals = op.outputs[0]
  sp_output = sparse_tensor.SparseTensor(indices, out_vals, shape)
  sp_grad = sparse_tensor.SparseTensor(indices, grad, shape)
  sp_product = sparse_tensor.SparseTensor(
      indices, sp_output.values * sp_grad.values, shape)

  # [..., B, 1], dense.
  sum_reduced = -sparse_ops.sparse_reduce_sum(sp_product, [-1], keep_dims=True)
  # sparse [..., B, C] + dense [..., B, 1] with broadcast; outputs sparse.
  sp_sum = sparse_ops.sparse_dense_cwise_add(sp_grad, sum_reduced)

  grad_x = sp_sum.values * sp_output.values
  return [None, grad_x, None]
예제 #6
0
  def testCwiseAdd(self):
    with test_util.force_cpu():
      # Identity(2) + AllOnes(2,2).  Should be equal to 2 * Identity(2).
      indices = [[0, 0], [1, 1]]
      vals = [1, 1]
      shape = (2, 2)

      sp_t = sparse_tensor.SparseTensor(indices, vals, shape)
      dense_t = array_ops.ones(shape, dtype=dtypes.int32)
      self._check(
          sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
          np.identity(2) * 2, sp_t)

      # Variant of above, but broadcasts the dense side.
      dense_t = array_ops.ones([1], dtype=dtypes.int32)
      self._check(
          sparse_ops.sparse_dense_cwise_add(sp_t, dense_t),
          np.identity(2) * 2, sp_t)