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


Python tensorflow.sort方法代码示例

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


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

示例1: take_top_p_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def take_top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, sequence, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch)[:, tf.newaxis],
        tf.range(0, sequence)[tf.newaxis, :],
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits < min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    ) 
开发者ID:openai,项目名称:lm-human-preferences,代码行数:19,代码来源:core.py

示例2: call

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def call(self, graph, feat):
        r"""Compute sort pooling.

        Parameters
        ----------
        graph : DGLGraph
            The graph.
        feat : tf.Tensor
            The input feature with shape :math:`(N, D)` where
            :math:`N` is the number of nodes in the graph.

        Returns
        -------
        tf.Tensor
            The output feature with shape :math:`(B, k * D)`, where
            :math:`B` refers to the batch size.
        """
        with graph.local_scope():
            # Sort the feature of each node in ascending order.
            feat = tf.sort(feat, -1)
            graph.ndata['h'] = feat
            # Sort nodes according to their last features.
            ret = tf.reshape(topk_nodes(graph, 'h', self.k, idx=-1)[0], (
                -1, self.k * feat.shape[-1]))
            return ret 
开发者ID:dmlc,项目名称:dgl,代码行数:27,代码来源:glob.py

示例3: _iteration_l_moments_dense

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def _iteration_l_moments_dense(
    current_index, l1_sum, l2_sum, l3_sum, l4_sum, l1_factors, l2_factors,
    l3_factors, l4_factors, x_rank_2):
  """Process one column of a `Tensor` and updates L-moments variables."""
  current_x = x_rank_2[:, current_index]
  sorted_x = tf.sort(current_x)

  dim_1 = tf.shape(x_rank_2)[1]
  new_l1_sum = l1_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l1_factors), axis=0)], [dim_1])
  new_l2_sum = l2_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l2_factors), axis=0)], [dim_1])
  new_l3_sum = l3_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l3_factors), axis=0)], [dim_1])
  new_l4_sum = l4_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l4_factors), axis=0)], [dim_1])
  return (tf.add(current_index, 1),
          new_l1_sum, new_l2_sum, new_l3_sum, new_l4_sum, l1_factors,
          l2_factors, l3_factors, l4_factors, x_rank_2) 
开发者ID:tensorflow,项目名称:transform,代码行数:25,代码来源:tf_utils.py

示例4: top_p_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction="DESCENDING", axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack(
        [
            tf.range(0, batch),
            # number of indices to include
            tf.maximum(
                tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0
            ),
        ],
        axis=-1,
    )
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(logits < min_values, tf.ones_like(logits) * -1e10, logits,) 
开发者ID:storybro,项目名称:storybro,代码行数:19,代码来源:sample.py

示例5: _single_token_mask

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def _single_token_mask(inputs, tgt_len, num_predict):
  """Sample individual tokens as prediction targets."""
  all_indices = tf.range(tgt_len, dtype=tf.int64)
  non_func_mask = tf.logical_and(
      tf.not_equal(inputs, SEP_ID),
      tf.not_equal(inputs, CLS_ID))
  non_func_indices = tf.boolean_mask(all_indices, non_func_mask)

  masked_pos = tf.random.shuffle(non_func_indices)
  masked_pos = tf.sort(masked_pos[:num_predict])
  target_mask = tf.sparse_to_dense(
      sparse_indices=masked_pos,
      output_shape=[tgt_len],
      sparse_values=1.0,
      default_value=0.0)

  is_masked = tf.cast(target_mask, tf.bool)

  return is_masked, target_mask 
开发者ID:tensorflow,项目名称:models,代码行数:21,代码来源:data_utils.py

示例6: sort_1d

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def sort_1d(input):
    return tf.sort(input), tf.cast(tf.argsort(input), dtype=tf.int64) 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:tensor.py

