使用近似最近邻和文本嵌入向量构建语义搜索

在 TensorFlow.org 上查看 在 Google Colab 中运行 在 GitHub 上查看源代码 下载笔记本 查看 TF Hub 模型

本教程演示了如何在给定输入数据的情况下,从 TensorFlow Hub (TF-Hub) 模块生成嵌入向量,并使用提取的嵌入向量构建近似最近邻 (ANN) 索引。之后,可以将该索引用于实时相似度匹配和检索。

在处理包含大量数据的语料库时,通过扫描整个存储库实时查找与给定查询最相似的条目来执行精确匹配的效率不高。因此,我们使用一种近似相似度匹配算法。利用这种算法,我们在查找精确的最近邻匹配时会牺牲一点准确率,但是可以显著提高速度。

在本教程中,我们将展示一个示例,在新闻标题语料库上进行实时文本搜索,以查找与查询最相似的标题。与关键字搜索不同,此过程会捕获在文本嵌入向量中编码的语义相似度。

本教程的操作步骤如下:

  1. 下载样本数据
  2. 使用 TF-Hub 模型为数据生成嵌入向量
  3. 为嵌入向量构建 ANN 索引
  4. 使用索引进行相似度匹配

我们使用 Apache Beam 从 TF-Hub 模型生成嵌入向量。此外,我们还使用 Spotify 的 ANNOY 库来构建近似最近邻索引。

更多模型

对于具有相同架构,但使用不同的语言进行训练的模型,请参考集合。这里可以找到 tfhub.dev 上当前托管的所有文本嵌入向量。

设置

安装所需的库。

pip install apache_beam
pip install 'scikit_learn~=0.23.0'  # For gaussian_random_matrix.
pip install annoy

导入所需的库。

import os
import sys
import pickle
from collections import namedtuple
from datetime import datetime
import numpy as np
import apache_beam as beam
from apache_beam.transforms import util
import tensorflow as tf
import tensorflow_hub as hub
import annoy
from sklearn.random_projection import gaussian_random_matrix
print('TF version: {}'.format(tf.__version__))
print('TF-Hub version: {}'.format(hub.__version__))
print('Apache Beam version: {}'.format(beam.__version__))
TF version: 2.6.0
TF-Hub version: 0.12.0
Apache Beam version: 2.31.0

1. 下载样本数据

A Million News Headlines 数据集包含著名的澳大利亚广播公司 (ABC) 在 15 年内发布的新闻标题。此新闻数据集汇总了从 2003 年初至 2017 年底在全球范围内发生的重大事件的历史记录,其中对澳大利亚的关注更为细致。

格式:以制表符分隔的两列数据:1) 发布日期和 2) 标题文本。我们只对标题文本感兴趣。

wget 'https://dataverse.harvard.edu/api/access/datafile/3450625?format=tab&gbrecs=true' -O raw.tsv
wc -l raw.tsv
head raw.tsv
--2021-08-13 20:51:21--  https://dataverse.harvard.edu/api/access/datafile/3450625?format=tab&gbrecs=true
Resolving dataverse.harvard.edu (dataverse.harvard.edu)... 54.162.175.159, 18.211.119.52, 72.44.40.54
Connecting to dataverse.harvard.edu (dataverse.harvard.edu)|54.162.175.159|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 57600231 (55M) [text/tab-separated-values]
Saving to: ‘raw.tsv’

raw.tsv             100%[===================>]  54.93M  14.8MB/s    in 4.4s    

2021-08-13 20:51:27 (12.4 MB/s) - ‘raw.tsv’ saved [57600231/57600231]

1103664 raw.tsv
publish_date    headline_text
20030219    "aba decides against community broadcasting licence"
20030219    "act fire witnesses must be aware of defamation"
20030219    "a g calls for infrastructure protection summit"
20030219    "air nz staff in aust strike for pay rise"
20030219    "air nz strike to affect australian travellers"
20030219    "ambitious olsson wins triple jump"
20030219    "antic delighted with record breaking barca"
20030219    "aussie qualifier stosur wastes four memphis match"
20030219    "aust addresses un security council over iraq"

为了简单起见,我们仅保留标题文本并移除发布日期。

!rm -r corpus
!mkdir corpus

with open('corpus/text.txt', 'w') as out_file:
  with open('raw.tsv', 'r') as in_file:
    for line in in_file:
      headline = line.split('\t')[1].strip().strip('"')
      out_file.write(headline+"\n")
