今回は大脳新皮質をモデルとした機械学習システムHTMにおけるSP層の学習を簡単なデモと共に紹介していきたいと思います。
以下に簡単なシステム構成を図示します。
この構成の中のSPに当たるのがSpatial Pooler層です。


Spatial Pooler(SP)
では、デモコードと共に実験しながら説明していきたいと思います。
(デモコードはnumenta社が提供している下記サイトのコードを使います。)
Encode
目的
HTMのSPを使って簡単なカテゴリ分類を行います。
分類するのは、cat, dog, monkey, lorisの4つの単語です。
まずは、SPに入力するためにencode(0,1に変換)を行います。encoderはnumenta社が提供するnupicの中にあるCategoryEncoderを使用します。
では、実行していきます!
import numpy
from nupic.encoders.category import CategoryEncoder
from nupic.algorithms.spatial_pooler import SpatialPooler
categories = ("cat", "dog", "monkey", "slow loris")
encoder = CategoryEncoder(w=3, categoryList=categories, forced=True)
cat = encoder.encode("cat")
dog = encoder.encode("dog")
monkey = encoder.encode("monkey")
loris = encoder.encode("slow loris")
print "cat = ", cat
print "dog = ", dog
print "monkey = ", monkey
print "slow loris =", loris
出力結果
cat = [0 0 0 1 1 1 0 0 0 0 0 0 0 0 0]
dog = [0 0 0 0 0 0 1 1 1 0 0 0 0 0 0]
monkey = [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0]
slow loris = [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1]
CategoryEncoder(w=3, categoryList=categories, forced=True)
w=3: 1つの単語を3bitで表す。
categoryList=cotegories: カテゴリ分類するリストの設定
( categories = (("cat", "dog", "monkey", "slow loris")
))
出力結果を見てみると、3つの1でそれぞれの単語が表されていることが分かる。
Spatial Poolerの学習
Spatial Ploolerの学習をシナプスの繋がりを見ながら実験を行なっていきたいと思います。
ソースコード
sp = SpatialPooler(inputDimensions=(15,), columnDimensions=(4,), potentialRadius=15, numActiveColumnsPerInhArea=1, globalInhibition=True, synPermActiveInc=0.03, potentialPct=1.0) print "------------"*4 for column in xrange(4): connected = numpy.zeros((15,), dtype="int") sp.getConnectedSynapses(column, connected) print connected output = numpy.zeros((4,), dtype="int") print "------------"*4 print "20回の学習" for _ in xrange(20): sp.compute(cat, learn=True, activeArray=output) print "------------"*4 print "output", output print "------------"*4 for column in xrange(4): connected = numpy.zeros((15,), dtype="int") sp.getConnectedSynapses(column, connected) print connected print "============"*4 print "200回の学習" for _ in xrange(200): sp.compute(cat, learn=True, activeArray=output) print "============"*4 for column in xrange(4): connected = numpy.zeros((15,), dtype="int") sp.getConnectedSynapses(column, connected) print connected
出力結果
------------------------------------------------
シナプス
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 1 1 0 1 1 1 1 1 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
------------------------------------------------
output [0 0 0 0]
================================================
20回の学習
================================================
output [0 1 0 0]
------------------------------------------------
シナプス
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 1 1 0 0 0 1 1 1 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
================================================
200回の学習
================================================
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 0 0 0 0 0 0 0 0 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
sp.getConnectedSynapses(column, connected)
で、シナプスをランダムで生成する。
output [0 0 0 0]
で分類するための4つの出力を確保
そして、20回の学習を行うと、
output[0100]の2つ目の要素で学習が行われた。
シナプスの学習前と学習後を比較すると、
シナプスの2つ目の配列に変化が見られる。
シナプス
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 1 1 0 1 1 1 1 1 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
------------------------------------------------
output [0 0 0 0]
================================================
20回の学習
================================================
output [0 1 0 0]
------------------------------------------------
シナプス
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 1 1 0 0 0 1 1 1 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
さらに、200回の学習の後、outputとシナプスの変化を見てみると、
output[0100]で2つ目の要素を出力しており、学習が出来ていると言える。
シナプスも2つ目の配列が学習され、
cat = [0 0 0 1 1 1 0 0 0 0 0 0 0 0 0]
と同じになったため、学習が終了したと言えるだろう。
シナプス
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 1 1 0 1 1 1 1 1 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
------------------------------------------------
output [0 0 0 0]
================================================
200回の学習
================================================
------------------------------------------------
output [0 1 0 0]
[0 0 0 0 1 0 1 0 1 1 0 0 1 0 0]
[0 0 0 1 1 1 0 0 0 0 0 0 0 0 0]
[0 1 1 0 1 0 0 1 1 1 0 0 1 1 1]
[0 0 1 0 0 1 0 0 1 0 1 0 1 1 1]
今回は、Spatial Poolerの学習過程について、実行結果と共に紹介しました。
次回は、実際にSpatial Poolerを使った分類を実行していきたいと思います!