本文整理汇总了Python中tensorflow.compat.v1.argsort方法的典型用法代码示例。如果您正苦于以下问题:Python v1.argsort方法的具体用法?Python v1.argsort怎么用?Python v1.argsort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.argsort方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _select_top_k
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def _select_top_k(logits, top_k):
"""Replaces logits, expect the top k highest values, with small number (-1e6).
If k is -1 don't replace anything.
Args:
logits: A `Tensor` of shape [batch_size, ..., vocab_size]
top_k: vector of batch size.
Returns:
A `Tensor` with same shape as logits.
"""
vocab_size = logits.shape[-1]
top_k = tf.where(
tf.not_equal(top_k, -1), top_k,
tf.ones_like(top_k) * vocab_size)
return tf.where(
tf.argsort(logits) < tf.reshape(top_k, [-1] + [1] *
(len(logits.shape) - 1)), logits,
tf.ones_like(logits) * -1e6)
示例2: _classification_loss
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def _classification_loss(self, pred_label, gt_label, num_matched_boxes):
"""Computes the classification loss.
Computes the classification loss with hard negative mining.
Args:
pred_label: a flatten tensor that includes all predicted class. The shape
is [batch_size, num_anchors, num_classes].
gt_label: a tensor that represents the classification groundtruth targets.
The shape is [batch_size, num_anchors, 1].
num_matched_boxes: the number of anchors that are matched to a groundtruth
targets. This is used as the loss normalizater.
Returns:
box_loss: a float32 representing total box regression loss.
"""
cross_entropy = tf.losses.sparse_softmax_cross_entropy(
gt_label, pred_label, reduction=tf.losses.Reduction.NONE)
mask = tf.greater(tf.squeeze(gt_label), 0)
float_mask = tf.cast(mask, tf.float32)
# Hard example mining
neg_masked_cross_entropy = cross_entropy * (1 - float_mask)
relative_position = tf.argsort(
tf.argsort(
neg_masked_cross_entropy, direction='DESCENDING'))
num_neg_boxes = tf.minimum(
tf.to_int32(num_matched_boxes) * ssd_constants.NEGS_PER_POSITIVE,
ssd_constants.NUM_SSD_BOXES)
top_k_neg_mask = tf.cast(tf.less(
relative_position,
tf.tile(num_neg_boxes[:, tf.newaxis], (1, ssd_constants.NUM_SSD_BOXES))
), tf.float32)
class_loss = tf.reduce_sum(
tf.multiply(cross_entropy, float_mask + top_k_neg_mask), axis=1)
return tf.reduce_mean(class_loss / num_matched_boxes)
示例3: framework
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def framework(msg='err'):
"""Return framework module or dummy version."""
del msg
if is_tf2:
return DummyModule(
arg_scope=None,
get_name_scope=lambda: tf.get_default_graph().get_name_scope(),
name_scope=tf.name_scope,
deprecated=deprecated,
nest=tf.nest,
argsort=tf.argsort)
from tensorflow.contrib import framework as contrib_framework # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top
return contrib_framework
示例4: _top_k_sample
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def _top_k_sample(logits, ignore_ids=None, num_samples=1, k=10):
"""
Does top-k sampling. if ignore_ids is on, then we will zero out those logits.
:param logits: [batch_size, vocab_size] tensor
:param ignore_ids: [vocab_size] one-hot representation of the indices we'd like to ignore and never predict,
like padding maybe
:param p: topp threshold to use, either a float or a [batch_size] vector
:return: [batch_size, num_samples] samples
# TODO FIGURE OUT HOW TO DO THIS ON TPUS. IT'S HELLA SLOW RIGHT NOW, DUE TO ARGSORT I THINK
"""
with tf.variable_scope('top_p_sample'):
batch_size, vocab_size = get_shape_list(logits, expected_rank=2)
probs = tf.nn.softmax(logits if ignore_ids is None else logits - tf.cast(ignore_ids[None], tf.float32) * 1e10,
axis=-1)
# [batch_size, vocab_perm]
indices = tf.argsort(probs, direction='DESCENDING')
# find the top pth index to cut off. careful we don't want to cutoff everything!
# result will be [batch_size, vocab_perm]
k_expanded = k if isinstance(k, int) else k[:, None]
exclude_mask = tf.range(vocab_size)[None] >= k_expanded
# OPTION A - sample in the sorted space, then unsort.
logits_to_use = tf.batch_gather(logits, indices) - tf.cast(exclude_mask, tf.float32) * 1e10
sample_perm = tf.random.categorical(logits=logits_to_use, num_samples=num_samples)
sample = tf.batch_gather(indices, sample_perm)
return {
'probs': probs,
'sample': sample,
}
示例5: nms_tf
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def nms_tf(dets, thresh):
"""Non-maximum suppression with tf graph mode."""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = tf.argsort(scores, direction='DESCENDING')
keep = tf.TensorArray(tf.int32, size=0, dynamic_size=True)
index = 0
while tf.size(order) > 0:
i = order[0]
keep = keep.write(index, i)
xx1 = tf.maximum(x1[i], tf.gather(x1, order[1:]))
yy1 = tf.maximum(y1[i], tf.gather(y1, order[1:]))
xx2 = tf.minimum(x2[i], tf.gather(x2, order[1:]))
yy2 = tf.minimum(y2[i], tf.gather(y2, order[1:]))
w = tf.maximum(0.0, xx2 - xx1 + 1)
h = tf.maximum(0.0, yy2 - yy1 + 1)
intersection = w * h
overlap = intersection / (
areas[i] + tf.gather(areas, order[1:]) - intersection)
inds = tf.where_v2(overlap <= thresh)
order = tf.concat(tf.gather(order, inds + 1), axis=1)
order = tf.squeeze(order, axis=-1)
index += 1
return keep.stack()
示例6: nms
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def nms(dets, thresh):
"""Non-maximum suppression."""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
intersection = w * h
overlap = intersection / (areas[i] + areas[order[1:]] - intersection)
inds = np.where(overlap <= thresh)[0]
order = order[inds + 1]
return keep
示例7: _top_p_sample
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def _top_p_sample(logits, ignore_ids=None, num_samples=1, p=0.9):
"""
Does top-p sampling. if ignore_ids is on, then we will zero out those logits.
:param logits: [batch_size, vocab_size] tensor
:param ignore_ids: [vocab_size] one-hot representation of the indices we'd like to ignore and never predict,
like padding maybe
:param p: topp threshold to use, either a float or a [batch_size] vector
:return: [batch_size, num_samples] samples
# TODO FIGURE OUT HOW TO DO THIS ON TPUS. IT'S HELLA SLOW RIGHT NOW, DUE TO ARGSORT I THINK
"""
with tf.variable_scope('top_p_sample'):
batch_size, vocab_size = get_shape_list(logits, expected_rank=2)
probs = tf.nn.softmax(logits if ignore_ids is None else logits - tf.cast(ignore_ids[None], tf.float32) * 1e10,
axis=-1)
if isinstance(p, float) and p > 0.999999:
# Don't do top-p sampling in this case
print("Top-p sampling DISABLED", flush=True)
return {
'probs': probs,
'sample': tf.random.categorical(
logits=logits if ignore_ids is None else logits - tf.cast(ignore_ids[None], tf.float32) * 1e10,
num_samples=num_samples, dtype=tf.int32),
}
# [batch_size, vocab_perm]
indices = tf.argsort(probs, direction='DESCENDING')
cumulative_probabilities = tf.math.cumsum(tf.batch_gather(probs, indices), axis=-1, exclusive=False)
# find the top pth index to cut off. careful we don't want to cutoff everything!
# result will be [batch_size, vocab_perm]
p_expanded = p if isinstance(p, float) else p[:, None]
exclude_mask = tf.logical_not(
tf.logical_or(cumulative_probabilities < p_expanded, tf.range(vocab_size)[None] < 1))
# OPTION A - sample in the sorted space, then unsort.
logits_to_use = tf.batch_gather(logits, indices) - tf.cast(exclude_mask, tf.float32) * 1e10
sample_perm = tf.random.categorical(logits=logits_to_use, num_samples=num_samples)
sample = tf.batch_gather(indices, sample_perm)
# OPTION B - unsort first - Indices need to go back to 0 -> N-1 -- then sample
# unperm_indices = tf.argsort(indices, direction='ASCENDING')
# include_mask_unperm = tf.batch_gather(include_mask, unperm_indices)
# logits_to_use = logits - (1 - tf.cast(include_mask_unperm, tf.float32)) * 1e10
# sample = tf.random.categorical(logits=logits_to_use, num_samples=num_samples, dtype=tf.int32)
return {
'probs': probs,
'sample': sample,
}
示例8: sp_mul
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import argsort [as 别名]
def sp_mul(sp_a_indices, sp_a_values, sp_b, default_value=0.):
"""Element-wise multiply two SparseTensors.
One SparseTensor has a fixed number of non-zero values, represented in the
form of a B x K matrix of indices and the other in a B x K matrix of values.
Args:
sp_a_indices: B x K Tensor. Indices into M values.
sp_a_values: B x K Tensor.
sp_b: B x M SparseTensor.
default_value: Scalar default value to use for indices which do not
intersect.
Returns:
mul_values: B x K Tensor corresponding to indices in sp_a_indices. For
indices also present in sp_b the output is the product of the values in
sp_a_values. For those absent in sp_b, the output is default_value.
"""
batch_size = tf.shape(sp_a_indices)[0]
num_neighbors = tf.shape(sp_a_indices)[1]
a_values = tf.reshape(sp_a_values, (batch_size * num_neighbors,))
brange = tf.tile(
tf.expand_dims(tf.cast(tf.range(batch_size), sp_a_indices.dtype), 1),
(1, num_neighbors))
nnz_indices = tf.stack([brange, sp_a_indices], axis=2)
nnz_indices = tf.reshape(nnz_indices, (batch_size * num_neighbors, 2))
# Linearize
scaling_vector = tf.cumprod(tf.cast(tf.shape(sp_b), sp_b.indices.dtype))
a1s = tf.linalg.matvec(
tf.cast(nnz_indices, sp_b.indices.dtype), scaling_vector)
b1s = tf.linalg.matvec(sp_b.indices, scaling_vector)
# Get intersections.
a_nonint, _ = tf.setdiff1d(a1s, b1s)
a_int, a_int_idx = tf.setdiff1d(a1s, a_nonint)
b_nonint, _ = tf.setdiff1d(b1s, a1s)
b_int, b_int_idx = tf.setdiff1d(b1s, b_nonint)
# Get sorting.
a_int_sort_ind = tf.argsort(a_int)
b_int_sort_ind = tf.argsort(b_int)
# Multiply.
int_vals = (
tf.gather(a_values, tf.gather(a_int_idx, a_int_sort_ind)) *
tf.gather(sp_b.values, tf.gather(b_int_idx, b_int_sort_ind)))
int_vals_dense = tf.sparse_to_dense(
sparse_indices=tf.expand_dims(tf.gather(a_int_idx, a_int_sort_ind), 1),
output_shape=tf.shape(a_values),
default_value=default_value,
sparse_values=int_vals,
validate_indices=False)
return tf.reshape(int_vals_dense, (batch_size, num_neighbors))