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


Python sparse_ops.sparse_merge函数代码示例

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


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

示例1: _construct_sparse_tensors_for_sparse_features

def _construct_sparse_tensors_for_sparse_features(features, tensor_dict):
  """Merges SparseTensors of indices and values of SparseFeatures.

  Updates `tensor_dict`. For `SparseFeatures` in the values of `features`
  expects their `index_key`s and `index_value`s to be present in `tensor_dict`
  mapping to `SparseTensor`s. Removes those, constructs a single `SparseTensor`
  from them, and adds it to `tensor_dict` with the key from `features`.

  Args:
    features: A `dict` mapping feature keys to `SparseFeature` values.
      Values of other types will be ignored.
    tensor_dict: A `dict` mapping feature keys to `Tensor` and `SparseTensor`
      values. Expected to contain keys of the `SparseFeature`s' `index_key`s and
      `value_key`s and mapping them to `SparseTensor`s.
  """
  # Construct SparseTensors for SparseFeatures.
  for key in sorted(features.keys()):
    feature = features[key]
    if isinstance(feature, SparseFeature):
      sp_ids = tensor_dict[feature.index_key]
      sp_values = tensor_dict[feature.value_key]
      tensor_dict[key] = sparse_ops.sparse_merge(
          sp_ids,
          sp_values,
          feature.size,
          feature.already_sorted)
  # Remove tensors from dictionary that were only used to construct
  # SparseTensors for SparseFeature.
  for key in set(tensor_dict.keys()) - set(features.keys()):
    del tensor_dict[key]
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:30,代码来源:parsing_ops.py

示例2: testInt64AndFloat64

  def testInt64AndFloat64(self):
    vocab_size = 50
    with self.test_session(use_gpu=False) as sess:
      indices, values = self._SparseTensor_3x50(dtypes.int64, dtypes.float64)
      sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

      output = sess.run(sp_output)
      self._AssertResultsSorted(output, vocab_size)
开发者ID:govindap,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例3: testInt64AndFloat64Shape

  def testInt64AndFloat64Shape(self):
    vocab_size = [50, 30]
    with test_util.force_cpu():
      indices, values = self._SparseTensor_3x50(np.int64, np.float64)
      sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

      output = self.evaluate(sp_output)
      self._AssertResultsSorted(output, vocab_size)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例4: testInt64AndFloat64Shape

  def testInt64AndFloat64Shape(self):
    vocab_size = [50, 30]
    with self.session(use_gpu=False) as sess:
      indices, values = self._SparseTensor_3x50(np.int64, np.float64)
      sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

      output = sess.run(sp_output)
      self._AssertResultsSorted(output, vocab_size)
开发者ID:abhinav-upadhyay,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例5: testInt64AndFloat32NonCanonicalOrder

    def testInt64AndFloat32NonCanonicalOrder(self):
        vocab_size = 50
        with self.test_session(use_gpu=False) as sess:
            indices, values = self._SparseTensor_3x50(dtypes.int64, dtypes.float32)
            sp_output = sparse_ops.sparse_merge(indices, values, vocab_size, already_sorted=True)

            output = sess.run(sp_output)
            self._AssertResultsNotSorted(output, vocab_size)
开发者ID:tongwang01,项目名称:tensorflow,代码行数:8,代码来源:sparse_ops_test.py

示例6: testInt64AndFloat32NonCanonicalOrder

  def testInt64AndFloat32NonCanonicalOrder(self):
    vocab_size = 50
    with test_util.force_cpu():
      indices, values = self._SparseTensor_3x50(np.int64, np.float32)
      sp_output = sparse_ops.sparse_merge(
          indices, values, vocab_size, already_sorted=True)

      output = self.evaluate(sp_output)
      self._AssertResultsNotSorted(output, vocab_size)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:9,代码来源:sparse_ops_test.py

示例7: testInt32AndFloat32

  def testInt32AndFloat32(self):
    vocab_size = 50
    indices_v, values_v = self._SparseTensorValue_3x50(np.int32, np.float32)
    with self.test_session(use_gpu=False) as sess:
      for indices in (indices_v, ops.SparseTensor.from_value(indices_v)):
        for values in (values_v, ops.SparseTensor.from_value(values_v)):
          sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

          output = sess.run(sp_output)
          self._AssertResultsSorted(output, vocab_size)