rm: cannot remove 'corpus': No such file or directory
tail corpus/text.txt
severe storms forecast for nye in south east queensland
snake catcher pleads for people not to kill reptiles
south australia prepares for party to welcome new year
strikers cool off the heat with big win in adelaide
stunning images from the sydney to hobart yacht
the ashes smiths warners near miss liven up boxing day test
timelapse: brisbanes new year fireworks
what 2017 meant to the kids of australia
what the papodopoulos meeting may mean for ausus
who is george papadopoulos the former trump campaign aide

2. 为数据生成嵌入向量

在本教程中,我们使用神经网络语言模型 (NNLM) 为标题数据生成嵌入向量。之后,可以轻松地使用句子嵌入向量计算句子级别的含义相似度。我们使用 Apache Beam 来运行嵌入向量生成过程。

嵌入向量提取方法

embed_fn = None

def generate_embeddings(text, model_url, random_projection_matrix=None):
  # Beam will run this function in different processes that need to
  # import hub and load embed_fn (if not previously loaded)
  global embed_fn
  if embed_fn is None:
    embed_fn = hub.load(model_url)
  embedding = embed_fn(text).numpy()
  if random_projection_matrix is not None:
    embedding = embedding.dot(random_projection_matrix)
  return text, embedding

转换为 tf.Example 方法

def to_tf_example(entries):
  examples = []

  text_list, embedding_list = entries
  for i in range(len(text_list)):
    text = text_list[i]
    embedding = embedding_list[i]

    features = {
        'text': tf.train.Feature(
            bytes_list=tf.train.BytesList(value=[text.encode('utf-8')])),
        'embedding': tf.train.Feature(
            float_list=tf.train.FloatList(value=embedding.tolist()))
    }

    example = tf.train.Example(
        features=tf.train.Features(
            feature=features)).SerializeToString(deterministic=True)

    examples.append(example)

  return examples

Beam 流水线

def run_hub2emb(args):
  '''Runs the embedding generation pipeline'''

  options = beam.options.pipeline_options.PipelineOptions(**args)
  args = namedtuple("options", args.keys())(*args.values())

  with beam.Pipeline(args.runner, options=options) as pipeline:
    (
        pipeline
        | 'Read sentences from files' >> beam.io.ReadFromText(
            file_pattern=args.data_dir)
        | 'Batch elements' >> util.BatchElements(
            min_batch_size=args.batch_size, max_batch_size=args.batch_size)
        | 'Generate embeddings' >> beam.Map(
            generate_embeddings, args.model_url, args.random_projection_matrix)
        | 'Encode to tf example' >> beam.FlatMap(to_tf_example)
        | 'Write to TFRecords files' >> beam.io.WriteToTFRecord(
            file_path_prefix='{}/emb'.format(args.output_dir),
            file_name_suffix='.tfrecords')
    )

生成随机投影权重矩阵

随机投影是一种简单而强大的技术,用于降低位于欧几里得空间中的一组点的维数。有关理论背景,请参阅约翰逊-林登斯特劳斯引理

利用随机投影降低嵌入向量的维数,这样,构建和查询 ANN 索引需要的时间将减少。

在本教程中,我们使用 Scikit-learn 库中的高斯随机投影

def generate_random_projection_weights(original_dim, projected_dim):
  random_projection_matrix = None
  random_projection_matrix = gaussian_random_matrix(
      n_components=projected_dim, n_features=original_dim).T
  print("A Gaussian random weight matrix was creates with shape of {}".format(random_projection_matrix.shape))
  print('Storing random projection matrix to disk...')
  with open('random_projection_matrix', 'wb') as handle:
    pickle.dump(random_projection_matrix, 
                handle, protocol=pickle.HIGHEST_PROTOCOL)

  return random_projection_matrix

设置参数

如果要使用原始嵌入向量空间构建索引而不进行随机投影,请将 projected_dim 参数设置为 None。请注意,这会减慢高维嵌入向量的索引编制步骤。

运行流水线

import tempfile

output_dir = tempfile.mkdtemp()
original_dim = hub.load(model_url)(['']).shape[1]
random_projection_matrix = None

if projected_dim:
  random_projection_matrix = generate_random_projection_weights(
      original_dim, projected_dim)

