深層学習とその他

機械学習したいマン

tqdmで精神衛生を保つ

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


今日はプログレスバーを表示するtqdmについて書いていきます。

tqdmとは

tqdm means “progress” in Arabic (taqadum, تقدّم) and is an abbreviation for “I love you so much” in Spanish (te quiero demasiado)

https://pypi.org/project/tqdm/

tqdmはアラビア語プログレスを意味し、
スペイン語での "I love you so much"の略語らしいです(豆知識)。

機能としてはプログレスバーを表示し、処理速度も簡易ですが表示されます。

使用例

for文の中に組み込むことで、プログレスバーを表示できます。

from tqdm import tqdm
import time
for i in tqdm(range(10)):
   time.sleep(1)


f:id:looseleaf0727:20181205213913p:plain
tqdm


jupyter notebookを使っている際にはimportを変えることで、よりリッチな表示にできます。

from tqdm import tqdm_notebook as tqdm
import time
for i in tqdm(range(10)):
   time.sleep(1)

f:id:looseleaf0727:20181205214302p:plain
tqdm_nb


アニメーションにしました。
ちょっと楽しいですね。

f:id:looseleaf0727:20181205222535g:plain
tqdm_anime


プログレスバーについて説明を出すこともできます。

from tqdm import tqdm_notebook as tqdm
import time
for i in tqdm(range(3), desc="first loop"):
    for j in tqdm(range(10), desc="second loop"):
        time.sleep(0.1)

f:id:looseleaf0727:20181205224151g:plain
tqdm description

まとめ

今回はtqdmについて書きました。
前処理などの時間がかかる処理や、プログレスバー機能がない深層学習の学習ループに使うとずっと見てなくてすむかもしれません。
今回紹介してない機能もまだまだあるのでこちらを参考にしてください。
pypi.org

pythonでバイナリを扱う

一人アドベントカレンダー4日目です。
たまたまpythonでバイナリを扱ったのでそれに関してのメモを書いていきます。

adventar.org

基本的な扱い

基本的にはstructで扱えばいいと思います。
パックするときはpackを使い、フォーマット文字列を指定します。
アンパックするときにはunpackを使い、同じフォーマット文字列を指定すれば戻すことができます。

import struct

x = struct.pack('II', 1000, 0)
print(x)
>> b'\xe8\x03\x00\x00\x00\x00\x00\x00'

print(struct.unpack('II', x))
>> (1000, 0)


今回はバイナリデータを読み込むためにこれを使いました。

f = open(hoge.txt, "rb")
offset, length = struct.unpack("II",f.read(8))

こんな感じでほしいデータを引っ張ることができました。

2018年度Amazonで買ってよかったもの

アドベントカレンダー3日目です。

adventar.org

今日は技術的な話ではなく、Amazonで買ってよかったものを紹介したいと思います。

 

今年は比較的Amazonで購入した金額が多く、157417円ほど使っていたようです。

調査方法は以下のブログを参考にしました。

Amazon歴13年で使用した金額を出力してみました - 力こそパワー

では、紹介していきます。

 

LOGICOOL ウェブカム HD画質 120万画素 C270

LOGICOOL ウェブカム HD画質 120万画素 C270
ロジクール (2010-08-20)
売り上げランキング: 118
マイクが内蔵されているので、卓上にマイクを置かなくていいところもいいですね。
あとは部屋が暗くても比較的きれいに映るところもいいですね。
 

難消化性デキストリン(水溶性食物繊維)400g(微顆粒品)

難消化性デキストリン(水溶性食物繊維)400g(微顆粒品)【ヘルシーカンパニー】
 
 
 
普段食事で食物繊維が足りていないと思うので、自分はこれでとっています。
冷たい飲み物にはあまり溶けないので、温かい飲み物に入れるとおすすめです。
一度に使う分量としては、スプーン一杯くらいをいれてます。
 

F-Foto キャノン EOS Kiss X9i/X9/X8i/X7i/80D/70D/9000D/8000D ダブルズームキット に適合 EW-63C & ET-63 互換フード 、レンズ 保護 フィルター 2枚 と 一眼カメラケース のセット

F-Foto キャノン EOS Kiss X9i/X9/X8i/X7i/80D/70D/9000D/8000D ダブルズームキット に適合 EW-63C & ET-63 互換フード 、レンズ 保護 フィルター 2枚 と 一眼カメラケース のセット (衝撃吸収 セミハードケース ブラック)636358F_CASE
 
 

