【HTM 算法】第一个例子 Online Prediction Framework (OPF)

叁歲伎倆 2023-05-30 09:09 165阅读 0赞

HTM Python

安装好 NuPIC 后,第一个例子。安装教程 前往
官网地址:http://nupic.docs.numenta.org/1.0.5/quick-start/opf.html
总共三个文件,其相对路径关系如图所示:

  • opf

    • data

      • gymdata.csv
    • examples

      • test.py
    • params

      • model.yaml

Python 代码

新建一个py文件,复制官网文档 该例 代码如下:

  1. import csv
  2. import datetime
  3. import os
  4. import yaml
  5. from itertools import islice
  6. from nupic.frameworks.opf.model_factory import ModelFactory
  7. _NUM_RECORDS = 3000
  8. _EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__))
  9. _INPUT_FILE_PATH = os.path.join(_EXAMPLE_DIR, os.pardir, "data", "gymdata.csv")
  10. _PARAMS_PATH = os.path.join(_EXAMPLE_DIR, os.pardir, "params", "model.yaml")
  11. # create model according to the file of params/model.yaml
  12. def createModel():
  13. with open(_PARAMS_PATH, "r") as f:
  14. modelParams = yaml.safe_load(f)
  15. return ModelFactory.create(modelParams)
  16. # run Hotgym.
  17. def runHotgym(numRecords):
  18. model = createModel()
  19. model.enableInference({ "predictedField": "consumption"})
  20. # open the csv file as fin
  21. with open(_INPUT_FILE_PATH) as fin:
  22. reader = csv.reader(fin)
  23. # skip three line useless data
  24. headers = reader.next()
  25. reader.next()
  26. reader.next()
  27. results = []
  28. # for each line
  29. for record in islice(reader, numRecords):
  30. # Create a dictionary with field names as keys, row values as values.
  31. modelInput = dict(zip(headers, record))
  32. # Convert string consumption to float value.
  33. modelInput["consumption"] = float(modelInput["consumption"])
  34. # Convert timestamp string to Python datetime.
  35. modelInput["timestamp"] = datetime.datetime.strptime(
  36. modelInput["timestamp"], "%m/%d/%y %H:%M")
  37. # Push the data into the model and get back results.
  38. result = model.run(modelInput)
  39. bestPredictions = result.inferences["multiStepBestPredictions"]
  40. allPredictions = result.inferences["multiStepPredictions"]
  41. # Confidence values are keyed by prediction value in multiStepPredictions.
  42. oneStep = bestPredictions[1]
  43. oneStepConfidence = allPredictions[1][oneStep]
  44. fiveStep = bestPredictions[5]
  45. fiveStepConfidence = allPredictions[5][fiveStep]
  46. result = (oneStep, oneStepConfidence * 100,
  47. fiveStep, fiveStepConfidence * 100)
  48. print "1-step: {:16} ({:4.4}%)\t 5-step: {:16} ({:4.4}%)".format(*result)
  49. results.append(result)
  50. return results
  51. if __name__ == "__main__":
  52. runHotgym(_NUM_RECORDS)

添加一个yaml文件

需要注意:如果不修改上面的代码,则需要在该py文件的上级目录下新建文件夹,起名为 params ,并且在此params目录下新建一个yaml文件,起名为 model.yaml ,填写内容如下:

  1. model: HTMPrediction
  2. version: 1
  3. aggregationInfo:
  4. fields:
  5. - [consumption, mean]
  6. microseconds: 0
  7. milliseconds: 0
  8. minutes: 0
  9. months: 0
  10. seconds: 0
  11. hours: 1
  12. days: 0
  13. weeks: 0
  14. years: 0
  15. predictAheadTime: null
  16. modelParams:
  17. inferenceType: TemporalMultiStep
  18. sensorParams:
  19. verbosity: 0
  20. encoders:
  21. consumption:
  22. fieldname: consumption
  23. name: consumption
  24. resolution: 0.88
  25. seed: 1
  26. type: RandomDistributedScalarEncoder
  27. timestamp_timeOfDay:
  28. fieldname: timestamp
  29. name: timestamp_timeOfDay
  30. timeOfDay: [21, 1]
  31. type: DateEncoder
  32. timestamp_weekend:
  33. fieldname: timestamp
  34. name: timestamp_weekend
  35. type: DateEncoder
  36. weekend: 21
  37. sensorAutoReset: null
  38. spEnable: true
  39. spParams:
  40. inputWidth: 946
  41. columnCount: 2048
  42. spVerbosity: 0
  43. spatialImp: cpp
  44. globalInhibition: 1
  45. localAreaDensity: -1.0
  46. numActiveColumnsPerInhArea: 40
  47. seed: 1956
  48. potentialPct: 0.85
  49. synPermConnected: 0.1
  50. synPermActiveInc: 0.04
  51. synPermInactiveDec: 0.005
  52. boostStrength: 3.0
  53. tmEnable: true
  54. tmParams:
  55. verbosity: 0
  56. columnCount: 2048
  57. cellsPerColumn: 32
  58. inputWidth: 2048
  59. seed: 1960
  60. temporalImp: cpp
  61. newSynapseCount: 20
  62. initialPerm: 0.21
  63. permanenceInc: 0.1
  64. permanenceDec: 0.1
  65. maxAge: 0
  66. globalDecay: 0.0
  67. maxSynapsesPerSegment: 32
  68. maxSegmentsPerCell: 128
  69. minThreshold: 12
  70. activationThreshold: 16
  71. outputType: normal
  72. pamLength: 1
  73. clParams:
  74. verbosity: 0
  75. regionName: SDRClassifierRegion
  76. alpha: 0.1
  77. steps: '1,5'
  78. maxCategoryCount: 1000
  79. implementation: cpp
  80. trainSPNetOnlyIfRequested: false

