google-research
56 строк · 2.3 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Utilities for negative cache training."""
17
18import tensorflow.compat.v2 as tf
19
20
21def approximate_top_k_with_indices(negative_scores, k):
22"""Approximately mines the top k highest scoreing negatives with indices.
23
24This function groups the negative scores into num_negatives / k groupings and
25returns the highest scoring element from each group. It also returns the index
26where the selected elements were found in the score matrix.
27
28Args:
29negative_scores: A matrix with the scores of the negative elements.
30k: The number of negatives to mine.
31
32Returns:
33The tuple (top_k_scores, top_k_indices), where top_k_indices describes the
34index of the mined elements in the given score matrix.
35"""
36bs = tf.shape(negative_scores)[0]
37num_elem = tf.shape(negative_scores)[1]
38batch_indices = tf.range(num_elem)
39indices = tf.tile(tf.expand_dims(batch_indices, axis=0), multiples=[bs, 1])
40grouped_negative_scores = tf.reshape(negative_scores, [bs * k, -1])
41grouped_batch_indices = tf.range(tf.shape(grouped_negative_scores)[0])
42grouped_top_k_scores, grouped_top_k_indices = tf.math.top_k(
43grouped_negative_scores)
44grouped_top_k_indices = tf.squeeze(grouped_top_k_indices, axis=1)
45gather_indices = tf.stack([grouped_batch_indices, grouped_top_k_indices],
46axis=1)
47grouped_indices = tf.reshape(indices, [bs * k, -1])
48grouped_top_k_indices = tf.gather_nd(grouped_indices, gather_indices)
49top_k_indices = tf.reshape(grouped_top_k_indices, [bs, k])
50top_k_scores = tf.reshape(grouped_top_k_scores, [bs, k])
51return top_k_scores, top_k_indices
52
53
54def cross_replica_concat(tensor, axis):
55replica_context = tf.distribute.get_replica_context()
56return replica_context.all_gather(tensor, axis=axis)
57