开发者ID:821760408-sp,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例8: testInt64AndFloat64NonCanonicalOrder

  def testInt64AndFloat64NonCanonicalOrder(self):
    vocab_size = 50
    vocab_size_tensor = constant_op.constant(vocab_size, dtypes.int64)
    with self.session(use_gpu=False) as sess:
      indices, values = self._SparseTensor_3x50(np.int64, np.float64)
      sp_output = sparse_ops.sparse_merge(
          indices, values, vocab_size_tensor, already_sorted=True)

      output = sess.run(sp_output)
      self._AssertResultsNotSorted(output, vocab_size)
开发者ID:abhinav-upadhyay,项目名称:tensorflow,代码行数:10,代码来源:sparse_ops_test.py

示例9: testInt32AndFloat32

  def testInt32AndFloat32(self):
    vocab_size = 50
    indices_v, values_v = self._SparseTensorValue_3x50(np.int32, np.float32)
    with test_util.force_cpu():
      for indices in (indices_v,
                      sparse_tensor.SparseTensor.from_value(indices_v)):
        for values in (values_v,
                       sparse_tensor.SparseTensor.from_value(values_v)):
          sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

          output = self.evaluate(sp_output)
          self._AssertResultsSorted(output, vocab_size)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:12,代码来源:sparse_ops_test.py

示例10: testShouldSetLastDimensionInDynamicShape

 def testShouldSetLastDimensionInDynamicShape(self):
   with ops.Graph().as_default():
     shape = constant_op.constant([2, 2], dtype=dtypes.int64)
     dynamic_shape = array_ops.placeholder_with_default(shape, shape=[2])
     ids = sparse_tensor.SparseTensor(
         indices=[[0, 0], [0, 1]],
         values=[1, 3],
         dense_shape=dynamic_shape)
     values = sparse_tensor.SparseTensor(
         indices=[[0, 0], [0, 1]],
         values=[0.4, 0.7],
         dense_shape=dynamic_shape)
     merged = sparse_ops.sparse_merge(
         sp_ids=ids, sp_values=values, vocab_size=5)
     self.assertEqual(5, merged.get_shape()[1])
开发者ID:HughKu,项目名称:tensorflow,代码行数:15,代码来源:sparse_ops_test.py

示例11: testInt64AndFloat64

  def testInt64AndFloat64(self):
    vocab_size = 50
    with self.test_session(use_gpu=False) as sess:
      indices, values = self._SparseTensor_3x50(dtypes.int64, dtypes.float64)
      sp_output = sparse_ops.sparse_merge(indices, values, vocab_size)

      output = sess.run(sp_output)
      self.assertAllEqual(
          output.indices,
          [[0, 0], [1, 10], [1, 13], [1, 14], [2, 32], [2, 33]])
      self.assertAllEqual(
          output.values,
          [-3, 1, 4, 1, 5, 9])
      self.assertAllEqual(
          output.shape,
          [3, vocab_size])
开发者ID:0-T-0,项目名称:tensorflow,代码行数:16,代码来源:sparse_ops_test.py

示例12: _construct_sparse_tensors_for_sparse_features

def _construct_sparse_tensors_for_sparse_features(features, tensor_dict):
  """Merges SparseTensors of indices and values of SparseFeatures.

  Constructs new dict based on `tensor_dict`. For `SparseFeatures` in the values
  of `features` expects their `index_key`s and `index_value`s to be present in
  `tensor_dict` mapping to `SparseTensor`s. Constructs a single `SparseTensor`
  from them, and adds it to the result with the key from `features`.
  Copies other keys and values from `tensor_dict` with keys present in
  `features`.

  Args:
    features: A `dict` mapping feature keys to `SparseFeature` values.
      Values of other types will be ignored.
    tensor_dict: A `dict` mapping feature keys to `Tensor` and `SparseTensor`
      values. Expected to contain keys of the `SparseFeature`s' `index_key`s and
      `value_key`s and mapping them to `SparseTensor`s.
  Returns:
    A `dict` mapping feature keys to `Tensor` and `SparseTensor` values. Similar
    to `tensor_dict` except each `SparseFeature`s in `features` results in a
    single `SparseTensor`.
  """
  tensor_dict = dict(tensor_dict)  # Do not modify argument passed in.
  # Construct SparseTensors for SparseFeatures.
  for key in sorted(features.keys()):
    feature = features[key]
    if isinstance(feature, SparseFeature):
      if isinstance(feature.index_key, str):
        sp_ids = tensor_dict[feature.index_key]
      else:
        sp_ids = [tensor_dict[index_key] for index_key in feature.index_key]
      sp_values = tensor_dict[feature.value_key]
      tensor_dict[key] = sparse_ops.sparse_merge(
          sp_ids,
          sp_values,
          vocab_size=feature.size,
          already_sorted=feature.already_sorted)
  # Remove tensors from dictionary that were only used to construct
  # SparseTensors for SparseFeature.
  for key in set(tensor_dict) - set(features):
    del tensor_dict[key]
  return tensor_dict