添加一个 csv 文件

需要注意:如果不修改上面的代码,则需要在该py文件的上级目录下新建文件夹,起名为 data ,并且在此 data目录下新建一个csv文件,起名为 gymdata.csv ,填写内容如下:

  1. timestamp,consumption
  2. datetime,float
  3. T,
  4. 7/2/10 0:00,21.2
  5. 7/2/10 1:00,16.4
  6. 7/2/10 2:00,4.7
  7. 7/2/10 3:00,4.7
  8. 7/2/10 4:00,4.6
  9. 7/2/10 5:00,23.5
  10. 7/2/10 6:00,47.5
  11. 7/2/10 7:00,45.4
  12. 7/2/10 8:00,46.1
  13. 7/2/10 9:00,41.5
  14. 7/2/10 10:00,43.4
  15. 7/2/10 11:00,43.8
  16. 7/2/10 12:00,37.8
  17. 7/2/10 13:00,36.6
  18. 7/2/10 14:00,35.7
  19. 7/2/10 15:00,38.9
  20. 7/2/10 16:00,36.2

运行

在该例中py文件所在目录下输入命令如下(根据实际文件名更改):

  1. $ sudo python test.py

效果展示

运行后可以看到控制台输出内容如下:

  1. 1-step: 21.2 (100.0%) 5-step: 21.2 (100.0%)
  2. 1-step: 16.4 (99.8%) 5-step: 16.4 (99.8%)
  3. 1-step: 4.7 (99.6%) 5-step: 4.7 (99.6%)
  4. 1-step: 4.7 (99.6%) 5-step: 4.7 (99.6%)
  5. 1-step: 4.6 (99.4%) 5-step: 4.6 (99.4%)
  6. 1-step: 23.5 (99.4%) 5-step: 23.5 (99.4%)
  7. 1-step: 47.5 (99.21%) 5-step: 47.5 (99.21%)
  8. 1-step: 45.4 (99.06%) 5-step: 45.4 (99.06%)
  9. 1-step: 46.1 (98.87%) 5-step: 46.1 (98.87%)
  10. 1-step: 41.5 (98.87%) 5-step: 41.5 (98.87%)
  11. 1-step: 43.4 (98.68%) 5-step: 43.4 (98.68%)
  12. 1-step: 43.8 (33.89%) 5-step: 43.8 (90.87%)
  13. 1-step: 43.8 (46.41%) 5-step: 37.8 (87.24%)
  14. 1-step: 36.6 (78.89%) 5-step: 45.61 (46.18%)
  15. 1-step: 45.61 (40.95%) 5-step: 47.5 (46.79%)
  16. 1-step: 45.61 (87.64%) 5-step: 43.8 (95.56%)
  17. 1-step: 38.9 (50.13%) 5-step: 43.8 (16.84%)

常见错误与解决方法

  1. 保证 python 版本为 2.x
  2. 如果提示错误如下:

    1. Traceback (most recent call last):
    2. File "test.py", line 4, in <module>
    3. import yaml
    4. ImportError: No module named yaml

    可能是没有安装这个yaml包,但是更有可能是需要添加sudo ,比如说输入 python test.py 提示以上错误,可以考虑使用 sudo python test.py

略加修改与测试

读python源码,发现只输出step1 和step5,所以我想输出step3。

  1. 修改 params/model.yaml 文件,查找其中的 1,5的位置,改成1,3,5

    修改后相应内容如下:

    1. clParams:
    2. verbosity: 0
    3. regionName: SDRClassifierRegion
    4. alpha: 0.1
    5. steps: '1,3,5'
    6. maxCategoryCount: 1000
    7. implementation: cpp
  2. 修改python代码,找到oneStep 与 fiveStep,类似地,定义一个变量threeStep,基本过程也是一样,需要修改数组中下标,输出也是需要进行类似修改。总而言之代码如下:

    1. oneStep = bestPredictions[1]
    2. oneStepConfidence = allPredictions[1][oneStep]
    3. treeStep = bestPredictions[3]
    4. treeStepConfidence = allPredictions[3][treeStep]
    5. fiveStep = bestPredictions[5]
    6. fiveStepConfidence = allPredictions[5][fiveStep]
    7. result = (oneStep, oneStepConfidence * 100,
    8. treeStep, treeStepConfidence * 100,
    9. fiveStep, fiveStepConfidence * 100)
    10. print "1-step: {:16} ({:4.4}%)\t 3-step: {:16} ({:4.4}%)\t 5-step: {:16} ({:4.4}%)".format(*result)

    测试效果如下:

    在这里插入图片描述

    分析部分将会记录在另一篇博客。

Smileyan

2019年11月8日 20:58

发表评论

表情:
评论列表 (有 0 条评论,165人围观)

还没有评论,来说两句吧...

相关阅读

    相关 第一Online项目风波

      刚才接到电话说今天上线的项目出现了问题,进入安全设置后没有进入相应的页面,并且有一个安全问题的下拉列表也没有正常显示。回想了一下代码,发现原因是之前在数据库中添加了新的字段