args = {
    'job_name': 'hub2emb-{}'.format(datetime.utcnow().strftime('%y%m%d-%H%M%S')),
    'runner': 'DirectRunner',
    'batch_size': 1024,
    'data_dir': 'corpus/*.txt',
    'output_dir': output_dir,
    'model_url': model_url,
    'random_projection_matrix': random_projection_matrix,
}

print("Pipeline args are set.")
args
2021-08-13 20:51:37.862452: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:37.869537: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:37.870621: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:37.872469: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-13 20:51:37.873113: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:37.874167: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:37.875070: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:38.472491: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:38.473528: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:38.474484: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 20:51:38.475323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 14648 MB memory:  -> device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0
2021-08-13 20:51:38.822644: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
2021-08-13 20:51:38.833929: W tensorflow/core/framework/cpu_allocator_impl.cc:80] Allocation of 498570752 exceeds 10% of free system memory.
A Gaussian random weight matrix was creates with shape of (128, 64)
Storing random projection matrix to disk...
Pipeline args are set.
/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/sklearn/utils/deprecation.py:86: FutureWarning: Function gaussian_random_matrix is deprecated; gaussian_random_matrix is deprecated in 0.22 and will be removed in version 0.24.
  warnings.warn(msg, category=FutureWarning)
{'job_name': 'hub2emb-210813-205139',
 'runner': 'DirectRunner',
 'batch_size': 1024,
 'data_dir': 'corpus/*.txt',
 'output_dir': '/tmp/tmpeluerp15',
 'model_url': 'https://tfhub.dev/google/nnlm-en-dim128/2',
 'random_projection_matrix': array([[-0.05573037, -0.17715057,  0.08861728, ..., -0.06721592,
         -0.0750567 ,  0.11154118],
        [-0.07969007,  0.08691717, -0.13066974, ...,  0.16887259,
          0.31830099, -0.03581718],
        [ 0.10661986, -0.00611403,  0.13345951, ..., -0.0792625 ,
          0.13127118, -0.0575228 ],
        ...,
        [ 0.01704308, -0.14311761,  0.03804902, ..., -0.19536509,
         -0.06047237, -0.172833  ],
        [-0.13806994,  0.04580202,  0.19509079, ...,  0.01621252,
         -0.07891657, -0.13279038],
        [ 0.2556592 , -0.09096174,  0.01744089, ...,  0.04188382,
          0.04387324,  0.28444199]])}
print("Running pipeline...")
%time run_hub2emb(args)
print("Pipeline is done.")
WARNING:apache_beam.runners.interactive.interactive_environment:Dependencies required for Interactive Beam PCollection visualization are not available, please use: `pip install apache-beam[interactive]` to install necessary dependencies to enable all data visualization features.
Running pipeline...
WARNING:apache_beam.options.pipeline_options:Discarding unparseable args: ['-f', '/tmp/tmp_d8acqc4.json', '--HistoryManager.hist_file=:memory:']
WARNING:apache_beam.options.pipeline_options:Discarding invalid overrides: {'batch_size': 1024, 'data_dir': 'corpus/*.txt', 'output_dir': '/tmp/tmpeluerp15', 'model_url': 'https://tfhub.dev/google/nnlm-en-dim128/2', 'random_projection_matrix': array([[-0.05573037, -0.17715057,  0.08861728, ..., -0.06721592,
        -0.0750567 ,  0.11154118],
       [-0.07969007,  0.08691717, -0.13066974, ...,  0.16887259,
         0.31830099, -0.03581718],
       [ 0.10661986, -0.00611403,  0.13345951, ..., -0.0792625 ,
         0.13127118, -0.0575228 ],
       ...,
       [ 0.01704308, -0.14311761,  0.03804902, ..., -0.19536509,
        -0.06047237, -0.172833  ],
       [-0.13806994,  0.04580202,  0.19509079, ...,  0.01621252,
        -0.07891657, -0.13279038],
       [ 0.2556592 , -0.09096174,  0.01744089, ...,  0.04188382,
         0.04387324,  0.28444199]])}
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.
2021-08-13 20:51:40.467778: W tensorflow/core/framework/cpu_allocator_impl.cc:80] Allocation of 498570752 exceeds 10% of free system memory.
WARNING:apache_beam.io.tfrecordio:Couldn't find python-snappy so the implementation of _TFRecordUtil._masked_crc32c is not as fast as it could be.
CPU times: user 8min 13s, sys: 9min 26s, total: 17min 39s
Wall time: 2min 14s
Pipeline is done.
ls {output_dir}
emb-00000-of-00001.tfrecords

