FUNC-RETURN

システム、インフラ、AI、デザイン

【HTM - Python】Swarmingでモデル最適化、新皮質モデルで機械学習

f:id:kijnmb-1110:20171231231233p:plain





HTM記事全体のの目次は以下。

【HTM - Python】新皮質モデルで機械学習(目次) - FUNC-RETURN

スウォーミング

NupicのSwarmingAPIはHTMで使うモデルの構造や各パラメータなどの最適解を教えてくれます。

Swarming — NuPIC 1.0.3 documentation

前準備

NupicはSwarmingにMysqlを使うので、最初にインストールしておいてください。

# Macの場合
brew install mysql
mysql.server start

スウォーミングパラメータ

スウォーミングのためのパラメータを設定します。(メタのメタ)

# -*- coding: utf-8 -*-

SWARM_DESCRIPTION =  {
  "includedFields": [
    {
      "fieldName": "sine",
      "fieldType": "float",
      # メモリ的にも精度的にも値の範囲が決まっていれば指定した方が良さそう
      "maxValue": 1.0,
      "minValue": -1.0
    }
  ],
  "streamDef": {
    "info": "sine",
    "version": 1,
    "streams": [
      {
        "info": "sine.csv",
        "source": "file://sine.csv",
        "columns": [
          "*"
        ]
      }
    ]
  },
  # 予測タイプ:時系列データに対する予測で、異常検知をしてくれる。
  "inferenceType": "TemporalAnomaly",
  "inferenceArgs": {
    # 一つ次の値を予測する
    "predictionSteps": [
      1
    ],
    # 予測対象のフィールド名
    "predictedField": "sine"
  },
  # スウォーミングの規模
  # [small, medium, large]のどれかで、処理時間と精度の期待値の天秤のような感じ
  "swarmSize": "medium"
}

inferenceType

Utilities — NuPIC 1.0.3 documentation

Document曰く以下のタイプが存在するようですが、それぞれの詳細な説明は特にないです。
名前で察しましょう。

  • TemporalNextStep
  • TemporalClassification
  • NontemporalClassification
  • TemporalAnomaly
  • NontemporalAnomaly
  • TemporalMultiStep
  • NontemporalMultiStep

predictionSteps

以下のQAでかなりわかりやすい議論がされているのでぜひ読んでください。
要は、何ステップ後のデータを予測したいかを指定すると、それに最適化されたモデルが生成されます。

Predicting multiple steps into the future - NuPIC - HTM Forum

swarmSize

smallは非常に早いが、本当に最低限のテストを行うだけ(SWARM_DESCRIPTIONの構文やinputデータのチェック)。
ほとんどのユースケースmediumが最適。
もし相当の時間をかけれるならlargeにすると、より良いモデルを選定できるかも。

以下Documentの原文です。

the swarmSize setting controls how exhaustive the swarm should be. The three possible settings are “small”, “medium”, and “large”. A “small” swarm runs very fast and is really just a small test to make sure you don’t have any syntax errors in your description or input data files. It evaluates only one possible model and only on the first 100 records of the dataset. For most cases, a “medium” swarm is the best choice. If you’re willing to tradeoff extra swarming time for a possibly better model, you can set this to “large”.

Guide to Running Swarms — NuPIC 1.0.3 documentation

スウォーミング実行

スウォーミングを実行するスクリプトです。

# -*- coding: utf-8 -*-

import os
import pprint as pp

from nupic.swarming import permutations_runner
from swarm_description import SWARM_DESCRIPTION



def swarm(output_dir):
  '''
  Run swarming.

  Args:
    output_dir: a directory name for swarm results.
  '''

  # スウォーム結果を保存するディレクトリを作成。
  swarm_dir = os.path.abspath(output_dir)
  if not os.path.exists(swarm_dir):
    os.mkdir(swarm_dir)

  # 実行
  # 戻り値は output_dir/model_0/model_params.py にあるディクショナリが帰ってくる。
  #
  # maxWorkers: worker数、実行環境によって変えましょう。
  # overwrite: Falseの場合、すでにファイルが存在するとエラーが出る。
  return permutations_runner.runWithConfig(
    SWARM_DESCRIPTION,
    {"maxWorkers": 4, "overwrite": True},
    outputLabel = "sine",
    outDir = swarm_dir,
    permWorkDir = swarm_dir
  )



if __name__ == "__main__":

  result = swarm("swarm")
  print("Swarming results:")
  pp.pprint(result)

コード全体は以下。
github.com



f:id:kijnmb-1110:20171231231233p:plain