機械学習20-ワードベクトル(Word 2 Vec)技術
4565 ワード
gensimツールパッケージを使用して、20種類のニューステキスト(20 newsgroups)を使用して語ベクトルトレーニングを行い、いくつかの語彙をサンプリングすることで、Word 2 Vec技術が言語学の知識を借りずに類似の他の語彙を見つけることができるかどうかを確認します.
以上の2つのグループの出力により,言語学辞書を用いない前提で,語ベクトル技術はコンテキスト情報を用いて語彙間の類似性を見つけることができることが容易に分かった.この技術は、多くの専門家の作業時間を節約するだけでなく、より複雑な自然言語処理タスクに基礎モデルとして応用することもできます.
from sklearn.datasets import fetch_20newsgroups
from bs4 import BeautifulSoup
import nltk, re
from gensim.models import word2vec
# news_to_sentences ,
# , 。
def news_to_sentences(news):
news_text = BeautifulSoup(news).get_text()
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
raw_sentences = tokenizer.tokenize(news_text)
sentences = []
for sent in raw_sentences:
sentences.append(re.sub('[^a-zA-Z]', ' ', sent.lower().strip()).split())
return sentences
news = fetch_20newsgroups(subset='all')
X, y = news.data, news.target
sentences = []
# , 。
for x in X:
sentences += news_to_sentences(x)
# 。
num_features = 300
#
min_word_count = 20
# CPU , 。
num_workers = 2
#
context = 5
downsampling = 1e-3
model = word2vec.Word2Vec(sentences, workers=num_workers,\
size=num_features, min_count=min_word_count,\
window=context, sample=downsampling)
# , 。
model.init_sims(replace=True)
# , morning 10
m = model.most_similar('morning')
print(m)
#out[]:
# [('afternoon', 0.8285419940948486),
# ('weekend', 0.7679079174995422),
# ('evening', 0.7551226615905762),
# ('saturday', 0.7222977876663208),
# ('night', 0.7116754055023193),
# ('friday', 0.6781198978424072),
# ('sunday', 0.6390078067779541),
# ('newspaper', 0.6356056928634644),
# ('summer', 0.6305795907974243),
# ('week', 0.6181687116622925)]
# , email 10 。
e = model.most_similar('email')
print(e)
#out[]:
# [('mail', 0.7398847341537476),
# ('contact', 0.6963222622871399),
# ('address', 0.6542695164680481),
# ('replies', 0.646983802318573),
# ('mailed', 0.6348010897636414),
# ('request', 0.632864236831665),
# ('send', 0.6214576959609985),
# ('sas', 0.6191704869270325),
# ('listserv', 0.6177695989608765),
# ('compuserve', 0.5945062041282654)]
以上の2つのグループの出力により,言語学辞書を用いない前提で,語ベクトル技術はコンテキスト情報を用いて語彙間の類似性を見つけることができることが容易に分かった.この技術は、多くの専門家の作業時間を節約するだけでなく、より複雑な自然言語処理タスクに基礎モデルとして応用することもできます.