示例7: gumbel_softmax

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def gumbel_softmax(logits, temperature, gumbel_samples=None, samples=1, greedy=False): 
	""" Draw a sample from the Gumbel-Softmax distribution"""
	input_shape_list = bert_utils.get_shape_list(logits, expected_rank=2)
	if samples > 1:
		logits = tf.expand_dims(logits, -1)
	if gumbel_samples is None:
		gumbel_samples = sample_gumbel(input_shape_list, samples)
	if greedy:
		tf.logging.info("==apply greedy based sampling and discrete relax==")
		# if int(tf.__version__.split(".")[1]) < 15:
		# 	if not use_tpu:
		# 		logits_index = tf.contrib.framework.argsort(logits, axis=1)
		# 		gumbel_samples_sorted = tf.contrib.framework.sort(gumbel_samples, axis=1)
		# 		gumbel_samples_sorted = reorder(gumbel_samples_sorted, logits_index)
		# 	else:

		# else:
		# 	logits_index = tf.argsort(logits, axis=1)
		# 	gumbel_samples_sorted = tf.sort(gumbel_samples, axis=1)
		# 	gumbel_samples_sorted = reorder(gumbel_samples_sorted, logits_index)

		gumbel_samples = reorder_approximate(logits, gumbel_samples)
		y = logits + gumbel_samples
		return [tf.exp(tf.nn.log_softmax(y / temperature, axis=1)),
				y]
	else:
		y = logits + gumbel_samples
		tf.logging.info("==apply sampling based sampling and discrete relax==")
		return [tf.exp(tf.nn.log_softmax(y / temperature, axis=1)), 
				y] 
开发者ID:yyht,项目名称:BERT,代码行数:32,代码来源:token_generator_gumbel.py

示例8: sort_key_val

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def sort_key_val(t1, t2, dim=-1):
    values = tf.sort(t1, axis=dim)
    t2 = tf.broadcast_to(t2, t1.shape)
    return values, tf.gather(t2, tf.argsort(t1, axis=dim), axis=dim) 
开发者ID:yyht,项目名称:BERT,代码行数:6,代码来源:reformer_utils.py

示例9: _apply

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def _apply(self, words):
    if self.count == 0:
      return tf.identity(words)
    num_words = tf.shape(words)[0]
    indices = tf.range(num_words)
    shuffle_indices = tf.random.shuffle(indices)
    keep_count = tf.maximum(num_words - self.count, 1)
    keep_indices = tf.sort(shuffle_indices[:keep_count])
    return tf.gather(words, keep_indices) 
开发者ID:OpenNMT,项目名称:OpenNMT-tf,代码行数:11,代码来源:noise.py

示例10: percent_confidence_mask_unsup

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def percent_confidence_mask_unsup(self, logits_y, labels_y, loss_l2u):
        # Adapted from google-research/uda/image/main.py

        # This function masks the unsupervised predictions that are below
        # a set confidence threshold. # Note the following will only work
        # using MSE loss and not KL-divergence.

        # Calculate largest predicted probability for each image.
        unsup_prob = tf.nn.softmax(logits_y, axis=-1)
        largest_prob = tf.reduce_max(unsup_prob, axis=-1)

        # Get the indices of the bottom x% of probabilities and mask those out.
        # In other words, get the probability of the image with the x%*#numofsamples
        # lowest probability and use that as the mask.

        # Calculate the current confidence_mask value using the specified schedule:
        sorted_probs = tf.sort(largest_prob, axis=-1, direction='ASCENDING')
        sort_index = tf.math.multiply(tf.to_float(tf.shape(sorted_probs)[0]), FLAGS.percent_mask)
        curr_confidence_mask = tf.slice(sorted_probs, [tf.to_int64(sort_index)], [1])

        # Mask the loss for images that don't contain a predicted
        # probability above the threshold.
        loss_mask = tf.cast(tf.greater(largest_prob, curr_confidence_mask), tf.float32)
        tf.summary.scalar('losses/high_prob_ratio', tf.reduce_mean(loss_mask)) # The ratio of unl images above the thresh
        tf.summary.scalar('losses/percent_confidence_mask', tf.reshape(curr_confidence_mask,[]))
        loss_mask = tf.stop_gradient(loss_mask)
        loss_l2u = loss_l2u * tf.expand_dims(loss_mask, axis=-1)

        # Return the average unsupervised loss.
        avg_unsup_loss = (tf.reduce_sum(loss_l2u) /
                        tf.maximum(tf.reduce_sum(loss_mask) * FLAGS.nclass, 1))
        return avg_unsup_loss 
开发者ID:uizard-technologies,项目名称:realmix,代码行数:34,代码来源:realmix.py

