當前位置: 首頁>>代碼示例>>Python>>正文


Python sparse_ops.sparse_tensor_dense_matmul方法代碼示例

本文整理匯總了Python中tensorflow.python.ops.sparse_ops.sparse_tensor_dense_matmul方法的典型用法代碼示例。如果您正苦於以下問題:Python sparse_ops.sparse_tensor_dense_matmul方法的具體用法?Python sparse_ops.sparse_tensor_dense_matmul怎麽用?Python sparse_ops.sparse_tensor_dense_matmul使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.ops.sparse_ops的用法示例。


在下文中一共展示了sparse_ops.sparse_tensor_dense_matmul方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testShapeInference

# 需要導入模塊: from tensorflow.python.ops import sparse_ops [as 別名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_dense_matmul [as 別名]
def testShapeInference(self):
    x = np.random.rand(10, 10)
    x[np.abs(x) < 0.5] = 0  # Make it sparse
    y = np.random.randn(10, 20)
    x_indices = np.vstack(np.where(x)).astype(np.int64).T
    x_values = x[np.where(x)]
    x_shape = x.shape
    x_st = tf.SparseTensor(x_indices, x_values, x_shape)
    result = tf.sparse_tensor_dense_matmul(x_st, y)
    self.assertEqual(result.get_shape(), (10, 20))

    x_shape_unknown = tf.placeholder(dtype=tf.int64, shape=None)
    x_st_shape_unknown = tf.SparseTensor(x_indices, x_values, x_shape_unknown)
    result_left_shape_unknown = tf.sparse_tensor_dense_matmul(
        x_st_shape_unknown, y)
    self.assertEqual(
        result_left_shape_unknown.get_shape().as_list(), [None, 20])

    x_shape_inconsistent = [10, 15]
    x_st_shape_inconsistent = tf.SparseTensor(
        x_indices, x_values, x_shape_inconsistent)
    with self.assertRaisesRegexp(ValueError, "Dimensions must be equal"):
      tf.sparse_tensor_dense_matmul(x_st_shape_inconsistent, y)

  # Tests setting one dimension to be a high value. 
開發者ID:tobegit3hub,項目名稱:deep_image_model,代碼行數:27,代碼來源:sparse_tensor_dense_matmul_op_test.py

示例2: _sparse_tensor_dense_vs_dense_matmul_benchmark_sparse

# 需要導入模塊: from tensorflow.python.ops import sparse_ops [as 別名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_dense_matmul [as 別名]
def _sparse_tensor_dense_vs_dense_matmul_benchmark_sparse(
    x_ind, x_val, x_shape, y, adjoint_a, adjoint_b):
  sp_x = tf.SparseTensor(indices=x_ind, values=x_val, shape=x_shape)

  def body(t, prev):
    with tf.control_dependencies([prev]):
      return (t + 1,
              sparse_ops.sparse_tensor_dense_matmul(
                  sp_x, y, adjoint_a=adjoint_a, adjoint_b=adjoint_b))

  t0 = tf.constant(0)
  v0 = tf.constant(0.0)
  def _timeit(iterations, _):
    (_, final) = tf.while_loop(
        lambda t, _: t < iterations, body, (t0, v0),
        parallel_iterations=1, back_prop=False)
    return [final]
  return _timeit 
開發者ID:tobegit3hub,項目名稱:deep_image_model,代碼行數:20,代碼來源:sparse_tensor_dense_matmul_op_test.py

示例3: _testMatmul