LP-E17 Newmowa 互換バッテリー 2個 + 充電器 対応機種 Canon LP-E17 Canon EOS M3 M5 M6 200D 750D 760D 800D Rebel T6i T6s 8000D 9000D Kiss X8i Kiss X9

LP-E17 Newmowa 互換バッテリー 2個 + 充電器 対応機種 Canon LP-E17 Canon EOS M3 M5 M6 200D 750D 760D 800D Rebel T6i T6s 8000D 9000D Kiss X8i Kiss X9 (ハーフデコード)
出かけてるときにバッテリーが切れるのが怖かったので買いました。
バッテリーが二つもついてこの値段なのは素晴らしいです。
いまのところ使っていて問題も起きていません。
 
 

APAGARD(アパガード) スモーキン

APAGARD(アパガード) スモーキン 【医薬部外品】 100g
サンギ (2014-09-16)
売り上げランキング: 417
クリアクリーンなどと比べて少し値は張りますが、歯を白くする効果を求めてこれを買いました。
スモーキンという名前ですが、喫煙者だけではなく、コーヒーや紅茶をよく飲む人にもおすすめです。
 
 

竹井器物製作所 フィーノ コーヒードリップポット 1.2L

 

竹井器物製作所 フィーノ コーヒードリップポット 1.2L
竹井器物製作所
売り上げランキング: 4,687
お湯を沸かすことももちろんできます。
お店で見るとドリップポットはどれも高かったのですが、
Amazonでこれを買ったときは1500円程度で買うことができました。
ハンドドリップに挑戦したい方におすすめです。
 
 

IZUMI VIDAN 往復式シェーバー ソリッドシリーズ 3枚刃 レッド IZF-V26-R

 

IZUMI VIDAN 往復式シェーバー ソリッドシリーズ 3枚刃 レッド IZF-V26-R
泉精器製作所 (2016-09-09)
売り上げランキング: 761
二十歳過ぎてきてから、剃刀負けに悩むようになりました。
ちゃんと保湿したりしても肌が切れて、困っていたのでこれを買ってみました。
電動のシェーバーはとても高いイメージがありましたが、
これは2000円程度で買えます。
 
使ってみた感想としては、思ったよりも全然きれいに剃れます。
そして、剃刀負けをすることは全くなくなりました。
剃刀負けに悩んでいる人は、とりあえず試してみるのがいいと思います。
 
 

なとり 激辛柿の種&ピーナッツ 60g×10袋

なとり 激辛柿の種&ピーナッツ 60g×10袋
なとり
売り上げランキング: 3,027
辛い物がとても好きで、普段ハバネロを使ったりする人におすすめです。
コンビニの中本のラーメンとかとは比にならない辛さです。
これだけ辛い物なのに値段は一袋100円です。
自分はハイボール飲んだりするときに食べたりしてます。
 
 

ドライペット 除湿剤 ふとん快適シート くりかえし再生タイプ 1シート入

ドライペット 除湿剤 ふとん快適シート くりかえし再生タイプ 1シート入
万年床になっているので、布団が湿気を吸うのが気になっていました。
これを使うようになってからは、布団が湿気てるとは感じなくなりました。
一つ欠点があるとすれば、敷布団が薄い場合には、このシートの硬さが気になるかもしれません。
 
 

【正規品】 BODUM ボダム BRAZIL フレンチプレスコーヒーメーカー 1.0L ブラック

【正規品】 BODUM ボダム BRAZIL フレンチプレスコーヒーメーカー 1.0L ブラック 10938-01
フレンチプレスコーヒーメーカーです。
普段はハンドドリップで入れてますが、入れるときまあまあ時間がかかります。
ちょっと面倒だなっというときにはフレンチプレスを使っています。
一回の量も1Lも作れるのが強みです。
ハンドドリップと比べてあまりコツがいらないのがおすすめポイントです。
買う際には、タイムセールの時を狙って買うと半額近くで買えます。
 

まとめ

今日はAmazonで買ってよかったものを紹介しました。
最近では、明らかに翻訳っぽい商品レビューも多いので、気を付けるようにしましょう。

深層学習を学ぶ人におすすめのツイッターアカウント

アドベントカレンダー2日目です。

adventar.org

深層学習について学んだり、新しい情報をキャッチアップすることは結構大変です。
毎日新しい論文を確認したり、海外の英語の情報を見てる人もいますが、
誰しもが行えるわけではありません。

 

