pylearn2 メモ

Pylearn2とは

Welcome — Pylearn2 dev documentation

Pylearn2 is still undergoing rapid development. Don’t expect a clean road without bumps!

まだ開発中のライブラリとのこと。

手法

ニューラルネットワークの課題

  • 誤差情報が伝播されない
  • 収束が遅い
  • 局所解に陥る
  • オーバーフィッティング

DeepLearning

  • 1階層ごとに教師無し学習で事前学習すると良い結果が得られることが示した(Hinton)
  • 猫の画像にだけ反応するニューロン(Andrew Ng)
SdA

Stacked denoising Autoencoder
denoising 可視層にノイズをかけることでロバストな性質を持つようになる
積み重ねるからStack

PyLearn2

  • Theano 行列、数値計算 Cのコードを自動でつくる CUDA->GPU利用も自動化
  • Numpy とか Scipy とか
  • Yoshua Bengio先生の研究室
  • LISAラボ
  • Theanoの自動微分が優秀
  • TheanoはGPUコード自動生成も

環境構築

必要なパッケージが多い。
NumPyはaptで入れたほうがいい、などの制約があるらしい。

Vagrant box

楽な環境構築方法としてはvagrantべアボーンを使うのが良さそう。
pylearn2 入門したい編 - laughingのブログ

Theano

Pylearn2のベースになっている。導関数を解析的に導出してくれる(自動微分)。
GPU用のコードも出せる。

参考スライド

Theano動作例

# vi theano.py

from theano import function, pp
from theano.tensor.nnet import sigmoid
import theano.tensor as T

# sample 1
x = T.dscalar('x')
y =  x ** 2
df = T.grad(y, x)
f = function([x], df)
print "sample1: ", pp(f.maker.fgraph.outputs[0])

# sample 2
y =  sigmoid(x)
df = T.grad(y, x)
f = function([x], df)
print "sample2: ", pp(f.maker.fgraph.outputs[0])

# sample 3
w = T.dscalar('w')
y =  sigmoid(T.dot(x, w))
df = T.grad(y, x)
f = function([x,w], df)
print "sample3: ", pp(f.maker.fgraph.outputs[0])
# python theano.py
sample1:  (TensorConstant{2.0} * x)
sample2:  Elemwise{Composite{[Composite{[mul(i0, sub(i1, i0))]}(scalar_sigmoid(i0), i1)]}}(x, TensorConstant{1.0})
sample3:  Elemwise{Composite{[Composite{[mul(i0, sub(i1, i0), i2)]}(Composite{[scalar_sigmoid(mul(i0, i1))]}(i0, i1), i2, i1)]}}(x, w, TensorConstant{1.0})

CIFER-10のチュートリアル

CIFERのデータセット
CIFAR-10 and CIFAR-100 datasets

The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.

本来のCIFAR10の入力データ次元数は3x32x32。Pylearn2のgrbm_smdでは、make_dataset.pyのpreprocessing.ExtractPatches(patch_shape=(8, 8)で縮小しているらしい。
縮小してるのでなく、パッチをつくっている。

$ cd ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd
$ python make_dataset.py (実行済み)
$ train.py cifar_grbm_smd.yaml  # 学習 (2分ほどかかる)
$ show_weights.py --out=weights.png cifar_grbm_smd.pkl
$ print_monitor.py cifar_grbm_smd.pkl

YAMLファイルに学習方法(dataset,model,algorithm)を記述する。学習結果はpklに出力される。(pklとはpythonオブジェクトをシリアライズしたもの。)
cifar_grbm_smd.yaml

とくに重要な部分は下記。

dataset: !pkl: "cifar10_preprocessed_train.pkl",
model: !obj:pylearn2.models.rbm.GaussianBinaryRBM {

        # The RBM needs 192 visible units (its inputs are 8x8 patches with 3
        # color channels)
        nvis : 192, # Num of visible 可視層のニューロン数

        # We'll use 400 hidden units for this RBM. That's a small number but we
        # want this example script to train quickly.
        nhid : 10, # 隠れ層のユニット数
algorithm: !obj:pylearn2.training_algorithms.sgd.SGD {
        # The learning rate determines how big of steps the learning algorithm
        # takes.  Here we use fairly big steps initially because we have a
        # learning rate adjustment scheme that will scale them down if
        # necessary.
        learning_rate : 1e-1, # 学習率

        # Each gradient step will be based on this many examples
        batch_size : 5, # バッチサイズ

基本的な使い方

  • YAMLをつくる
  • 学習 train.py
  • 評価・変更
    • show_weighs.py xxx.png xxxxx.pkl
    • print_monitor.py xxx.pkl|grep test_y_misclass

MNIST

MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges
手書きの数字データ。

$ cd ~/pylearn2/pylearn2/scripts/datasets
$ python download_mnist.py (実行済み)
$ cd ~/pylearn2/pylearn2/scripts/tutorials/stacked_autoencoders/tests
$ python test_dae.py

deep_trainer

3層以上のニューラルネットをつくる。

cd /home/ubuntu/pylearn2/pylearn2/scripts/tutorials/deep_trainer
python run_deep_trainer.py

  • d mnist でデータセットを指定する。指定しないとトイデータ。

Maxout

MNIST,cifAR で実行できるようになってる。
活性化関数を学習する。

$ cd ~/pylearn2/pylearn2/scripts/papers/maxout
$ train.py mnist_pi.yaml
$ train.py mnist_pi_continue.yaml

Dropout

ランダムに隠れ層を使えないような状態にして学習する。
欠損データがあってもロバスとに学習できるようにする。

次のステップ

GPUを利用する

theanoは簡単にGPUに対応できる
/.theanorc
[global]
device=gpu
floatX=float32

まとめ

pre-trainingよりlearning Rate, Batch size, Epoch数、Hiddenレイヤーのユニット数が影響大きい。
Pre-Trainingは最後の寄り切りに近い。

  • libsvm等にくらべて敷居が高い。
  • 自分のデータで実行するためにはpythonでラッパを書く必要がある。
  • ライブラリとして未完,カスタマイズが必要
  • 汎用データ形式に対応していない
  • 学習過程がわかりにくい
  • 学習と検証はわけたい


新しいデータを適用したい場合
基本:pylearn2/pylearn2/datasetsにクラスを追加


汎用っぽいのもある
Matlab 不明 matlab_dataset.py
たとえばlibsvm形式を汎用でつかえるようにするとか


そもそもYAML定義や Pythonコードが面倒
自分でスクリプトを作って楽にする、とか。


Pylearn は Theano をベースとしている。
SdA DBN Maxout
GPU対応
ライブラリとして未完
ハックが必要

ほかのツール
Matlab がおおい
トロント大のヒントン先生界隈もMatlab

vagrantによる環境構築について

virtualbox+vagrant
pylearnの入ったubuntuvm(precise64)がある。
pylearn2 入門したい編 - laughingのブログ
vagrant up で動かせた。プロビジョニングでPuppetを使っている。

twitterイコン画像で試したひとのブログ

pylearn2 入門したい編 - laughingのブログ
チュートリアルにもあるgrbmを使って学習している。