# 需要導入模塊: from tensorflow.python.ops import sparse_ops [as 別名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_dense_matmul [as 別名]
def _testMatmul(self, x, y, adjoint_a=False, adjoint_b=False):
    x_mat = np.matrix(x)
    if adjoint_a:
      x_mat = x_mat.H
    y_mat = np.matrix(y)
    if adjoint_b:
      y_mat = y_mat.H

    np_ans = x_mat * y_mat

    x_indices = np.vstack(np.where(x)).astype(np.int64).T
    x_values = x[np.where(x)]
    x_shape = x.shape

    with self.test_session(use_gpu=True):
      sp_x_value = tf.SparseTensorValue(
          indices=x_indices, values=x_values, shape=x_shape)
      tf_value_ans = sparse_ops.sparse_tensor_dense_matmul(
          sp_x_value, y, adjoint_a=adjoint_a, adjoint_b=adjoint_b)
      tf_tensor_ans = sparse_ops.sparse_tensor_dense_matmul(
          tf.SparseTensor.from_value(sp_x_value), y, adjoint_a=adjoint_a,
          adjoint_b=adjoint_b)

      # Ensure that the RHS shape is known at least.
      self.assertEqual(tf_value_ans.get_shape()[1], np_ans.shape[1])
      self.assertEqual(tf_tensor_ans.get_shape()[1], np_ans.shape[1])

      for out in (tf_value_ans.eval(), tf_tensor_ans.eval()):
        if x.dtype == np.float32:
          self.assertAllClose(np_ans, out, rtol=1e-4, atol=1e-4)
        elif x.dtype == np.float64:
          self.assertAllClose(np_ans, out, rtol=1e-6, atol=1e-6)
        else:
          self.assertAllClose(np_ans, out, rtol=1e-4, atol=1e-4) 
開發者ID:tobegit3hub,項目名稱:deep_image_model,代碼行數:36,代碼來源:sparse_tensor_dense_matmul_op_test.py

示例4: _SparseTensorDenseMatMulGrad

# 需要導入模塊: from tensorflow.python.ops import sparse_ops [as 別名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_dense_matmul [as 別名]
def _SparseTensorDenseMatMulGrad(op, grad):
  """Gradients for the dense tensor in the SparseTensorDenseMatMul op.

  If either input is complex, no gradient is provided.

  Args:
    op: the SparseTensorDenseMatMul op
    grad: the incoming gradient

  Returns:
    Gradient for each of the 4 input tensors:
      (sparse_indices, sparse_values, sparse_shape, dense_tensor)
    The gradients for indices and shape are None.

  Raises:
    TypeError: When the two operands don't have the same type.
  """
  sp_t = sparse_tensor.SparseTensor(*op.inputs[:3])
  adj_a = op.get_attr("adjoint_a")
  adj_b = op.get_attr("adjoint_b")

  a_type = sp_t.values.dtype.base_dtype
  b_type = op.inputs[3].dtype.base_dtype
  if a_type != b_type:
    raise TypeError("SparseTensorDenseMatMul op received operands with "
                    "different types: ", a_type, " and ", b_type)
  if a_type in (ops.dtypes.complex64, ops.dtypes.complex128):
    raise NotImplementedError("SparseTensorDenseMatMul op does not support "
                              "complex gradients.")

  # gradient w.r.t. dense
  b_grad = sparse_ops.sparse_tensor_dense_matmul(sp_t, grad,
                                                 adjoint_a=not adj_a)
  if adj_b:
    b_grad = array_ops.transpose(b_grad)

  # gradient w.r.t. sparse values
  a_indices = op.inputs[0]
  b = op.inputs[3]

  rows = a_indices[:, 0]
  cols = a_indices[:, 1]

  # TODO(zongheng, ebrevdo): add conjugates in the right places when complex
  # values are allowed.
  # TODO(zongheng): these gather calls could potentially duplicate rows/cols in
  # memory.  If there is a need, we should look into implementing this more
  # intelligently to avoid duplicating data.
  parts_a = array_ops.gather(grad, rows if not adj_a else cols)
  parts_b = array_ops.gather(b if not adj_b else array_ops.transpose(b),
                             cols if not adj_a else rows)
  a_values_grad = math_ops.reduce_sum(parts_a * parts_b, reduction_indices=1)

  # gradients w.r.t. (a_indices, a_values, a_shape, b)
  return (None, a_values_grad, None, b_grad) 
開發者ID:abhisuri97,項目名稱:auto-alt-text-lambda-api,代碼行數:57,代碼來源:sparse_grad.py


注:本文中的tensorflow.python.ops.sparse_ops.sparse_tensor_dense_matmul方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。