人工知性を作りたい

私が日々、挑戦したことや学んだことなどを紹介していく雑記ブログです。 (新しいAI技術HTM, 専門の音声信号処理, 趣味のアニメ等も書いてます。)

HTM(Hierarchical Temporal Memory)におけるTMでの学習(自然言語処理)

今回は大脳新皮質をモデルとした機械学習システムHTMにおけるTM層の学習を簡単なデモと共に紹介していきたいと思います。

 

前回紹介したSP層は、脳がある一瞬を切り取った学習となっています。

そして、今回紹介するTM層が時間情報、時間毎のデータの変化を学習するシステムになります。

 

そこで、今回は、単語の繋がりを予測するシステムを作成して行きます。

 

 

では、説明を行なって行きます。

 

以下に簡単なシステム構成を図示します。

この構成の中のTMに当たるのがTemporal Memory層です。 

f:id:hiro-htm877:20181101121430p:plain
 
また、以下のようなColumnが学習のモデルであり、この表面の1層がSprial Poolerとなり、Columnの縦の繋がりがTemporal Memoryとなります。

 

f:id:hiro-htm877:20181101121802p:plain

 

Temporal Memory(TM)

では、デモコードと共に実験しながら説明していきたいと思います。

(デモコードはnumenta社が提供している下記サイトのコードを使います。)

github.com

 

目的 

HTMのTM層を使って単語の予測を行います。

予測する文章は、dog is animal. です。

まずは、TM層に入力するためにencode(0,1に変換)を行います。encoderはnumenta社が提供するnupicの中にあるCategoryEncoderを使用します。

 

では、実行していきます!

 

ソースコード 


