深層学習を用いた任意のオブジェクトに対する自然言語意味検索の構築方法
深層学習を活用し、自然言語クエリで任意のオブジェクトを意味的に検索するシステムの構築方法を解説。
キーポイント
自然言語と任意オブジェクト(コード、画像、音声など)を共有ベクトル空間にマッピングすることで意味的検索を実現する手法を提案
Pythonコードの意味的検索を具体例として、再現可能な最小限の実装例を提供
従来のキーワード検索の限界を超え、意味理解に基づく情報発見性を大幅に向上させる可能性を示す
影響分析・編集コメントを表示
影響分析
この記事は、従来テキスト中心だった意味的検索を任意のオブジェクトに拡張する実践的な手法を示しており、コードリポジトリ検索からマルチモーダル検索まで幅広い応用可能性を開く。開発者の生産性向上や非構造化データの活用促進に寄与する重要な技術進展と言える。
編集コメント
実践的なチュートリアル形式で高度な技術を解説しており、研究者だけでなく実務家にも価値のある内容。マルチモーダル検索の実装パターンとして参考になる。
深層学習を用いた任意オブジェクトに対する自然言語意味検索の構築方法
現代の検索エンジンの力は否定できないが、この能力はあらゆる場所に存在するわけではない。多くの状況では、検索は厳格なキーワード検索に限定されるか、オブジェクトがテキストでない場合には検索自体が利用できない。さらに、厳格なキーワード検索では意味的な検索が不可能であり、情報の発見性が損なわれている。
本記事では、任意のオブジェクトに対して意味検索を可能にする再現性のある最小限の実用製品(MVP)を紹介する。具体的には、Pythonコードを意味的に検索するシステムの構築方法を示すが、このアプローチは画像や音声クリップなどの他のエンティティにも一般化できる。
意味検索の価値を示す例として、検索クエリ「Ping REST api and return results」に対して、結果として返されるコードやコメントに「Ping」「REST」「api」という単語が含まれていなくても、適切な結果が得られることが挙げられる。これは、キーワードに加えてコンテンツの意味を検索できるため、ユーザーが求める情報を見つける可能性を最大化する。意味検索の応用は広範であり、例えば開発者が構文に詳しくない、または適切なキーワードを予測できない場合でも、リポジトリ内のコードを検索できるようになる。さらに重要なのは、このアプローチを画像、音声、その他未だ考えられていないオブジェクトに一般化できる点である。
この技術を実現するための中心的アイデアは、テキストと検索対象オブジェクト(コード)を共有ベクトル空間で表現することである。目標は、コードを自然言語のベクトル空間にマッピングし、同じ概念を説明する(テキスト、コード)ペアが近傍に、無関係なペアが遠くに位置するようにすることである。類似度の測定にはコサイン類似度が用いられる。
このプロセスを実装するためには、自然言語とコードの両方を理解できる深層学習モデルが必要となる。記事では、自然言語とプログラミング言語の両方で事前学習されたモデルを利用する。具体的な手順として、まずコードスニペットと対応する自然言語の説明文(ドキュストリングやコメントなど)のペアを収集し、それらを用いてモデルを微調整する。これにより、モデルはコードと自然言語の間の意味的関係を学習する。次に、コードベース全体をこのモデルで処理し、各コードスニペットのベクトル表現(埋め込み)を生成して検索インデックスとして保存する。ユーザーが自然言語クエリを入力すると、同じモデルでクエリのベクトル表現を生成し、インデックス内のコードベクトルと比較して、コサイン類似度が高いものから順に結果を返す。
このアプローチの利点は、検索対象がコードに限定されないことである。同じ原理で、画像とそのキャプション、音声とトランスクリプトなど、あらゆるペアデータが存在するオブジェクトに対して意味検索システムを構築できる。要約すると、適切なペアデータと深層学習モデルを用いて共有ベクトル空間を構築することで、従来
原文を表示
An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.
How To Create Natural Language Semantic Search For Arbitrary Objects With Deep Learning
An end-to-end example of how to build a system that can search objects semantically. By Hamel Husain & Ho-Hsiang Wu
The power of modern search engines is undeniable: you can summon knowledge from the internet at a moment’s notice. Unfortunately, this superpower isn’t omnipresent. There are many situations where search is relegated to strict keyword search, or when the objects aren’t text, search may not be available. Furthermore, strict keyword search doesn’t allow the user to search semantically, which means information is not as discoverable.
Today, we share a reproducible, minimally viable product that illustrates how you can enable semantic search for arbitrary objects! Concretely, we will show you how to create a system that searches python code semantically — but this approach can be generalized to other entities (such as pictures or sound clips).
Why is semantic search so exciting? Consider the below example:
Semantic search at work on python code. *See Disclaimer section below.The search query presented is “Ping REST api and return results”. However, the search returns reasonable results even though the code & comments found do not contain the words Ping, REST or api.
This illustrates the power of semantic search: we can search content for its meaning in addition to keywords, and maximize the chances the user will find the information they are looking for. The implications of semantic search are profound — for example, such a procedure would allow developers to search for code in repositories even if they are not familiar with the syntax or fail to anticipate the right keywords. More importantly, you can generalize this approach to objects such as pictures, audio and other things that we haven’t thought of yet.
If this is not exciting enough, here is a live demonstration of what you will be able to build by the end of this tutorial:
Sometimes I use Jupyter notebooks and custom magic functions to create demonstrations when I cannot build a pretty website. it can be a quick way to interactively demonstrate your work!Intuition : Construct a Shared Vector-Space
Before diving into the technical details, it is useful to provide you with a high-level intuition of how we will accomplish semantic search. The central idea is to represent both text and the object we want to search (code) in a shared vector space, as illustrated below:
Example: Text 2 and the code should be represented by similar vectors since they are directly related.The goal is to map code into the vector space of natural language, such that (text, code) pairs that describe the same concept are close neighbors, whereas unrelated (text, code) pairs are further apart, measured by cosine similarity.
There are many ways to accomplish this goal, however, we will demonstrate the approach of taking a pre-trained model that extracts features from code and fine-tuning this model to project latent code features into a vector space of natural language. One warning: We use the term vector and embedding interchangeably throughout this tutorial.
{Update 1/1/2020}: CodeSearchNet
The techniques presented in this blog post are old and have been significantly refined in a subsequent project called CodeSearchNet, with an associated paper.
I recommend looking at the aforementioned project for a more modern approach to this topic, as in retrospect this blog post is somewhat of an ugly hack.
We recommend familiarity with the following items prior to reading this tutorial:
Sequence-to-sequence models: It will be helpful to review the information presented in a previous tutorial.
Peruse this paper at a high level and understand the intuition of the approach presented. We draw on similar concepts for what we present here.
This tutorial will be broken into 5 concrete steps. These steps are illustrated below and will be a useful reference as you progress throughout the tutorial. After completing the tutorial, it will be useful to revisit this diagram to reinforce how all the steps fit together.
A mind map of this tutorial. Hi-res version available here.Each step 1–5 corresponds to a Jupyter notebook here. We will explore each step in more detail below.
Part 1 — Acquire and Parse Data:
The folks at Google collect and store data from open-source GitHub repositories on BigQuery. This is a great open dataset for all kinds of interesting data-science projects, including this one! When you sign up for a Google Cloud account, they give you $300 which is more than enough to query the data for this exercise. Getting this data is super convenient, as you can use SQL queries to select what type of files you are looking for as well as other meta-data about repos such as commits, stars, etc.
The steps
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み