当前位置: 首页>>代码示例>>Python>>正文


Python sparse_ops.sparse_to_dense方法代码示例

本文整理汇总了Python中tensorflow.python.ops.sparse_ops.sparse_to_dense方法的典型用法代码示例。如果您正苦于以下问题:Python sparse_ops.sparse_to_dense方法的具体用法?Python sparse_ops.sparse_to_dense怎么用?Python sparse_ops.sparse_to_dense使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.python.ops.sparse_ops的用法示例。


在下文中一共展示了sparse_ops.sparse_to_dense方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _apply_transform

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _apply_transform(self, input_tensors, **kwargs):
    """Applies the transformation to the `transform_input`.

    Args:
      input_tensors: a list of Tensors representing the input to
        the Transform.
      **kwargs: Additional keyword arguments, unused here.

    Returns:
        A namedtuple of Tensors representing the transformed output.
    """
    s = input_tensors[0]

     # pylint: disable=not-callable
    return self.return_type(sparse_ops.sparse_to_dense(
        s.indices, s.dense_shape, s.values, default_value=self.default_value)) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:18,代码来源:densify.py

示例2: _zero_out_grad

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _zero_out_grad(op, grad):
  """The gradients for `zero_out`.

  Args:
    op: The `zero_out` `Operation` that we are differentiating, which we can use
      to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `zero_out` op.

  Returns:
    Gradients with respect to the input of `zero_out`.
  """
  to_zero = op.inputs[0]
  shape = array_ops.shape(to_zero)
  index = array_ops.zeros_like(shape)
  first_grad = array_ops.reshape(grad, [-1])[0]
  to_zero_grad = sparse_ops.sparse_to_dense(index, shape, first_grad, 0)
  return [to_zero_grad]  # List of one Tensor, since we have one input 
开发者ID:tensorflowkorea,项目名称:tensorflow-kr,代码行数:19,代码来源:zero_out_grad_2.py

示例3: _sparse_vs_dense_xent_benchmark_dense

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _sparse_vs_dense_xent_benchmark_dense(labels, logits):
  labels = tf.identity(labels)
  logits = tf.identity(logits)
  with tf.device("/cpu:0"):  # Sparse-to-dense must be on CPU
    batch_size = tf.shape(logits)[0]
    num_entries = tf.shape(logits)[1]
    length = batch_size * num_entries
    labels += num_entries * tf.range(batch_size)
    target = sparse_ops.sparse_to_dense(labels, tf.stack([length]), 1.0, 0.0)
  target = tf.reshape(target, tf.stack([-1, num_entries]))
  crossent = tf.nn.softmax_cross_entropy_with_logits(
      logits, target, name="SequenceLoss/CrossEntropy")
  crossent_sum = tf.reduce_sum(crossent)
  grads = tf.gradients([crossent_sum], [logits])[0]

  return (crossent_sum, grads) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:18,代码来源:sparse_xent_op_test.py

示例4: _TopKGrad

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _TopKGrad(op, grad, _):
  """Return the gradients for TopK.

  Args:
    op: The TopKOp for which we need to generate gradients.
    grad: Tensor. The gradients passed to the TopKOp.

  Returns:
    A list of two tensors, the first being the gradient w.r.t to the input and
    TopK, and the second being the gradient w.r.t. to the indices (all zero).
  """
  in_shape = array_ops.shape(op.inputs[0])
  ind_shape = array_ops.shape(op.outputs[1])

  ind_lastdim = array_ops.gather(ind_shape, array_ops.size(ind_shape) - 1)
  # Flatten indices to 2D.
  ind_2d = array_ops.reshape(op.outputs[1], array_ops.stack([-1, ind_lastdim]))

  in_lastdim = array_ops.gather(in_shape, array_ops.size(in_shape) - 1)
  outerdim = array_ops.shape(ind_2d)[0]
  # Compute linear indices (flattened to 1D).
  ind = array_ops.reshape(ind_2d + array_ops.expand_dims(
      math_ops.range(0, outerdim * in_lastdim, in_lastdim), -1), [-1])

  # Substitute grad to appropriate locations and fill the rest with zeros,
  # finally reshaping it to the original input shape.
  return [array_ops.reshape(
      sparse_ops.sparse_to_dense(ind,
                                 array_ops.reshape(
                                     math_ops.reduce_prod(in_shape), [1]),
                                 array_ops.reshape(grad, [-1]),
                                 validate_indices=False),
      in_shape), array_ops.zeros(
          [], dtype=dtypes.int32)] 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:36,代码来源:nn_grad.py

