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


Python sparse_ops.dense_to_sparse_tensor函数代码示例

本文整理汇总了Python中tensorflow.contrib.layers.python.ops.sparse_ops.dense_to_sparse_tensor函数的典型用法代码示例。如果您正苦于以下问题:Python dense_to_sparse_tensor函数的具体用法?Python dense_to_sparse_tensor怎么用?Python dense_to_sparse_tensor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: testBowEncoderSparseTensor

 def testBowEncoderSparseTensor(self):
   with self.cached_session() as sess:
     docs = [[0, 1], [2, 3]]
     sparse_docs = sparse_ops.dense_to_sparse_tensor(docs)
     enc = encoders.bow_encoder(sparse_docs, 4, 3)
     sess.run(variables.global_variables_initializer())
     self.assertAllEqual([2, 3], enc.eval().shape)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:7,代码来源:encoders_test.py

示例2: test_dense_to_sparse_tensor_2d

 def test_dense_to_sparse_tensor_2d(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor([[1, 2, 0, 0], [3, 4, 5, 0]])
     result = sess.run(st)
   self.assertAllEqual([[0, 0], [0, 1], [1, 0], [1, 1], [1, 2]],
                       result.indices)
   self.assertAllEqual([1, 2, 3, 4, 5], result.values)
   self.assertAllEqual([2, 4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例3: test_dense_to_sparse_tensor_1d_no_shape

 def test_dense_to_sparse_tensor_1d_no_shape(self):
   with self.test_session() as sess:
     tensor = array_ops.placeholder(shape=[None], dtype=dtypes.int32)
     st = sparse_ops.dense_to_sparse_tensor(tensor)
     result = sess.run(st, feed_dict={tensor: [0, 100, 0, 3]})
   self.assertAllEqual([[1], [3]], result.indices)
   self.assertAllEqual([100, 3], result.values)
   self.assertAllEqual([4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例4: bow_encoder

def bow_encoder(ids,
                vocab_size,
                embed_dim,
                sparse_lookup=True,
                initializer=None,
                regularizer=None,
                trainable=True,
                scope=None,
                reuse=None):
  """Maps a sequence of symbols to a vector per example by averaging embeddings.

  Args:
    ids: `[batch_size, doc_length]` `Tensor` or `SparseTensor` of type
      `int32` or `int64` with symbol ids.
    vocab_size: Integer number of symbols in vocabulary.
    embed_dim: Integer number of dimensions for embedding matrix.
    sparse_lookup: `bool`, if `True`, converts ids to a `SparseTensor`
        and performs a sparse embedding lookup. This is usually faster,
        but not desirable if padding tokens should have an embedding. Empty rows
        are assigned a special embedding.
    initializer: An initializer for the embeddings, if `None` default for
        current scope is used.
    regularizer: Optional regularizer for the embeddings.
    trainable: If `True` also add variables to the graph collection
      `GraphKeys.TRAINABLE_VARIABLES` (see tf.Variable).
    scope: Optional string specifying the variable scope for the op, required
        if `reuse=True`.
    reuse: If `True`, variables inside the op will be reused.

  Returns:
    Encoding `Tensor` `[batch_size, embed_dim]` produced by
    averaging embeddings.

  Raises:
    ValueError: If `embed_dim` or `vocab_size` are not specified.
  """
  if not vocab_size or not embed_dim:
    raise ValueError('Must specify vocab size and embedding dimension')
  with variable_scope.variable_scope(
      scope, 'bow_encoder', [ids], reuse=reuse):
    embeddings = variables.model_variable(
        'embeddings', shape=[vocab_size, embed_dim],
        initializer=initializer, regularizer=regularizer,
        trainable=trainable)
    if sparse_lookup:
      if isinstance(ids, sparse_tensor.SparseTensor):
        sparse_ids = ids
      else:
        sparse_ids = sparse_ops.dense_to_sparse_tensor(ids)
      return contrib_embedding_ops.safe_embedding_lookup_sparse(
          [embeddings], sparse_ids, combiner='mean', default_id=0)
    else:
      if isinstance(ids, sparse_tensor.SparseTensor):
        raise TypeError('ids are expected to be dense Tensor, got: %s', ids)
      return math_ops.reduce_mean(
          embedding_ops.embedding_lookup(embeddings, ids),
          reduction_indices=1)
开发者ID:finardi,项目名称:tensorflow,代码行数:57,代码来源:encoders.py

示例5: test_dense_to_sparse_unknown_rank

 def test_dense_to_sparse_unknown_rank(self):
   ph = array_ops.placeholder(dtype=dtypes.int32)
   with self.cached_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor(ph)
     result = sess.run(st, feed_dict={ph: [[1, 2, 0, 0], [3, 4, 5, 0]]})
   self.assertAllEqual([[0, 0], [0, 1], [1, 0], [1, 1], [1, 2]],
                       result.indices)
   self.assertAllEqual([1, 2, 3, 4, 5], result.values)
   self.assertAllEqual([2, 4], result.dense_shape)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:9,代码来源:sparse_ops_test.py

示例6: test_dense_to_sparse_tensor_1d_str

 def test_dense_to_sparse_tensor_1d_str(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor([b'qwe', b'', b'ewq', b''])
     result = sess.run(st)
   self.assertEqual(result.indices.dtype, np.int64)
   self.assertEqual(result.values.dtype, np.object)
   self.assertEqual(result.dense_shape.dtype, np.int64)
   self.assertAllEqual([[0], [2]], result.indices)
   self.assertAllEqual([b'qwe', b'ewq'], result.values)
   self.assertAllEqual([4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例7: test_dense_to_sparse_tensor_1d_bool

 def test_dense_to_sparse_tensor_1d_bool(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor([True, False, True, False])
     result = sess.run(st)
   self.assertEqual(result.indices.dtype, np.int64)
   self.assertEqual(result.values.dtype, np.bool)
   self.assertEqual(result.dense_shape.dtype, np.int64)
   self.assertAllEqual([[0], [2]], result.indices)
   self.assertAllEqual([True, True], result.values)
   self.assertAllEqual([4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例8: test_dense_to_sparse_tensor_1d_float

 def test_dense_to_sparse_tensor_1d_float(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor([1.5, 0.0, 2.3, 0.0])
     result = sess.run(st)
   self.assertEqual(result.indices.dtype, np.int64)
   self.assertEqual(result.values.dtype, np.float32)
   self.assertEqual(result.dense_shape.dtype, np.int64)
   self.assertAllEqual([[0], [2]], result.indices)
   self.assertAllClose([1.5, 2.3], result.values)
   self.assertAllEqual([4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例9: test_dense_to_sparse_tensor_1d

 def test_dense_to_sparse_tensor_1d(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor([1, 0, 2, 0])
     result = sess.run(st)
   self.assertEqual(result.indices.dtype, np.int64)
   self.assertEqual(result.values.dtype, np.int32)
   self.assertEqual(result.dense_shape.dtype, np.int64)
   self.assertAllEqual([[0], [2]], result.indices)
   self.assertAllEqual([1, 2], result.values)
   self.assertAllEqual([4], result.dense_shape)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例10: test_dense_to_sparse_tensor_1d_str_special_ignore

 def test_dense_to_sparse_tensor_1d_str_special_ignore(self):
   with self.test_session() as sess:
     st = sparse_ops.dense_to_sparse_tensor(
         [b'qwe', b'', b'ewq', b''], ignore_value=b'qwe')
     result = sess.run(st)
   self.assertEqual(result.indices.dtype, np.int64)
   self.assertEqual(result.values.dtype, np.object)
   self.assertEqual(result.shape.dtype, np.int64)
   self.assertAllEqual([[1], [2], [3]], result.indices)
   self.assertAllEqual([b'', b'ewq', b''], result.values)
   self.assertAllEqual([4], result.shape)
开发者ID:ComeOnGetMe,项目名称:tensorflow,代码行数:11,代码来源:sparse_ops_test.py

示例11: test_dense_to_sparse_tensor_3d_no_shape

 def test_dense_to_sparse_tensor_3d_no_shape(self):
   with self.test_session() as sess:
     tensor = tf.placeholder(shape=[None, None, None], dtype=tf.int32)
     st = sparse_ops.dense_to_sparse_tensor(tensor)
     result = sess.run(st,
                       feed_dict={
                           tensor: [[[1, 2, 0, 0], [3, 4, 5, 0]],
                                    [[7, 8, 0, 0], [9, 0, 0, 0]]]
                       })
   self.assertAllEqual([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [0, 1, 2],
                        [1, 0, 0], [1, 0, 1], [1, 1, 0]], result.indices)
   self.assertAllEqual([1, 2, 3, 4, 5, 7, 8, 9], result.values)
   self.assertAllEqual([2, 2, 4], result.shape)
开发者ID:ComeOnGetMe,项目名称:tensorflow,代码行数:13,代码来源:sparse_ops_test.py

示例12: _prepare_inputs_for_fit_sparse

  def _prepare_inputs_for_fit_sparse(self,
                                     model_matrix,
                                     response,
                                     model_coefficients_start=None,
                                     convert_to_sparse_tensor=False):
    if model_coefficients_start is None:
      model_coefficients_start = np.zeros(model_matrix.shape[:-2] +
                                          model_matrix.shape[-1:])
    if convert_to_sparse_tensor:
      model_matrix = sparse_ops.dense_to_sparse_tensor(model_matrix)

    model_matrix = self._adjust_dtype_and_shape_hints(model_matrix)
    response = self._adjust_dtype_and_shape_hints(response)
    model_coefficients_start = self._adjust_dtype_and_shape_hints(
        model_coefficients_start)

    return model_matrix, response, model_coefficients_start
开发者ID:asudomoeva,项目名称:probability,代码行数:17,代码来源:proximal_hessian_test.py

示例13: test_convert_to_sparse_undef_shape

 def test_convert_to_sparse_undef_shape(self):
   with self.test_session():
     with self.assertRaises(ValueError):
       tensor = array_ops.placeholder(dtype=dtypes.int32)
       sparse_ops.dense_to_sparse_tensor(tensor)
开发者ID:Immexxx,项目名称:tensorflow,代码行数:5,代码来源:sparse_ops_test.py

示例14: testBowEncoderSparseTensorDenseLookup

 def testBowEncoderSparseTensorDenseLookup(self):
   with self.cached_session():
     docs = [[0, 1]]
     sparse_docs = sparse_ops.dense_to_sparse_tensor(docs)
     with self.assertRaises(TypeError):
       encoders.bow_encoder(sparse_docs, 4, 3, sparse_lookup=False)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:6,代码来源:encoders_test.py


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