读取生成的部分嵌入向量…

embed_file = os.path.join(output_dir, 'emb-00000-of-00001.tfrecords')
sample = 5

# Create a description of the features.
feature_description = {
    'text': tf.io.FixedLenFeature([], tf.string),
    'embedding': tf.io.FixedLenFeature([projected_dim], tf.float32)
}

def _parse_example(example):
  # Parse the input `tf.Example` proto using the dictionary above.
  return tf.io.parse_single_example(example, feature_description)

dataset = tf.data.TFRecordDataset(embed_file)
for record in dataset.take(sample).map(_parse_example):
  print("{}: {}".format(record['text'].numpy().decode('utf-8'), record['embedding'].numpy()[:10]))
headline_text: [-0.02572352  0.01282816 -0.04206443 -0.07709609 -0.07464334 -0.09604754
  0.3417428   0.09377193  0.1404351   0.28789845]
aba decides against community broadcasting licence: [ 6.4809017e-02 -2.7826595e-01  1.5332937e-02 -1.4106102e-01
  2.7198857e-01 -5.9620809e-02 -8.1134029e-05  6.4011730e-02
 -1.7671141e-01  3.4886587e-02]
act fire witnesses must be aware of defamation: [ 0.04252682 -0.11108188 -0.07355075 -0.2739961   0.0062704   0.14101376
 -0.29312718  0.29525098 -0.10813845  0.02115536]
a g calls for infrastructure protection summit: [ 0.02583028  0.00596316  0.12090514 -0.14003596  0.03392243  0.1625118
  0.04277842  0.16665995  0.00146728  0.1391557 ]
air nz staff in aust strike for pay rise: [ 0.04351382 -0.04861023  0.03254236 -0.32149762  0.07647106 -0.01432635
 -0.05308914  0.20955665  0.02209205 -0.13726647]

3. 为嵌入向量构建 ANN 索引

ANNOY(近似最近邻)是一个包含 Python 绑定的 C++ 库,用于搜索空间中与给定查询点接近的点。此外,它还会创建基于文件的大型只读数据结构,这些数据结构会映射到内存中。它由 Spotify 构建并用于音乐推荐。如果您感兴趣,可以尝试使用 ANNOY 的其他替代库,例如 NGTFAISS 等。

def build_index(embedding_files_pattern, index_filename, vector_length, 
    metric='angular', num_trees=100):
  '''Builds an ANNOY index'''

  annoy_index = annoy.AnnoyIndex(vector_length, metric=metric)
  # Mapping between the item and its identifier in the index
  mapping = {}

  embed_files = tf.io.gfile.glob(embedding_files_pattern)
  num_files = len(embed_files)
  print('Found {} embedding file(s).'.format(num_files))

  item_counter = 0
  for i, embed_file in enumerate(embed_files):
    print('Loading embeddings in file {} of {}...'.format(i+1, num_files))
    dataset = tf.data.TFRecordDataset(embed_file)
    for record in dataset.map(_parse_example):
      text = record['text'].numpy().decode("utf-8")
      embedding = record['embedding'].numpy()
      mapping[item_counter] = text
      annoy_index.add_item(item_counter, embedding)
      item_counter += 1
      if item_counter % 100000 == 0:
        print('{} items loaded to the index'.format(item_counter))

  print('A total of {} items added to the index'.format(item_counter))

  print('Building the index with {} trees...'.format(num_trees))
  annoy_index.build(n_trees=num_trees)
  print('Index is successfully built.')

  print('Saving index to disk...')
  annoy_index.save(index_filename)
  print('Index is saved to disk.')
  print("Index file size: {} GB".format(
    round(os.path.getsize(index_filename) / float(1024 ** 3), 2)))
  annoy_index.unload()

  print('Saving mapping to disk...')
  with open(index_filename + '.mapping', 'wb') as handle:
    pickle.dump(mapping, handle, protocol=pickle.HIGHEST_PROTOCOL)
  print('Mapping is saved to disk.')
  print("Mapping file size: {} MB".format(
    round(os.path.getsize(index_filename + '.mapping') / float(1024 ** 2), 2)))
embedding_files = "{}/emb-*.tfrecords".format(output_dir)
embedding_dimension = projected_dim
index_filename = "index"

!rm {index_filename}
!rm {index_filename}.mapping

