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


Python v1.SparseTensor方法代码示例

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


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

示例1: _build

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def _build(self):
    dataset = tfds.load(name=self._dataset_name, split=self._mode)
    minibatch = dataset.map(parse).repeat()

    if self._shuffle:
      minibatch = minibatch.shuffle(self._batch_size*100)
    minibatch = minibatch.batch(
        self._batch_size).make_one_shot_iterator().get_next()
    minibatch['sentiment'].set_shape([self._batch_size])
    minibatch['sentence'] = tf.SparseTensor(
        indices=minibatch['sentence'].indices,
        values=minibatch['sentence'].values,
        dense_shape=[self._batch_size, minibatch['sentence'].dense_shape[1]])
    # minibatch.sentence sparse tensor with dense shape
    # [batch_size x seq_length], length: [batch_size]
    return Dataset(
        tokens=minibatch['sentence'],
        num_tokens=self.get_row_lengths(minibatch['sentence']),
        sentiment=minibatch['sentiment'],
    ) 
开发者ID:deepmind,项目名称:interval-bound-propagation,代码行数:22,代码来源:robust_model.py

示例2: parse

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def parse(data_dict):
  """Parse dataset from _data_gen into the same format as sst_binary."""
  sentiment = data_dict['label']
  sentence = data_dict['sentence']
  dense_chars = tf.decode_raw(sentence, tf.uint8)
  dense_chars.set_shape((None,))
  chars = tfp.math.dense_to_sparse(dense_chars)
  if six.PY3:
    safe_chr = lambda c: '?' if c >= 128 else chr(c)
  else:
    safe_chr = chr
  to_char = np.vectorize(safe_chr)
  chars = tf.SparseTensor(indices=chars.indices,
                          values=tf.py_func(to_char, [chars.values], tf.string),
                          dense_shape=chars.dense_shape)
  return {'sentiment': sentiment,
          'sentence': chars} 
开发者ID:deepmind,项目名称:interval-bound-propagation,代码行数:19,代码来源:robust_model.py

示例3: _sparse_tensor_to_numpy_dict

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def _sparse_tensor_to_numpy_dict(
    sparse_tensor):
  """Convert a tf.SparseTensor into a dictionary of numpy arrays.

  Args:
    sparse_tensor: A SparseTensor of the trained relation.

  Returns:
    A dictionary representing the data.
  """
  return {
      'shape': sparse_tensor.dense_shape,
      'rows': numpy.array(sparse_tensor.indices[:, 0]),
      'cols': numpy.array(sparse_tensor.indices[:, 1]),
      'values': numpy.array(sparse_tensor.values)
  } 
开发者ID:google-research,项目名称:language,代码行数:18,代码来源:io.py

示例4: declare_relation

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def declare_relation(self,
                       rel_name,
                       domain_type,
                       range_type,
                       trainable = False,
                       dense = False,
                       gpus = 0):
    """Declare the domain and range types for a relation.

    Args:
      rel_name: string naming a relation
      domain_type: string naming the type of subject entities for the relation
      range_type: string naming the type of object entities for the relation
      trainable: boolean, true if the weights for this relation will be trained
      dense: if true, store data as a dense tensor instead of a SparseTensor
      gpus: number of gpus available for computation

    Raises:
      RelationNameError: If a relation with this name already exists.
    """
    super(DistributedNeuralQueryContext,
          self).declare_relation(rel_name, domain_type, range_type, trainable,
                                 dense)
    if gpus <= 0:
      self._declaration[rel_name].underlying_parameters = [None] * gpus 
开发者ID:google-research,项目名称:language,代码行数:27,代码来源:dist.py

示例5: sparse_dense_mul

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def sparse_dense_mul(sp_mat, dense_mat):
  """Element-wise multiplication between sparse and dense tensors.

  Returns a sparse tensor. Limited broadcasting of dense_mat is supported.
  If rank(dense_mat) < rank(sparse_mat), then dense_mat is broadcasted on the
  rightmost dimensions to match sparse_mat.

  Args:
    sp_mat: SparseTensor.
    dense_mat: DenseTensor with rank <= sp_mat.

  Returns:
    SparseTensor.
  """
  rank = dense_mat.get_shape().ndims
  indices = sp_mat.indices[:, :rank]
  dense_values = tf.gather_nd(dense_mat, indices)
  return tf.SparseTensor(sp_mat.indices, sp_mat.values * dense_values,
                         sp_mat.dense_shape) 
