当前位置: 首页>>代码示例>>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;未经允许,请勿转载。