深層学習とその他

機械学習したいマン

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を軽く紹介しました。
以前よりもかなり種類が増えていて説明も少し細かくなっています。
みなさんも画像や自然言語、いろんなサンプルを試してみてくださいね。