开发者ID:google-research,项目名称:language,代码行数:21,代码来源:model_fns.py

示例6: sparse_reduce

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def sparse_reduce(sp_tensor, rank, agg_fn="sum", axis=-1):
  """Reduce SparseTensor along the given axis.

  Args:
    sp_tensor: SparseTensor of arbitrary rank.
    rank: Integer rank of the sparse tensor.
    agg_fn: Reduce function for aggregation.
    axis: Integer specifying axis to sum over.

  Returns:
    sp_tensor: SparseTensor of one less rank.
  """
  if axis < 0:
    axis = rank + axis
  axes_to_keep = tf.one_hot(
      axis, rank, on_value=False, off_value=True, dtype=tf.bool)
  indices_to_keep = tf.boolean_mask(sp_tensor.indices, axes_to_keep, axis=1)
  new_shape = tf.boolean_mask(sp_tensor.dense_shape, axes_to_keep)
  indices_to_keep.set_shape([None, rank - 1])
  indices, values = aggregate_sparse_indices(
      indices_to_keep, sp_tensor.values, new_shape, agg_fn=agg_fn)
  return tf.sparse.reorder(tf.SparseTensor(indices, values, new_shape)) 
开发者ID:google-research,项目名称:language,代码行数:24,代码来源:model_fns.py

示例7: remove_from_sparse

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def remove_from_sparse(sp_tensor, remove_indices):
  """Remove indices from SparseTensor if present."""
  # 1. create 1d index maps
  scaling_vector = tf.cumprod(
      tf.cast(tf.shape(sp_tensor), sp_tensor.indices.dtype))
  a1s = tf.linalg.matvec(sp_tensor.indices, scaling_vector)
  b1s = tf.linalg.matvec(remove_indices, scaling_vector)

  # 2. get relevant indices of sp_a
  int_idx = tf.setdiff1d(a1s, b1s)[1]
  to_retain = tf.sparse_to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(a1s),
      default_value=0.0,
      sparse_values=1.0) > 0.5
  return tf.sparse.retain(sp_tensor, to_retain) 
开发者ID:google-research,项目名称:language,代码行数:18,代码来源:model_fns.py

示例8: load_sparse_matrix

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def load_sparse_matrix(var_name, checkpoint_path):
  """Load sparse matrix from checkpoint."""
  with tf.gfile.Open(checkpoint_path + ".info") as f:
    num_nnz = int(f.read())
  tf_data = tf.get_local_variable(
      var_name + "_data", shape=[num_nnz], dtype=tf.float32, use_resource=True)
  tf_indices = tf.get_local_variable(
      var_name + "_indices",
      shape=[num_nnz, 2],
      dtype=tf.int64,
      use_resource=True)
  tf_shape = tf.get_local_variable(
      var_name + "_shape", shape=[2], dtype=tf.int64, use_resource=True)
  init_from_checkpoint(
      checkpoint_path, target_variables=[tf_data, tf_indices, tf_shape])
  tf_sp = tf.SparseTensor(tf_indices, tf_data, tf_shape)
  return tf_sp 
开发者ID:google-research,项目名称:language,代码行数:19,代码来源:search_utils.py

示例9: _reshape_context_features

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def _reshape_context_features(self, keys_to_tensors):
    """Reshape context features.

    The instance context_features are reshaped to
      [num_context_features, context_feature_length]

    Args:
      keys_to_tensors: a dictionary from keys to tensors.

    Returns:
      A 2-D float tensor of shape [num_context_features, context_feature_length]
    """
    context_feature_length = keys_to_tensors['image/context_feature_length']
    to_shape = tf.cast(tf.stack([-1, context_feature_length]), tf.int32)
    context_features = keys_to_tensors['image/context_features']
    if isinstance(context_features, tf.SparseTensor):
      context_features = tf.sparse_tensor_to_dense(context_features)
    context_features = tf.reshape(context_features, to_shape)
    return context_features 
开发者ID:tensorflow,项目名称:models,代码行数:21,代码来源:tf_sequence_example_decoder.py