%time build_index(embedding_files, index_filename, embedding_dimension)
rm: cannot remove 'index': No such file or directory
rm: cannot remove 'index.mapping': No such file or directory
Found 1 embedding file(s).
Loading embeddings in file 1 of 1...
100000 items loaded to the index
200000 items loaded to the index
300000 items loaded to the index
400000 items loaded to the index
500000 items loaded to the index
600000 items loaded to the index
700000 items loaded to the index
800000 items loaded to the index
900000 items loaded to the index
1000000 items loaded to the index
1100000 items loaded to the index
A total of 1103664 items added to the index
Building the index with 100 trees...
Index is successfully built.
Saving index to disk...
Index is saved to disk.
Index file size: 1.6 GB
Saving mapping to disk...
Mapping is saved to disk.
Mapping file size: 50.61 MB
CPU times: user 8min 41s, sys: 59 s, total: 9min 40s
Wall time: 3min 37s
ls
corpus         random_projection_matrix
index          raw.tsv
index.mapping  tf2_semantic_approximate_nearest_neighbors.ipynb

4. 使用索引进行相似度匹配

现在,我们可以使用 ANN 索引查找与输入查询语义接近的新闻标题。

加载索引和映射文件

index = annoy.AnnoyIndex(embedding_dimension)
index.load(index_filename, prefault=True)
print('Annoy index is loaded.')
with open(index_filename + '.mapping', 'rb') as handle:
  mapping = pickle.load(handle)
print('Mapping file is loaded.')
Annoy index is loaded.
/home/kbuilder/.local/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: The default argument for metric will be removed in future version of Annoy. Please pass metric='angular' explicitly.
  """Entry point for launching an IPython kernel.
Mapping file is loaded.

相似度匹配方法

def find_similar_items(embedding, num_matches=5):
  '''Finds similar items to a given embedding in the ANN index'''
  ids = index.get_nns_by_vector(
  embedding, num_matches, search_k=-1, include_distances=False)
  items = [mapping[i] for i in ids]
  return items

从给定查询中提取嵌入向量

# Load the TF-Hub model
print("Loading the TF-Hub model...")
%time embed_fn = hub.load(model_url)
print("TF-Hub model is loaded.")

random_projection_matrix = None
if os.path.exists('random_projection_matrix'):
  print("Loading random projection matrix...")
  with open('random_projection_matrix', 'rb') as handle:
    random_projection_matrix = pickle.load(handle)
  print('random projection matrix is loaded.')

def extract_embeddings(query):
  '''Generates the embedding for the query'''
  query_embedding =  embed_fn([query])[0].numpy()
  if random_projection_matrix is not None:
    query_embedding = query_embedding.dot(random_projection_matrix)
  return query_embedding
Loading the TF-Hub model...
2021-08-13 20:57:33.334370: W tensorflow/core/framework/cpu_allocator_impl.cc:80] Allocation of 498570752 exceeds 10% of free system memory.
CPU times: user 570 ms, sys: 351 ms, total: 921 ms
Wall time: 915 ms
TF-Hub model is loaded.
Loading random projection matrix...
random projection matrix is loaded.
extract_embeddings("Hello Machine Learning!")[:10]
array([ 0.01479617, -0.00505297,  0.10314572, -0.10045563, -0.22697452,
        0.00304227,  0.11268801, -0.16526401, -0.14322434,  0.21455356])

输入查询以查找最相似的条目

Generating embedding for the query...
CPU times: user 2.85 ms, sys: 1.75 ms, total: 4.6 ms
Wall time: 1.89 ms

Finding relevant items in the index...
CPU times: user 799 µs, sys: 0 ns, total: 799 µs
Wall time: 545 µs

Results:
=========
confronting global challenges
conference examines challenges facing major cities
global warming forces rethink of japanese dress
gore brings hope of global emissions reduction
environment report identifies challenges
gloomy outlook for exports
climate change bringing higher risk of flooding
new research challenges global warming theories
employment figures underline disturbing trend of
west report shows changing face of agriculture

了解更多信息

您可以在 tensorflow.org 上详细了解 TensorFlow,并在 tensorflow.org/hub 上查看 TF-Hub API 文档。此外,还可以在 tfhub.dev 上找到可用的 TensorFlow Hub 模型,包括更多的文本嵌入向量模型和图像特征矢量模型。

另外,请查看机器学习速成课程,这是 Google 对机器学习的快节奏实用介绍。