示例11: _iteration_l_moments_sparse

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def _iteration_l_moments_sparse(
    current_index, l1_sum, l2_sum, l3_sum, l4_sum, count_samples,
    count_pairs, count_triplets, count_quadruplets, x_rank_2):
  """Process one column of a `SparseTensor` and updates L-moments variables."""
  current_x = tf.boolean_mask(
      x_rank_2.values,
      tf.math.equal(x_rank_2.indices[:, 1], [current_index]))
  sorted_x = tf.sort(current_x, axis=0)
  num_samples = tf.shape(current_x)[0]
  (current_samples, current_pairs, current_triplets, current_quadruplets,
   l1_factors, l2_factors, l3_factors,
   l4_factors) = _num_terms_and_factors(num_samples, x_rank_2.values.dtype)

  dim_1 = x_rank_2.dense_shape[1]
  new_l1_sum = l1_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l1_factors), axis=0)], [dim_1])
  new_l2_sum = l2_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l2_factors), axis=0)], [dim_1])
  new_l3_sum = l3_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l3_factors), axis=0)], [dim_1])
  new_l4_sum = l4_sum + tf.scatter_nd(
      [[current_index]],
      [tf.reduce_sum(tf.multiply(sorted_x, l4_factors), axis=0)], [dim_1])

  new_count_samples = count_samples + tf.scatter_nd(
      [[current_index]], [current_samples], [dim_1])
  new_count_pairs = count_pairs + tf.scatter_nd(
      [[current_index]], [current_pairs], [dim_1])
  new_count_triplets = count_triplets + tf.scatter_nd(
      [[current_index]], [current_triplets], [dim_1])
  new_count_quadruplets = count_quadruplets + tf.scatter_nd(
      [[current_index]], [current_quadruplets], [dim_1])

  return (tf.add(current_index, 1),
          new_l1_sum, new_l2_sum, new_l3_sum, new_l4_sum,
          new_count_samples, new_count_pairs, new_count_triplets,
          new_count_quadruplets, x_rank_2) 
开发者ID:tensorflow,项目名称:transform,代码行数:42,代码来源:tf_utils.py

示例12: top_p_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def top_p_logits(logits, p):
	"""Took from OpenAI GPT-2 Implememtation"""
	batch = tf.shape(logits)[0]
	sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
	cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
	indices = tf.stack([
		tf.range(0, batch),
		tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0),
	], axis=-1)
	min_values = tf.gather_nd(sorted_logits, indices)
	return tf.where(
		logits < min_values,
		tf.ones_like(logits) * -1e10,
		logits,
	) 
开发者ID:akanyaani,项目名称:gpt-2-tensorflow2.0,代码行数:17,代码来源:sample.py

示例13: top_p_logits

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def top_p_logits(logits, p):
    with tf.variable_scope('top_p_logits'):
        logits_sort = tf.sort(logits, direction='DESCENDING')

        # logits_sort = (logits_sort[i,:] for i in range(logits_sort.get_shape().as_list()[0]))
        probs_sort = tf.nn.softmax(logits_sort)
        probs_sums = tf.cumsum(probs_sort, axis=1, exclusive=True)

        logits_masked = tf.where(probs_sums < p, logits_sort, tf.ones_like(logits_sort)*1000) # [batchsize, vocab]
        min_logits = tf.reduce_min(logits_masked, axis=1, keepdims=True) # [batchsize, 1]
        return tf.where(
            logits < min_logits,
            tf.ones_like(logits, dtype=logits.dtype) * -1e10,
            logits,
        ) 
开发者ID:galois-autocompleter,项目名称:galois-autocompleter,代码行数:17,代码来源:sample.py