示例10: _reshape_keypoints

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def _reshape_keypoints(self, keys_to_tensors):
    """Reshape keypoints.

    The keypoints are reshaped to [num_instances, num_keypoints, 2].

    Args:
      keys_to_tensors: a dictionary from keys to tensors. Expected keys are:
        'image/object/keypoint/x'
        'image/object/keypoint/y'

    Returns:
      A 3-D float tensor of shape [num_instances, num_keypoints, 2] with values
        in [0, 1].
    """
    y = keys_to_tensors['image/object/keypoint/y']
    if isinstance(y, tf.SparseTensor):
      y = tf.sparse_tensor_to_dense(y)
    y = tf.expand_dims(y, 1)
    x = keys_to_tensors['image/object/keypoint/x']
    if isinstance(x, tf.SparseTensor):
      x = tf.sparse_tensor_to_dense(x)
    x = tf.expand_dims(x, 1)
    keypoints = tf.concat([y, x], 1)
    keypoints = tf.reshape(keypoints, [-1, self._num_keypoints, 2])
    return keypoints 
开发者ID:tensorflow,项目名称:models,代码行数:27,代码来源:tf_example_decoder.py

示例11: _reshape_instance_masks

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def _reshape_instance_masks(self, keys_to_tensors):
    """Reshape instance segmentation masks.

    The instance segmentation masks are reshaped to [num_instances, height,
    width].

    Args:
      keys_to_tensors: a dictionary from keys to tensors.

    Returns:
      A 3-D float tensor of shape [num_instances, height, width] with values
        in {0, 1}.
    """
    height = keys_to_tensors['image/height']
    width = keys_to_tensors['image/width']
    to_shape = tf.cast(tf.stack([-1, height, width]), tf.int32)
    masks = keys_to_tensors['image/object/mask']
    if isinstance(masks, tf.SparseTensor):
      masks = tf.sparse_tensor_to_dense(masks)
    masks = tf.reshape(
        tf.cast(tf.greater(masks, 0.0), dtype=tf.float32), to_shape)
    return tf.cast(masks, tf.float32) 
开发者ID:tensorflow,项目名称:models,代码行数:24,代码来源:tf_example_decoder.py

示例12: get_sparse_variable

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def get_sparse_variable(name, indices, shape, dtype=None, trainable=True,
                        initializer=None, partitioner=None, regularizer=None):
  n = len(indices)
  values = tf.get_variable(name, [n], dtype=dtype,
                           initializer=initializer, partitioner=partitioner,
                           regularizer=regularizer, trainable=trainable)
  return tf.sparse_reorder(
      tf.SparseTensor(indices=indices, values=values, dense_shape=shape)) 
开发者ID:deepmind,项目名称:lamb,代码行数:10,代码来源:utils.py

示例13: sparse_equals_constant

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def sparse_equals_constant(constant, tensor):
  return tf.SparseTensor(
      indices=tensor.indices,
      dense_shape=tensor.dense_shape,
      values=tf.equal(tensor.values, constant)) 
开发者ID:tensorflow,项目名称:tensor2tensor,代码行数:7,代码来源:common_layers.py

示例14: sparse_expand_dims

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def sparse_expand_dims(tensor, current_num_dims, axis=0):
  if axis == -1:
    axis = current_num_dims

  new_col = tf.zeros([tf.shape(tensor.indices)[0]], dtype=tf.int64)
  cols = tf.unstack(tensor.indices, axis=1, num=current_num_dims)
  shape = tf.unstack(tensor.dense_shape, num=current_num_dims)
  new_indices = tf.stack(cols[:axis] + [new_col] + cols[axis:], axis=1)
  return tf.SparseTensor(
      indices=new_indices,
      values=tensor.values,
      dense_shape=tf.stack(shape[:axis] + [1] + shape[axis:])) 
开发者ID:tensorflow,项目名称:tensor2tensor,代码行数:14,代码来源:common_layers.py

示例15: sparse_add_constant

# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import SparseTensor [as 别名]
def sparse_add_constant(constant, tensor):
  return tf.SparseTensor(
      indices=tensor.indices,
      values=constant + tensor.values,
      dense_shape=tensor.dense_shape) 
开发者ID:tensorflow,项目名称:tensor2tensor,代码行数:7,代码来源:common_layers.py


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