いわて駐在研究日誌

OpenCAE、電子工作、R/C等、徒然なるままに

SOM_PAK

遅ればせながら長男が生まれ、お産の入院やら退院やらでばたばたしていたが、嫁実家に落ち着いたので、ようやく日記記録を復帰させることができた。

 

MOGAなどで出てきたパレート解をSOM(自己組織化写像 - Wikipedia)で評価してみたい。SOMのアルゴリズム自体は、コホネン先生の提案から始まり、先生ご提供のCソース(som_pak)とか、pythonの実装(SOMPY)とか、Rでの実装とか、多数存在している。また、最近は新しい類似の手法(GTM:generative topographic map)なども提案されている。

SOMの基本アルゴリズム

  1. SOMマップの大きさ(例えば2次元64x64など)を決める。
  2. 入力データの大きさ(パレート解の探索ベクトルの大きさ)と同じユニットをマップの大きさ分宣言し、それぞれのユニットの成分を乱数で初期化する。
  3. 入力データから1つパレート解の探索ベクトルを選び、全てのユニットベクトルとのユークリッド距離を求める。
  4. 最小のユークリッド距離を持つユニットベクトルを選定し、勝者ユニットとする。
  5. 勝者ユニットおよびその近傍を次式で更新する。iは繰り返し回数の添え字である。hは近傍関数、kは学習効率係数で、繰り返し回数とともに減少する(更新の影響領域が小さくなる)。  W[i+1]=W[i]+h[i]*k[i](V[i]-W[i])
  6. 全ての入力データVについて行い、Wが収束していなければ、3にもどる。

SOMのパッケージ

- SOM_PAK(HUT - CIS - Research - SOM_PAK, LVQ_PAK)

wget http://www.cis.hut.fi/research/som_pak/som_pak-3.1.tar

tar xvf som_pak-3.1.tar

cd som_pak-3.1

開発当時とコンパイラの条件が異なりgetline関数の宣言が重複する。そこで、最近のgccでビルドするには、*.c/*.hの各所のgetline関数の呼び出しをリネームするか、コンパイラに-ansiオプションをつけてビルドする必要がある。ここでは、簡単に以下のようにしてみる。

vi makefile.unix

[snip]

CC=gcc
CFLAGS=-O2 -ansi

[snip]

make -f makefile.unix

付属データex.datを使ったテスト

①データファイルの作成

cat ex.dat

5   ・・・カラム数
13.575570 12.656892 -1.424328 -2.302774 404.921600
13.844373 12.610620 -1.435429 -1.964423 404.978180
13.996934 12.669785 -1.384147 -1.830788 405.187378
14.060876 12.755087 -1.378407 -2.020230 404.89254

  :

②参照ベクトル(ユニット)の生成(ex.cod)

./randinit -din ex.dat -cout ex.cod -xdim 64 -ydim 64 -topol hexa -neigh bubble -rand 123

※ サイズ(64x64)、ヘックスマップ、近傍関数bubble(or gaussian)

③SOMのinteractive実行
./vfind
This program will repeatedly run the initialization, training
and testing cycle for Self-Organizing Map algorithm.

In the following the training file name, the test file name
(that can be the same) and the map save file name are asked.
After them the type of map topology is asked, as well as
the type of neighborhood function. The x- and y-dimension
of the map should be integers and prefereably x-dimension
should be larger than y-dimension.

The training is done in two parts. First an ordering phase
that is usually shorter than the following converging phase.
The number of training cycles, the training rates and
the radius of the adaptation area are asked separately for
both phases. The fixed point qualifiers and weighting qualifiers
are used if the corresponding parameters were given.

The quantization error is computed for each map and
the best map (smallest quantization error) is saved to
the given file. If the verbose parameter allows the quantization
error is given for each separate trial.

After the answers have been given the training begins
and depending on the size of problem it may take a long time.

Give the number of trials: 3  
Give the input data file name: ex.dat
Give the input test file name: ex.cod
Give the output map file name: ex.cod
Give the topology type: hexa
Give the neighborhood type: bubble
Give the x-dimension: 64
Give the y-dimension: 64
Give the training length of first part: 1000
Give the training rate of first part: 0.05
Give the radius in first part: 10
Give the training length of second part: 10000
Give the training rate of second part: 0.02
Give the radius in second part: 3

 

④可視化(マップの生成)

- ラベル付け(うまくいかない?)

./vcal -din ex.dat -cin ex.cod -cout ex.cod [-numlabs x]

- ラベルの位置確認(多すぎると分かりくいため)

./visual -din ex.dat -cin ex.cod -dout ex.vis

- マップ作成(モノクロ)

planes -cin ex.cod -plane 1 -ps 1

 

参考文献:

徳高 他、自己組織化マップの応用(第2版)、海文堂(2005)

ちなみにSOMのpython実装はたくさんあるので紛らわしい。どれが良いか悩むのは後回しにする。

 

sompy  jpzk/sompy · GitHub

SOMPY  sevamoo/SOMPY · GitHub

Sompy  kdickerson/Sompy · GitHub

Self Organizing Maps in Python  Self Organizing Maps in Python :: Source Code :: Paras Chopra