そこで今回は、できるだけ気楽に情報を集められるツイッターのアカウントを紹介していきます。

 

 

piqcy (@icoxfog417) | Twitter

英語の論文や海外のツイートなどを翻訳してツイートしてくれます。
情報も画像や自然言語処理などいろんな分野についてツイートされています。

 

Ryobot | りょぼっと (@_Ryobot) | Twitter

メンヘラちゃんが最新論文について紹介してくれるツイートが有名です。
内容的に難しい論文も雰囲気をわかりやすくつかむことができると思います。

 

aidiary (@sylvan5) | Twitter

深層学習に関する記事や有益な情報をリツイートしています。

 

arXivTimes (@arxivtimes) | Twitter

 論文の要約情報をまとめてるサイト、arXivTimesの更新情報をツイートするアカウントです。

 

人工知能,機械学習関係ニュース研究所 (@AI_m_lab) | Twitter

AIや深層学習についての記事をツイートするアカウントです。
記述的な話というよりは、一般的なAIに関するニュースが多いです。

DLHacks (@DL_Hacks) | Twitter

東京大学の松尾研が運営してるアカウントで、論文の輪読会スライドがあげられています。

 

まとめ

今回はおすすめのツイッターアカウントを紹介しました。
ほかにもおすすめのアカウントがあったらぜひ教えてください。

chainerのexamplesについて

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

昨年に引き続きやっていきますが、
去年よりは忙しいのですべて埋まるかは怪しいです。

今日はChainerのexamplesについて書いていきたいと思います。
github.com

Chainerのexamplesは少しずつ数が増えていて、
昔はmnist, imagenet, ptb, cifar, vae ....と、
そんな感じだったと思うのですが、
現在ではこれだけあります。

f:id:looseleaf0727:20181129014047p:plain
chainer examples list

実際にそれぞれ見たことある方はわかると思いますが、詳しい説明や実行方法については触れられていないものもあります。
そこで、今回はそれぞれがどういうものなのか、ということを順に説明したいと思います。

caffe_export

This example shows how to export Chainer models into Caffe protobuf format. With this example, you can easily export the following five pre-trained models:
・GoogLeNet
・ResNet50
・ResNet101
・ResNet152
・VGG16

https://github.com/chainer/chainer/tree/master/examples/caffe_export

読んでそのままですが、ChainerのモデルをCaffeのprotobuf形式にexportするもののようです。
この例では、上記の5つの学習済みモデルをexportする例が示されています。

chainermn

特に説明は書かれていませんが、ChainerMNに関するサンプルだと思います。
ChainerMNに関する詳しい話はこちらである程度わかると思います。
Overview — ChainerMN 1.3.1 documentation
複数のGPUを使っている人、複数のノードを用いて学習する人向けの例で、並列処理をしたい人は参考になるかもしれません。ChainerMN自体があまり個人向けではないかも知れませんが。

cifar

Convolutional neural networks for CIFAR-10 and CIFAR-100 Classification

This is an example of a convolutional neural network (convnet) applied to an image classification task using the CIFAR-10 or CIFAR-100 dataset. The CIFAR datasets can be a good choice for initial experiments with convnets because the size and number of images is small enough to allow typical models to be trained in a reasonable amount of time. However, the classification task is still challenging because natural images are used.

https://github.com/chainer/chainer/tree/master/examples/cifar

CIFAR-10、CIFAR-100と呼ばれるデータセットがあります。
CIFAR-10は10クラスにラベル付けされた画像のデータセットで、
CIFAR-100は100クラスにラベル付けされたデータセットです。
主に画像分類に用いられるデータセットで、このサンプルでは、CNNを用いて学習を行うようです。

dcgan

DCGAN

