第15章自然言語処理:事前学習
15.7.2. Applying Pretrained Word Vectors15.7.2 事前学習した単語ベクトルを応用する
Using the loaded GloVe vectors, we will demonstrate their semantics by applying them in the following word similarity and analogy tasks.
英語のヒント
読み込んだGloVeのベクトルを使い、次の単語の類似度と類推のタスクに当てはめて、その意味の性質を示す。
15.7.2.1. Word Similarity15.7.2.1 単語の類似度
Similar to Section 15.4.3, in order to find semantically similar words for an input word based on cosine similarities between word vectors, we implement the following
knn (-nearest neighbors) function.英語のヒント
15.4.3節と同じように、単語ベクトルどうしのコサイン類似度にもとづいて、入力した単語に意味の似た単語を見つけるため、次の
knn(近傍法)関数を実装する。def knn(W, x, k):
# Add 1e-9 for numerical stability
cos = torch.mv(W, x.reshape(-1,)) / (
torch.sqrt(torch.sum(W * W, axis=1) + 1e-9) *
torch.sqrt((x * x).sum()))
_, topk = torch.topk(cos, k=k)
return topk, [cos[int(i)] for i in topk]Then, we search for similar words using the pretrained word vectors from the
TokenEmbedding instance embed.英語のヒント
それから、
TokenEmbeddingのインスタンスembedの事前学習した単語ベクトルを使って、似た単語を探す。def get_similar_tokens(query_token, k, embed):
topk, cos = knn(embed.idx_to_vec, embed[[query_token]], k + 1)
for i, c in zip(topk[1:], cos[1:]): # Exclude the input word
print(f'cosine sim={float(c):.3f}: {embed.idx_to_token[int(i)]}')The vocabulary of the pretrained word vectors in
glove_6b50d contains 400000 words and a special unknown token. Excluding the input word and unknown token, among this vocabulary let’s find three most semantically similar words to word “chip”.英語のヒント
glove_6b50dの事前学習した単語ベクトルの語彙は、400000語と一つの特殊な未知トークンを含む。入力した単語と未知トークンを除いて、この語彙の中から、単語「chip」に意味が最も似ている三つの単語を見つけよう。get_similar_tokens('chip', 3, glove_6b50d)cosine sim=0.856: chips
cosine sim=0.749: intel
cosine sim=0.749: electronicsBelow outputs similar words to “baby” and “beautiful”.
英語のヒント
以下は、「baby」と「beautiful」に似た単語を出力する。
get_similar_tokens('baby', 3, glove_6b50d)cosine sim=0.839: babies
cosine sim=0.800: boy
cosine sim=0.792: girlget_similar_tokens('beautiful', 3, glove_6b50d)cosine sim=0.921: lovely
cosine sim=0.893: gorgeous
cosine sim=0.830: wonderful15.7.2.2. Word Analogy15.7.2.2 単語の類推
Besides finding similar words, we can also apply word vectors to word analogy tasks. For example, “man”:“woman”::“son”:“daughter” is the form of a word analogy: “man” is to “woman” as “son” is to “daughter”. Specifically, the word analogy completion task can be defined as: for a word analogy , given the first three words , and , find . Denote the vector of word by . To complete the analogy, we will find the word whose vector is most similar to the result of .
英語のヒント
似た単語を見つけるほかに、単語ベクトルを単語の類推のタスクに当てはめることもできる。たとえば、「man」:「woman」::「son」:「daughter」は単語の類推の形で、「man」に対する「woman」の関係は、「son」に対する「daughter」の関係に等しいことを表す。具体的には、単語の類推を完成させるタスクは次のように定義できる。単語の類推について、最初の三つの単語、、が与えられたとき、を見つける。単語のベクトルをで表す。類推を完成させるには、の結果に最も似たベクトルをもつ単語を見つける。
def get_analogy(token_a, token_b, token_c, embed):
vecs = embed[[token_a, token_b, token_c]]
x = vecs[1] - vecs[0] + vecs[2]
topk, cos = knn(embed.idx_to_vec, x, 1)
return embed.idx_to_token[int(topk[0])] # Remove unknown wordsLet’s verify the “male-female” analogy using the loaded word vectors.
英語のヒント
読み込んだ単語ベクトルを使って、「男性‐女性」の類推を確かめよう。
get_analogy('man', 'woman', 'son', glove_6b50d)'daughter'Below completes a “capital-country” analogy: “beijing”:“china”::“tokyo”:“japan”. This demonstrates semantics in the pretrained word vectors.
英語のヒント
以下は、「首都‐国」の類推「beijing」:「china」::「tokyo」:「japan」を完成させる。これは、事前学習した単語ベクトルにある意味の性質を示している。
get_analogy('beijing', 'china', 'tokyo', glove_6b50d)'japan'For the “adjective-superlative adjective” analogy such as “bad”:“worst”::“big”:“biggest”, we can see that the pretrained word vectors may capture the syntactic information.
英語のヒント
「bad」:「worst」::「big」:「biggest」のような「形容詞‐形容詞の最上級」の類推では、事前学習した単語ベクトルが統語的な情報をとらえうることがわかる。
get_analogy('bad', 'worst', 'big', glove_6b50d)'biggest'To show the captured notion of past tense in the pretrained word vectors, we can test the syntax using the “present tense-past tense” analogy: “do”:“did”::“go”:“went”.
英語のヒント
事前学習した単語ベクトルがとらえた過去時制の概念を示すため、「現在時制‐過去時制」の類推「do」:「did」::「go」:「went」を使って統語を試せる。
get_analogy('do', 'did', 'go', glove_6b50d)'went'