开发者ID:Immexxx,项目名称:tensorflow,代码行数:41,代码来源:parsing_ops.py

示例13: _training_examples_and_variables

    def _training_examples_and_variables():
      """Returns dictionaries for training examples and variables."""
      batch_size = targets.get_shape()[0]

      # Iterate over all feature columns and create appropriate lists for dense
      # and sparse features as well as dense and sparse weights (variables) for
      # SDCA.
      # TODO(sibyl-vie3Poto): Reshape variables stored as values in column_to_variables
      # dict as 1-dimensional tensors.
      dense_features, sparse_features = [], []
      dense_features_weights, sparse_features_weights = [], []
      for column in sorted(set(linear_feature_columns), key=lambda x: x.key):
        transformed_tensor = features[column]
        if isinstance(column, layers.feature_column.
                      _RealValuedColumn):  # pylint: disable=protected-access
          # A real-valued column corresponds to a dense feature in SDCA.
          if column.dimension != 1:
            raise ValueError(
                "Invalid column dimension %d for column %s. SDCAOptimizer "
                "supports only 1-dimensional dense feature columns." %
                (column.dimension, column.name))

          dense_features.append(array_ops.reshape(transformed_tensor,
                                                  shape=[-1]))
          # For real valued columns, the variables list contains exactly one
          # element.
          dense_features_weights.append(columns_to_variables[column][0])
        elif isinstance(column, layers.feature_column.
                        _BucketizedColumn):  # pylint: disable=protected-access
          # A bucketized column corresponds to a sparse feature in SDCA. The
          # bucketized feature is "sparsified" for SDCA by converting it to a
          # SparseTensor respresenting the one-hot encoding of the bucketized
          # feature.
          dense_bucket_tensor = column.to_dnn_input_layer(transformed_tensor)
          sparse_bucket_tensor = _dense_to_sparse_tensor(dense_bucket_tensor)
          sparse_features.append(sparse_bucket_tensor)
          # For bucketized columns, the variables list contains exactly one
          # element.
          sparse_features_weights.append(columns_to_variables[column][0])
        elif isinstance(column,
                        (layers.feature_column.
                         _CrossedColumn,  # pylint: disable=protected-access
                         layers.feature_column._SparseColumn
                        )):  # pylint: disable=protected-access
          weights_tensor = ops.SparseTensor(
              indices=transformed_tensor.indices,
              values=array_ops.ones_like(transformed_tensor.values),
              shape=transformed_tensor.shape)
          sparse_features_tensor = sparse_ops.sparse_merge(transformed_tensor,
                                                           weights_tensor,
                                                           column.length)
          sparse_features.append(math_ops.to_float(sparse_features_tensor))
          sparse_features_weights.append(columns_to_variables[column][0])
        elif isinstance(
            column,
            layers.feature_column._WeightedSparseColumn):  # pylint: disable=protected-access
          id_tensor = column.id_tensor(transformed_tensor)
          weight_tensor = column.weight_tensor(transformed_tensor)
          sparse_features_tensor = sparse_ops.sparse_merge(
              id_tensor, weight_tensor, column.length,
              name="{}_sparse_merge".format(column.name))
          sparse_features.append(math_ops.to_float(
              sparse_features_tensor, name="{}_to_float".format(column.name)))
          sparse_features_weights.append(columns_to_variables[column][0])
        else:
          raise ValueError("SDCAOptimizer does not support column type %s." %
                           type(column).__name__)

      example_weights = array_ops.reshape(
          features[weight_column_name],
          shape=[-1]) if weight_column_name else array_ops.ones([batch_size])
      example_ids = features[self._example_id_column]
      examples = dict(
          sparse_features=sparse_features,
          dense_features=dense_features,
          example_labels=math_ops.to_float(
              array_ops.reshape(targets, shape=[-1])),
          example_weights=example_weights,
          example_ids=example_ids)
      sdca_variables = dict(sparse_features_weights=sparse_features_weights,
                            dense_features_weights=dense_features_weights)
      return examples, sdca_variables
开发者ID:31H0B1eV,项目名称:tensorflow,代码行数:82,代码来源:sdca_optimizer.py


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