毎日テキストマイニング

180日間、毎日テキストマイニングをするブログです

2018/8/7【45日目】word2vecを行うためにTensolFolwを触ってみる

分散表現を作るために、昨日ちらっと出てきましたGoogle製の機械学習フレームワークTensorFlowを触っていきたいと思います。

チュートリアルのページはこちら。

Vector Representations of Words  |  TensorFlow

数学の理論は後回しにして、コードのサンプルから見ていきます。

サンプルコードの実行

サンプルコードは下記のGithub上にあります。

github.com

コードは353行あります。いや、サンプルにしては長すぎない? というレベルなのですが、[word2vec_basic.py]と保存し、実行してみます。

$ python word2vec_basic.py

実行結果。

ModuleNotFoundError: No module named 'tensorflow.contrib.tensorboard'

モジュールが見つからない、というエラー。ネット上ではTensorFlowのバージョンが古いと起きるエラーだと書いてあるので、最新だったはずですが再インストールします。

$ pip uninstall tensorflow
$ pip install  tensorflow

ちなみにこの時に気づきましたが、TensorFlowはCPU版とGPU版の2種類があるそうです。GPU版をインストールしたいときは次のコマンドを実行します。

pip install tensorflow-gpu

今回はCPU版で実行していきます。 再インストールできたところで、もう一度実行。

実行結果。

urllib.error.ContentTooShortError: <urlopen error retrieval incomplete: got only 12194984 out of 31344016 bytes>

また、エラーが起きました。 31344016bytes必要なデータをダウンロードしようとおもったけど、12194984bytesしかダウンロードできていないよ、という通信エラーのようです。

通信エラーなので、もう一回実行してみると、違うエラーが出ます。

Exception: Failed to verify /var/folders/gg/ssh38fq52kj8htsj_c4ytzb40000gn/T/text8.zip. Can you get to it with a browser?

text8.zipがないからブラウザでダウンロードしてね、というエラーのようです。とりあえずその通りにしてみます。

ダウンロードは下記から。

http://mattmahoney.net/dc/text8.zip

ダウンロードができたら、指定された [/var/folders/gg/ssh38fq52kj8htsj_c4ytzb40000gn/T/t] にzipファイルを移動します。

mv text8.zip /var/folders/gg/ssh38fq52kj8htsj_c4ytzb40000gn/T/

もう一回実行

Nearest to into: from, through, callithrix, fachhochschule, upanija, thaler, vma, with,
Nearest to nine: eight, seven, six, five, zero, four, three, agouti,
Nearest to not: often, still, generally, they, easily, to, never, there,
Nearest to d: b, operatorname, six, agouti, e, osteoblasts, circ, pulau,
Nearest to used: known, found, busan, dasyprocta, considered, microsite, agouti, michelob,
Nearest to while: although, however, and, when, but, if, though, ursus,
Nearest to state: random, wct, ursinus, michelob, kifl, city, atherosclerosis, rigidly,
Nearest to to: albury, microsite, will, could, not, would, can, ursus,
Nearest to so: arthurian, loran, abet, ursus, loglan, impossible, iit, tested,
Nearest to during: in, when, after, with, orion, agouti, peacocks, upanija,
Nearest to only: wct, forefront, but, michelob, paigc, unassigned, dasyprocta, morita,
Nearest to its: their, his, the, her, nzenberg, acacias, leontopithecus, dasyprocta,
Nearest to an: peacocks, rcn, the, reverberation, listeners, agave, trusted, statehouse,
Nearest to at: in, on, under, after, politecnico, within, with, operatorname,
Nearest to been: be, was, were, become, had, immense, recitative, by,
Nearest to up: him, out, peacocks, hakama, off, yakovlev, them, hides,

やっと動きました。

コードを実行しますと、こんな画像ができました。 f:id:rimt:20180808013741p:plain

めちゃくちゃ細かいですが、単語があらゆる位置に並んでいます。 この距離と方向によって単語を計算できるようになるそうです。

しかし、TensorFlowに触るだけで結構な体力を使いますね。もっと楽な方法はないもんですかね。。。

今日の結果

今日のAKBメンバーによる呟きは31件でした。 かなり少ないですね。画像はこんな感じです。

f:id:rimt:20180808013807p:plain

'すごい': 4, '楽しい': 3, 'ない': 3, 'おしい': 1, '無い': 1, '正しい': 1, '欲しい': 1, 'いい': 1, '良い': 1, '凄い': 1, '嬉しい': 1, 'あたたかい': 1}
'日': 15, '写真': 7, '今日': 7, '公演': 7, '歳': 6, '月': 5, '広島': 5, '原爆': 5, 'すごい': 4, 'ちゃん': 4, '平和': 4, '感謝': 4, 'ん': 4, '年': 4, '今': 4, '楽しい': 3, 'ない': 3,
'する': 21, '日': 15, 'てる': 10, '写真': 7, '今日': 7, '公演': 7, '歳': 6, '月': 5, '広島': 5, '原爆': 5, 'れる': 5, 'られる': 5, 'すごい': 4, 'ちゃん': 4, '平和': 4, '感謝': 4, 'ん': 4, 

今日勉強したこと

  • Tensolflowの導入