This is an example implementation of DCGAN (https://arxiv.org/abs/1511.06434) using trainer.

This code uses Cifar-10 dataset by default. You can use your own dataset by specifying --dataset argument to the directory consisting of image files for training. The model assumes the resolution of an input image is 32x32. If you want to use another image resolution, you need to change the network architecture in net.py.

https://github.com/chainer/chainer/tree/master/examples/dcgan

DCGANと呼ばれる、画像生成によく使われるネットワークのサンプルですね。この例では、cifar-10データセットを用いて学習を行えるようです。

glance

Multi-Layer Perceptron for mushroom lethality categorization

This is a minimal example to write a feed-forward net. The code consists of three parts: dataset preparation, network and optimizer definition and learning loop. This is a common routine to write a learning process of networks with dataset that is small enough to fit into memory.

https://github.com/chainer/chainer/tree/master/examples/glance

MLPを使って、マッシュルームのデータセットを分類するサンプルです。この例は、フィードフォワードネットワークの参考になると思います。

image_captioning

Image Captioning with Convolutional Neural Networks

This is an example implementation of Show and Tell: A Neural Image Caption Generator (https://arxiv.org/abs/1411.4555) a generative image captioning model using a neural network with convolutional and recurrent layers. Given an image, this model generates a sentence that describes it.

https://github.com/chainer/chainer/tree/master/examples/image_captioning

イメージキャプションに関するサンプルです。モデルとしては、CNN+RNNを使うようです。

imagenet

Description

This is an experimental example of learning from the ILSVRC2012 classification dataset. It requires the training and validation dataset of following format:
・Each line contains one training example.
・Each line consists of two elements separated by space(s).
・The first element is a path to 256x256 RGB image.
・The second element is its ground truth label from 0 to 999.

https://github.com/chainer/chainer/tree/master/examples/imagenet

ILSVRC2012の分類データセットのサンプルです。デフォルトalexnetやgooglenet、resnetなどのモデルを使うことができます。

memnn

End-to-end memory networks example

This is an example of end-to-end memory networks[1].
[1] Sainbayar Sukhbaatar, Arthur szlam, Jason Weston and Rob Fergus. End-To-End Memory Networks. https://papers.nips.cc/paper/5846-end-to-end-memory-networks

https://github.com/chainer/chainer/tree/master/examples/memnn

モリーネットワークに関するサンプルです。
独特なモデルをしてると思うので、見てみると面白いかもしれません。

mnist

Multi-Layer Perceptron for MNIST Classification

This is a minimal example to write a feed-forward net. The code consists of three parts: dataset preparation, network and optimizer definition and learning loop. This is a common routine to write a learning process of networks with dataset that is small enough to fit into memory.

https://github.com/chainer/chainer/tree/master/examples/mnist

シンプルなMLPを使ったMNISTの分類のサンプルです。
MNISTは0~9までの数字の画像のデータセットで、一番基礎となるサンプルだと思います。

modelzoo

Evaluate a Caffe reference model

This is an example of evaluating a Caffe reference model using ILSVRC2012 classification dataset. It requires the validation dataset in the same format as that for the imagenet example.

Model files can be downloaded by download_model.py. AlexNet and reference CaffeNet requires a mean file, which can be downloaded by download_mean_file.py.

https://github.com/chainer/chainer/tree/master/examples/modelzoo

先ほど出てきたILSVRC2012のデータセットを使って、caffeのモデルを評価するサンプルだと思います。

pix2pix

chainer-pix2pix

chainer implementation of pix2pix https://phillipi.github.io/pix2pix/

https://github.com/chainer/chainer/tree/master/examples/pix2pix

pix2pixを実装したサンプルになります。生成系によく使われるモデルで、上記のURLから実際にデモを動かすことができるので、それを触ってみることでpix2pix自体のイメージはつかめるかもしれません。

pos

POS-tagging

This is an example of a POS-tagging problem for natural language processing. Part of speech (POS) is a category of word, such as noun and verb. POS-tagging problem is the task of assigning part of speech to each word in a sentence. This problem can be formulated as a sequential labeling problem. So, you can apply this example to another problems such as named entity recognition.

This example uses traditional linear-chain CRF model. And of course you can extends this model with RNN, LSTM, Bi-LSTM and CNN.

https://github.com/chainer/chainer/tree/master/examples/pos

自然言語処理のPOSタグ付けに関するサンプルです。
モデル自体はlinear-chain CRFを使っているようです。

ptb

Recurrent Net Language Model

This is an example of a recurrent net for language modeling. The network is trained to predict the word given the preceding word sequence.
This example is based on the following RNNLM implementation written in Torch7. https://github.com/tomsercu/lstm

https://github.com/chainer/chainer/tree/master/examples/ptb

次の単語を予想していくRNNのサンプルです。
ちなみにptbはPenn Tree Bankの略で、使用するデータセットの名前です。

reinforcement_learning

Reinforcement Learning Examples

These are minimal examples of writing reinforcement learning agents. You need to install gym before running the examples.

dqn_cartpole.py implements DQN and DoubleDQN and supports discrete-action tasks.
・ddpg_pendulum.py implements DDPG and supports continuous-action tasks.

https://github.com/chainer/chainer/tree/master/examples/reinforcement_learning

強化学習に関するサンプルで、cartpole(棒を倒さないように保ゲーム)とpendulum(振り子のゲーム)を動かすサンプルです。
ネットワークにはDQNとDoubleDQNなどが使われているようです。
chainerrlは使われていないみたいです。

sentiment

Recursive Nets for Sentiment Analysis

This example implements the simple recursive model by Richard Socher. It requires the preprocessed dataset which is available by running download.py.

https://github.com/chainer/chainer/tree/master/examples/sentiment

感情分析に関するサンプルです。
RecursiveNetと呼ばれるネットワークが使われています。

seq2seq

Sequence-to-sequnce learning example for machine translation

This is a minimal example of sequence-to-sequence learning. Sequence-to-sequence is a learning model that converts an input sequence into an output sequence. You can regard many tasks in the natural language processing field as this type of task, such as machine translation, dialogue and summarization.

https://github.com/chainer/chainer/tree/master/examples/seq2seq

自然言語処理によく使われるseq2seqのサンプルです。
ネットワークには、NStepLSTMが使用されているので、そちらの参考にもなるかもしれません。

serialization

Serialization

This example intends to show how to use serializers with minimal code. These scripts show two ways to save/load snapshots using NumPy and H5Py, so please ensure that you have installed h5py package besides NumPy preliminary.

https://github.com/chainer/chainer/tree/master/examples/serialization

モデルの保存や読み込みなどのサンプルです。
この例では、H5Pyを使う方法とserializersを使う方法が示されています。

static_graph_optimizations

Demonstration of "static subgraph optimizations" feature

The subfolders in this directory contain versions of the following Chainer examples that were modified to support this feature. Since the static graph optimizations feature is experimental, these examples might be unstable.

https://github.com/chainer/chainer/tree/master/examples/static_graph_optimizations

静的グラフ最適化に関するサンプルとのことで、まだ実験的な機能のようです。

text_classification

Neural Networks for Text Classification

This is an example of text classification using typical neural networks. This code can switch choices below:
・LSTM
・CNN + MLP
・BoW + MLP
・Character-based variant models of those

https://github.com/chainer/chainer/tree/master/examples/text_classification

文書分類に関するサンプルです。
LSTM、CNN+MLP、BoW+MLPなどのネットワークを使って学習をすることができます。

vae

Variational AutoEncoder

This is a sample implementation of variational autoencoder.
This method is proposed by Kingma and Welling, Auto-Encoding Variational Bayes, 2014.

https://github.com/chainer/chainer/tree/master/examples/vae

Variational AutoEncoder(VAE)に関するサンプルです。
mnistのデータセットを使い、画像を生成するようです。

wavenet

WaveNet

A Chainer implementation of mel-spectrogram vocoder using WaveNet.

https://github.com/chainer/chainer/tree/master/examples/wavenet

一時期話題になったWaveNetのChainer実装です。
音声処理に興味がある人はいいかも知れません。

word2vec

Word Embedding

This is an example of word embedding. We implemented Mikolov's Skip-gram model and Continuous-BoW model with Hierarchical softmax and Negative sampling.

https://github.com/chainer/chainer/tree/master/examples/word2vec

word embeddingに関するサンプルです。
word2vec自体は知ってる人も多いと思います。

まとめ

今回はChainerのexamplesを軽く紹介しました。
以前よりもかなり種類が増えていて説明も少し細かくなっています。
みなさんも画像や自然言語、いろんなサンプルを試してみてくださいね。

複数のファイルをunzipする

複数のファイルをunzipする場合、こういう書き方が思い浮かぶと思います。

unzip *.zip

実際にこのコマンドを打ち込んでも解凍はできません。
以下のようなエラーが出ました。

End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.

書き方を少しだけ変えるだけで複数ファイルの解凍ができます。ダブルクオーテーションで囲うだけです。

unzip "*.zip"

XGBoostでInvalid Parameter format for max_depth expect int but valueが出る

XGBoostでパラメータを設定する際にエラーが出ました。

model = xgb.XGBRegressor(params)
XGBoostError: b"Invalid Parameter format for max_depth expect int but value='{'eval_metric': 'rmse', 'learning_rate': 0.1}'"


こちらを参考にしたところ、動くようになりました。
github.com

model = xgb.XGBRegressor(**params)