示例5: one_hot_mask

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def one_hot_mask(labels, num_classes, scope=None):
  """Compute 1-hot encodings for masks.

  Given a label image, this computes the one hot encoding at
  each pixel.

  Args:
    labels: (batch_size, width, height, 1) tensor containing labels.
    num_classes: number of classes
    scope: optional scope name

  Returns:
    Tensor of shape (batch_size, width, height, num_classes) with
    a 1-hot encoding.
  """
  with ops.name_scope(scope, "OneHotMask", [labels]):
    height, width, depth = _shape(labels)
    assert depth == 1
    sparse_labels = math_ops.to_int32(array_ops.reshape(labels, [-1, 1]))
    sparse_size, _ = _shape(sparse_labels)
    indices = array_ops.reshape(math_ops.range(0, sparse_size, 1), [-1, 1])
    concated = array_ops.concat([indices, sparse_labels], 1)
    dense_result = sparse_ops.sparse_to_dense(concated,
                                              [sparse_size, num_classes], 1.0,
                                              0.0)
    result = array_ops.reshape(dense_result, [height, width, num_classes])
    return result 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:29,代码来源:misc.py

示例6: testDenseFromConstantToSparse

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def testDenseFromConstantToSparse(self):
    expected_constant = np.reshape(np.arange(24, dtype=np.int64), (3, 4, 2))
    tensor = constant_op.constant(expected_constant)
    sparse = _layers.dense_to_sparse(tensor)
    dense = sparse_ops.sparse_to_dense(sparse.indices, sparse.dense_shape,
                                       sparse.values)
    with self.cached_session() as sess:
      constant = sess.run(dense)
      self.assertAllEqual(expected_constant, constant) 
开发者ID:google-research,项目名称:tf-slim,代码行数:11,代码来源:layers_test.py

示例7: _check

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _check(self, result_tensor, result_np, input_sp_t):
    self.assertTrue(isinstance(result_tensor, tf.SparseTensor))
    self.assertTrue(isinstance(input_sp_t, tf.SparseTensor))
    self.assertAllEqual(input_sp_t.indices.eval(), result_tensor.indices.eval())
    self.assertAllEqual(input_sp_t.shape.eval(), result_tensor.shape.eval())

    res_densified = sparse_ops.sparse_to_dense(result_tensor.indices,
                                               result_tensor.shape,
                                               result_tensor.values).eval()
    self.assertAllEqual(result_np, res_densified) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:12,代码来源:sparse_ops_test.py

示例8: _TopKGrad

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def _TopKGrad(op, grad, _):
  """Return the gradients for TopK.

  Args:
    op: The TopKOp for which we need to generate gradients.
    grad: Tensor. The gradients passed to the TopKOp.

  Returns:
    A list of two tensors, the first being the gradient w.r.t to the input and
    TopK, and the second being the gradient w.r.t. to the indices (all zero).
  """
  in_shape = array_ops.shape(op.inputs[0])
  ind_shape = array_ops.shape(op.outputs[1])

  ind_lastdim = array_ops.gather(ind_shape, array_ops.size(ind_shape) - 1)
  # Flatten indices to 2D.
  ind_2d = array_ops.reshape(op.outputs[1], array_ops.pack([-1, ind_lastdim]))

  in_lastdim = array_ops.gather(in_shape, array_ops.size(in_shape) - 1)
  outerdim = array_ops.shape(ind_2d)[0]
  # Compute linear indices (flattened to 1D).
  ind = array_ops.reshape(ind_2d + array_ops.expand_dims(
      math_ops.range(0, outerdim * in_lastdim, in_lastdim), -1), [-1])

  # Substitute grad to appropriate locations and fill the rest with zeros,
  # finally reshaping it to the original input shape.
  return [array_ops.reshape(
      sparse_ops.sparse_to_dense(ind,
                                 array_ops.reshape(
                                     math_ops.reduce_prod(in_shape), [1]),
                                 array_ops.reshape(grad, [-1]),
                                 validate_indices=False),
      in_shape), array_ops.zeros(
          [], dtype=dtypes.int32)] 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:36,代码来源:nn_grad.py