import numpy
from nupic.encoders.category import CategoryEncoder
categories = ("cat", "dog", "animal", "is")
encoder = CategoryEncoder(w=3, categoryList=categories, forced=True)
cat = encoder.encode("cat")
dog = encoder.encode("dog")
animal = encoder.encode("animal")
Is = encoder.encode("is")
print "cat = ", cat
print "dog = ", dog
print "animal = ", animal
print "is = ", Is

 出力結果

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]
animal = [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0]
is =         [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でそれぞれの単語が表されていることが分かる。

 

Temporal Memoryの学習

Temporal Memoryの学習をシナプスの繋がりを見ながら実験を行なっていきたいと思います。学習回数は5回です。


ソースコード

tm = TM(columnDimensions = (15,),
cellsPerColumn=1,
initialPermanence=0.5,
connectedPermanence=0.5,
minThreshold=2,
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.0,
activationThreshold=2,
)

 

categories = ("cat", "dog", "animal", "is")
encoder = CategoryEncoder(w=3, categoryList=categories, forced=True)
cat = encoder.encode("cat")
dog = encoder.encode("dog")
animal = encoder.encode("animal")
Is = encoder.encode("is")
print "cat = ", cat
print "dog = ", dog
print "animal = ", animal
print "is = ", Is
# sys.exit()
dogIsAnimal = []
dogIsAnimal.append(dog)
dogIsAnimal.append(Is)
dogIsAnimal.append(animal)

#ステップ3:学習のためにこの単純なシーケンスを一時記憶装置に送る
#シーケンスを5回繰り返す
print "---------------"*3, "\n"

for i in range(5):

  for j in dogIsAnimal:
    #activeColumns = set([i for i, j in zip(count(), x[j]) if j == 1])
    #activeColumns = set([k for k in range(len(j)) if j[k]==1])
    activeColumns = set([k for k, data in zip(count(), j) if data==1])
    print "activeColumns", activeColumns

    tm.compute(activeColumns, learn = True)


    print("active cells " + str(tm.getActiveCells()))
    print("predictive cells " + str(tm.getPredictiveCells()))
    print("winner cells " + str(tm.getWinnerCells()))
    print("# of active segments " + str(tm.connections.numSegments()))
    print("\n")


tm.reset()


 

 出力結果

================================================
1回目の学習
================================================

activeColumns set([8, 6, 7])
active cells [6, 7, 8]
predictive cells
winner cells [6, 7, 8]
# of active segments 0


activeColumns set([12, 13, 14])
active cells [12, 13, 14]
predictive cells
winner cells [12, 13, 14]
# of active segments 3


activeColumns set([9, 10, 11])
active cells [9, 10, 11]
predictive cells
winner cells [9, 10, 11]
# of active segments 6


================================================
2回目の学習
================================================

activeColumns set([8, 6, 7])
active cells [6, 7, 8]
predictive cells [12, 13, 14]
winner cells [6, 7, 8]
# of active segments 6


activeColumns set([12, 13, 14])
active cells [12, 13, 14]
predictive cells [9, 10, 11]
winner cells [12, 13, 14]
# of active segments 6


activeColumns set([9, 10, 11])
active cells [9, 10, 11]
predictive cells
winner cells [9, 10, 11]
# of active segments 6

以下、省略

 

1回目の学習では、

activeColumns set([8, 6, 7])
active cells [6, 7, 8]
predictive cells
winner cells [6, 7, 8]
# of active segments 0

preactive cellsが  となり、予測できていない。

 

しかし、2回目には1回目の学習の効果で

activeColumns set([8, 6, 7])
active cells [6, 7, 8]
predictive cells [12, 13, 14]
winner cells [6, 7, 8]
# of active segments 6

となり、[8,6,7]つまり[000000111000000]=dogと入力すると、

preactive cells(予測結果)が[12,13,14]つまり、[000000000000111]=is

の結果が出力されている。

学習させている文章がdog is animallなので、しっかりと学習できている。

 

isの入力の後もanimalが出力されている。

 

学習がうまくいったので次は、学習なし→learn=Falseで動かしてみる。

 

テスト用
ソースコード


#######################################################################
#
#ステップ3:ベクトルの同じシーケンスを送信し、
#一時的な記憶
print "=================================================================-"
for j in dogIsAnimal:
  activeColumns = set([k for k, data in zip(count(), j) if data==1])


  #各ベクトルをTMに送信し、学習はオフにします
  tm.compute(activeColumns, learn = False)

 

  #次のprint文は、アクティブなセルを出力します.predictive
  #セル、アクティブなセグメント、および勝者セル。
  #注目すべきは、アクティブな状態が1の列
  #現在の入力パターンのSDRと、
  #予測状態は1であり、次の期待パターンのSDRを表す
  print "\nAll the active and predicted cells:"

  print("active cells " + str(tm.getActiveCells()))
  print("predictive cells " + str(tm.getPredictiveCells()))
  print("winner cells " + str(tm.getWinnerCells()))
  print("# of active segments " + str(tm.connections.numSegments()))

  activeColumnsIndeces = [tm.columnForCell(i) for i in tm.getActiveCells()]
  predictedColumnIndeces = [tm.columnForCell(i) for i in tm.getPredictiveCells()]


  # 1をアクティブ、0をアクティブおよび非アクティブの列に再構成する
  #非アクティブ表現。

  actColState = ['1' if i in activeColumnsIndeces else '0' for i in    range(tm.numberOfColumns())]
  actColStr = ("".join(actColState))
  predColState = ['1' if i in predictedColumnIndeces else '0' for i in range(tm.numberOfColumns())]
  predColStr = ("".join(predColState))

 

  #便宜上、セルはグループ化されています一度に
  # 10。列ごとに複数のセルがある場合、プリントアウト
  #は、列内のセルがスタックされるように配置されています
  print "Active columns: " + formatRow(actColStr)
  print "Predicted columns: " + formatRow(predColStr)

  #1 predictedCells [C] [i]はc番目におけるi番目のセルの状態を表します
  #列。列が予測されているかどうかを確認するには、ORその列のすべてのセルにまた  がって#を入力します。numpyでは、
  #軸1に沿った最大値。

 

出力結果

=================================================================-

All the active and predicted cells:
active cells [6, 7, 8]
predictive cells [12, 13, 14]
winner cells [6, 7, 8]
# of active segments 6
Active columns: 0000001110 00000
Predicted columns: 0000000000 00111


All the active and predicted cells:
active cells [12, 13, 14]
predictive cells [9, 10, 11]
winner cells [12, 13, 14]


# of active segments 6
Active columns: 0000000000 00111
Predicted columns: 0000000001 11000

All the active and predicted cells:
active cells [9, 10, 11]
predictive cells []
winner cells [9, 10, 11]
# of active segments 6
Active columns: 0000000001 11000
Predicted columns: 0000000000 00000

 

Active columnsが[0000001110 00000]=dogを入力すれば

Predicted columns: [0000000000 00111]=isが予測される

 

Active columnsが[0000000000 00111]=isを入力すれば

Predicted columns: [0000000001 11000]=animalが予測される

 

Active columnsが[0000000001 11000]=animalを入力すれば

Predicted columns: [0000000000 00000]となり、何も予測されない結果となった。

これは、animalの後に何も学習させていないためである。

 

 

このように、TM層では時間情報を学習、予測することができます。

うまく使えば、株価の予測、心拍数の異常検知などにも使われています。

 

ぜひ皆様も、従来の機械学習だけでなく、

HTMを使って何かアプリケーションを作って見てください!

 

 

次回は、Temporal Memoryを使って分類をもう少し難しい文章を理解できるようにし、簡単な会話ができるシステムを構築してみようと思っています。

 

お読み下さりありがとうございました!