示例14: sample_token

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def sample_token(logits):
    """
    Inputs:
    logits: tf.Tensor([batch_size,len,num_tokens])
    Outpus:
    samples: tf.Tensor([batch_size,len,1])
    """
    # credits: https://github.com/nshepperd/gpt-2

    logits /= FLAGS.temperature

    batch_size = tf.shape(logits)[0]
    seq_len = tf.shape(logits)[1]
    num_toks = tf.shape(logits)[2]

    if sampling_strategy() == 'top_p':
        logits_sorted = tf.sort(logits,
                                direction="DESCENDING",
                                axis=-1)
        probs = tf.nn.softmax(logits_sorted, axis=-1)
        cum_probs = tf.math.cumsum(probs,
                                   axis=-1,
                                   exclusive=True)
        logits_masked = tf.where(cum_probs < FLAGS.top_p,
                                 logits_sorted,
                                 tf.ones_like(logits_sorted) * 100)
        min_logits = tf.reduce_min(logits_masked, axis=-1)

        logits_masked = tf.where(logits < min_logits,
                                 tf.ones_like(logits) * -1e10,
                                 logits)

    elif sampling_strategy() == "top_k":
        if FLAGS.top_k != 0:
            values, _ = tf.nn.top_k(logits, k=FLAGS.top_k)
            min_values = values[:, :, -1:]
            logits_masked = tf.where(
                logits < min_values,
                tf.ones_like(logits, dtype=logits.dtype) * -1e10,
                logits,
            )
    else:
        raise NotImplementedError("Invalid sampling strategy")

    logits_masked = tf.reshape(logits_masked, (-1, num_toks))

    samples = tf.random.categorical(logits_masked,
                                    num_samples=1,
                                    dtype=tf.int32)

    probs = tf.nn.softmax(tf.reshape(logits, (-1, num_toks)), axis=-1)
    confidences = tf.gather_nd(params=probs, batch_dims=1, indices=samples)

    return tf.reshape(samples, (batch_size, seq_len, 1)),\
        tf.reshape(confidences, (batch_size, seq_len, 1)) 
开发者ID:rusiaaman,项目名称:XLnet-gen,代码行数:57,代码来源:language_generation.py

示例15: clip_eta

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sort [as 别名]
def clip_eta(eta, ord, eps):
  """
  Helper function to clip the perturbation to epsilon norm ball.
  :param eta: A tensor with the current perturbation.
  :param ord: Order of the norm (mimics Numpy).
              Possible values: np.inf, 1 or 2.
  :param eps: Epsilon, bound of the perturbation.
  """

  # Clipping perturbation eta to self.ord norm ball
  if ord not in [np.inf, 1, 2]:
    raise ValueError('ord must be np.inf, 1, or 2.')
  reduc_ind = list(xrange(1, len(eta.get_shape())))
  avoid_zero_div = 1e-12
  if ord == np.inf:
    eta = clip_by_value(eta, -eps, eps)
  elif ord == 1:
    # Implements a projection algorithm onto the l1-ball from
    # (Duchi et al. 2008) that runs in time O(d*log(d)) where d is the
    # input dimension.
    # Paper link (Duchi et al. 2008): https://dl.acm.org/citation.cfm?id=1390191

    eps = tf.cast(eps, eta.dtype)

    dim = tf.reduce_prod(tf.shape(eta)[1:])
    eta_flat = tf.reshape(eta, (-1, dim))
    abs_eta = tf.abs(eta_flat)

    if 'sort' in dir(tf):
      mu = -tf.sort(-abs_eta, axis=-1)
    else:
      # `tf.sort` is only available in TF 1.13 onwards
      mu = tf.nn.top_k(abs_eta, k=dim, sorted=True)[0]
    cumsums = tf.cumsum(mu, axis=-1)
    js = tf.cast(tf.divide(1, tf.range(1, dim + 1)), eta.dtype)
    t = tf.cast(tf.greater(mu - js * (cumsums - eps), 0), eta.dtype)

    rho = tf.argmax(t * cumsums, axis=-1)
    rho_val = tf.reduce_max(t * cumsums, axis=-1)
    theta = tf.divide(rho_val - eps, tf.cast(1 + rho, eta.dtype))

    eta_sgn = tf.sign(eta_flat)
    eta_proj = eta_sgn * tf.maximum(abs_eta - theta[:, tf.newaxis], 0)
    eta_proj = tf.reshape(eta_proj, tf.shape(eta))

    norm = tf.reduce_sum(tf.abs(eta), reduc_ind)
    eta = tf.where(tf.greater(norm, eps), eta_proj, eta)

  elif ord == 2:
    # avoid_zero_div must go inside sqrt to avoid a divide by zero
    # in the gradient through this operation
    norm = tf.sqrt(tf.maximum(avoid_zero_div,
                              reduce_sum(tf.square(eta),
                                         reduc_ind,
                                         keepdims=True)))
    # We must *clip* to within the norm ball, not *normalize* onto the
    # surface of the ball
    factor = tf.minimum(1., div(eps, norm))
    eta = eta * factor
  return eta 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:62,代码来源:utils_tf.py


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