示例9: ctc_decode

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def ctc_decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1):
  """Decodes the output of a softmax.

  Can use either greedy search (also known as best path)
  or a constrained dictionary search.

  Arguments:
      y_pred: tensor `(samples, time_steps, num_categories)`
          containing the prediction, or output of the softmax.
      input_length: tensor `(samples, )` containing the sequence length for
          each batch item in `y_pred`.
      greedy: perform much faster best-path search if `true`.
          This does not use a dictionary.
      beam_width: if `greedy` is `false`: a beam search decoder will be used
          with a beam of this width.
      top_paths: if `greedy` is `false`,
          how many of the most probable paths will be returned.

  Returns:
      Tuple:
          List: if `greedy` is `true`, returns a list of one element that
              contains the decoded sequence.
              If `false`, returns the `top_paths` most probable
              decoded sequences.
              Important: blank labels are returned as `-1`.
          Tensor `(top_paths, )` that contains
              the log probability of each decoded sequence.
  """
  y_pred = math_ops.log(array_ops.transpose(y_pred, perm=[1, 0, 2]) + 1e-8)
  input_length = math_ops.to_int32(input_length)

  if greedy:
    (decoded, log_prob) = ctc.ctc_greedy_decoder(
        inputs=y_pred, sequence_length=input_length)
  else:
    (decoded, log_prob) = ctc.ctc_beam_search_decoder(
        inputs=y_pred,
        sequence_length=input_length,
        beam_width=beam_width,
        top_paths=top_paths)
  decoded_dense = [
      sparse_ops.sparse_to_dense(
          st.indices, st.dense_shape, st.values, default_value=-1)
      for st in decoded
  ]
  return (decoded_dense, log_prob)


# HIGH ORDER FUNCTIONS 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:51,代码来源:backend.py

示例10: sparse_vs_dense_xent_benchmark

# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_to_dense [as 别名]
def sparse_vs_dense_xent_benchmark(batch_size, num_entries, use_gpu):
  config = tf.ConfigProto()
  config.allow_soft_placement = True
  config.gpu_options.per_process_gpu_memory_fraction = 0.3
  labels = np.random.randint(num_entries, size=batch_size).astype(np.int32)
  logits = np.random.randn(batch_size, num_entries).astype(np.float32)

  def _timer(sess, ops):
    # Warm in
    for _ in range(20):
      sess.run(ops)

    # Timing run
    start = time.time()
    for _ in range(20):
      sess.run(ops)
    end = time.time()

    return (end - start)/20.0  # Average runtime per iteration

  # Using sparse_to_dense and softmax_cross_entropy_with_logits
  with tf.Session(config=config) as sess:
    if not use_gpu:
      with tf.device("/cpu:0"):
        ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits)
    else:
      ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits)
    delta_dense = _timer(sess, ops)

  # Using sparse_softmax_cross_entropy_with_logits
  with tf.Session(config=config) as sess:
    if not use_gpu:
      with tf.device("/cpu:0"):
        ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits)
    else:
      ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits)
    delta_sparse = _timer(sess, ops)

  print(
      "%d \t %d \t %s \t %f \t %f \t %f"
      % (batch_size, num_entries, use_gpu, delta_dense, delta_sparse,
         delta_sparse/delta_dense)) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:44,代码来源:sparse_xent_op_test.py


注:本文中的tensorflow.python.ops.sparse_ops.sparse_to_dense方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。