深層学習とその他

機械学習したいマン

ChainerのTextDatasetを使ってみる

アドベントカレンダー10日目です。
今日はChainerのTextDatasetを使ってみたいと思います。

adventar.org

TextDatasetとは

Dataset of a line-oriented text file. This dataset reads each line of text file(s) on every call of the getitem() operator. Positions of line boundaries are cached so that you can quickliy random access the text file by the line number.
chainer.datasets.TextDataset — Chainer 5.1.0 documentation

テキストファイルを渡すことでランダムな行にアクセスしたり、メモリの使用量を減らせるとかそんなだったような気がしています。

TextDatasetの使い方

datasets.TextDataset()にテキストファイルのパスを渡すだけです。

import chainer
from chainer import datasets

train = datasets.TextDataset("temp.txt")
train_iter = chainer.iterators.SerialIterator(train, batch_size=1)

あとはいつも通りtrain_iter.next()で値が取れます。
ソースコードのサンプルは以下から。

まとめ

今回はTextDatasetを使ってみました。かなりお手軽にテキストを読み込んで処理することができるので、ぜひ使ってみてください。

GoogleColaboratoryで書いたipynbをGistに公開する

アドベントカレンダー14日目です。
今日はGoogleColaboratoryで書いたipynbをGistに公開する方法を書いていきたいと思います。

 

Gistへの公開方法

Colaboratoryでノートを開いたら、ファイルタブから赤枠の「コピーをGitHub Gistとして保存」をクリックします。

f:id:looseleaf0727:20181214151749j:plain

 

GitHubにログインしていれば、ウィンドウが開いてコピーが作成されます。
Gistにアクセスすると、コピーが作成されていることを確認できます。

f:id:looseleaf0727:20181214152059j:plain



 

ソースコードの貼り付け

赤枠の部分をクリックしてコピーするとソースコードをブログに張り付けることができます。

f:id:looseleaf0727:20181214152113j:plain

 

 

まとめ

はてなブログソースコードを張り付けるのは少し手間なので、Gistから張り付けるほうが手軽だと思います。

word2vecで学習済みfastTextを読み込んで単語類似度を表示する

アドベントカレンダー9日目です。
今日は、word2vecで学習済みfastTextを読み込んで類似度を出してみたいと思います。

adventar.org

学習済みfastTextのデータをダウンロードする

学習済みfastTextのデータはこちらからダウンロードできます。

fastTextの学習済みモデルを公開しました - Qiita

今回はこちらのDownload Word Vectors(NEologd)からダウンロードしました。

 

単語類似度を出す

出現頻度が高い単語は類似語も正確に出やすいですが、低い単語は怪しい数値が出ます。ソースコードは以下から。

 

Cannot uninstall 'scipy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.というエラーが出る

gensimをインストールするときに、scipyについてこのエラーが出ました。

Cannot uninstall 'scipy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.



このエラーが出た際に"pip uninstall scipy"を使っても同様のエラーがでるだけです。
以下のオプションをつけることで正しくインストールできました。

!pip install scipy --ignore-installed

これは強制的な解決方法なので、できればインストールした際の方法でアンインストールするのが一番かもしれません。

seabornで折れ線グラフを描く

アドベントカレンダー8日目です。
今日は、seabornを使って折れ線グラフを書いていきたいと思います。

 

matplotlibでの折れ線グラフ

matplotlibではplot関数で折れ線が描画できます。
この際に、yの値は渡さなくても一応描画できます。
figure(figsize)で図のサイズ指定、xlabelでx軸のラベル付け、ylabelでy軸のラベル付け、gridでグリッドの描画、titleでtitle設定、savefigで図の保存ができます。

seabornでの折れ線グラフ

pointplotで描画できます。
matplotlibよりも見た目が少しリッチになったり、グリッドが少しきれいになったように感じました。seabornの関数を使ったり、matplotlibの関数を使ったりできます。

GoogleColaboratoryでChainerを使う

アドベントカレンダー7日目です。
adventar.org


コラボラトリーでChainerを使う際のコマンドをいつも忘れるのでメモ。また、先にランタイムをGPUに切り替えることを忘れないようにしましょう。

Chainerのインストール

この一行を動かすだけで大丈夫です。

!curl https://colab.chainer.org/install | sh -


確認してみましょう。

import chainer
print(chainer.__version__)
>> 5.1.0

jupyterthemesでjupyterの見た目をよくする

アドベントカレンダー6日目です。
adventar.org

今日はjupyterthemesについて書いていきたいと思います。

jupyter-themesとは

jupyter notebookのデザインをいじることができて、背景の色の変更やフォントの指定、フォントサイズの変更などができます。
github.com


インストール

pipを使って簡単にできます。

pip install jupyterthemes
pip install --upgrade jupyterthemes


使い方

最初にデフォルトの状態を見せた後に、簡単に機能を見ていきましょう。
f:id:looseleaf0727:20181206213212p:plain

テーマの一覧表示

jt -l
>> Available Themes:
      chesterish
      grade3
      gruvboxd
      gruvboxl
      monokai
      oceans16
      onedork
      solarizedd
      solarizedl


テーマの変更

jt -t chesterish

f:id:looseleaf0727:20181206213708p:plain


コードのフォントサイズの変更
デフォルトは11です。

jt -t chesterish -fs 8

f:id:looseleaf0727:20181206213846p:plain


MDのフォントサイズの変更
デフォルトは13です。

jt -t chesterish -tfs 10

f:id:looseleaf0727:20181206215232p:plain


一行の幅の変更
デフォルトは170です。

jt -t chesterish -lineh 120

f:id:looseleaf0727:20181206215458p:plain

ツールバーの表示

jt -t chesterish -T

f:id:looseleaf0727:20181206215918p:plain


名前とロゴの表示

jt -t chesterish -N



デフォルトのテーマに戻す

jt -r



現在使ってる設定

jt -t monokai -tfs 9 -nfs 9 -lineh 120 -cellw 130% -T -N

テーマ一覧

最後に各テーマを表示して終わりにします。
chesterish
f:id:looseleaf0727:20181206213708p:plain


grade3
f:id:looseleaf0727:20181206221042p:plain


gruvboxd
f:id:looseleaf0727:20181206220721p:plain


gruvboxl
f:id:looseleaf0727:20181206230442p:plain


monokai
f:id:looseleaf0727:20181206230618p:plain


oceans16
f:id:looseleaf0727:20181206230741p:plain


onedork
f:id:looseleaf0727:20181206230929p:plain


solarizedd
f:id:looseleaf0727:20181206231334p:plain


solarizedl
f:id:looseleaf0727:20181206231727p:plain

まとめ

今回はjupyterのデザインを変更するjupyterthemesを紹介しました。
モチベーションにもつながるので、ぜひ使ってみてはいかがでしょうか。