Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUP-2405: quick-start guide for the Network API #3557

Merged
merged 21 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
80bb517
NUP-2394: use YAML file for model params
Apr 4, 2017
54a9d10
NUP-2394: network API quick-start example (WIP)
Apr 4, 2017
69841ff
NUP-2394: run inference outside network
Apr 5, 2017
6956e0c
NUP-2394: save and check all complete-examples.py predictions
Apr 5, 2017
956f569
Merge branch 'master' of https://github.com/numenta/nupic into NUP-23…
Apr 5, 2017
fb95987
NUP-2394: use YAML params in "algo" code example
Apr 6, 2017
dc3cdbf
NUP-2394: update comments of YAML params based on feedback
Apr 6, 2017
a771b84
NUP-2394: scripts to compare predictions results between the 3 code e…
Apr 6, 2017
93160cf
NUP-2394: Run classification inside network. Details:
Apr 8, 2017
4ce9edc
NUP-2394: Show RMSE in plot titles
Apr 8, 2017
9c77a66
Merge branch 'master' of https://github.com/numenta/nupic into NUP-23…
Apr 14, 2017
21d82b1
Merge branch 'master' of https://github.com/numenta/nupic into NUP-23…
Apr 17, 2017
b727b69
Code review feedback:
Apr 17, 2017
dc72590
NUP-2394: Fix YAML with new CLA model name (HTMPrediction)
Apr 17, 2017
fac2380
NUP-2394: make model_params camel case for consistency and update cod…
Apr 18, 2017
3c205af
NUP-2394: re-order network creation logic:
Apr 18, 2017
d7e8593
NUP-2394: fix indentation
Apr 18, 2017
0466c73
NUP-2405: quick-start guide for the network API:
Apr 18, 2017
829db9d
NUP-2405: Fix reference to old modelParams in OPF example:
Apr 18, 2017
214c9bd
NUP-2405: address PR feedback
Apr 26, 2017
5fa9357
Merge branch 'master' of https://github.com/numenta/nupic into NUP-24…
Apr 26, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/examples/network/example-add-classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clParams = modelParams["clParams"]
network.addRegion("classifier", "py.SDRClassifierRegion", json.dumps(clParams))
1 change: 1 addition & 0 deletions docs/examples/network/example-add-sensor-region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
network.addRegion("sensor", "py.RecordSensor", '{}')
7 changes: 7 additions & 0 deletions docs/examples/network/example-add-sp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spParams = modelParams["spParams"]

# Make sure the SP input width matches the sensor output width.
spParams["inputWidth"] = sensorRegion.encoder.getWidth()

# Add SP region.
network.addRegion("SP", "py.SPRegion", json.dumps(spParams))
2 changes: 2 additions & 0 deletions docs/examples/network/example-add-tm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tmParams = modelParams["tmParams"]
network.addRegion("TM", "py.TPRegion", json.dumps(tmParams))
13 changes: 13 additions & 0 deletions docs/examples/network/example-create-encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from nupic.encoders import MultiEncoder

def createEncoder(encoderParams):
encoder = MultiEncoder()
encoder.addMultipleEncoders(encoderParams)
return encoder

# Use the same modelParams extracted from the YAML file earlier.
encoderParams = modelParams["sensorParams"]["encoders"]

# Add encoder to the sensor region.
sensorRegion = network.regions["sensor"].getSelf()
sensorRegion.encoder = createEncoder(encoderParams)
4 changes: 4 additions & 0 deletions docs/examples/network/example-create-network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from nupic.engine import Network

# A network that will hold the regions.
network = Network()
49 changes: 49 additions & 0 deletions docs/examples/network/example-create-regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json

# Add a sensor region, set its encoder and data source.
network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": 0}))

# Make sure the SP input width matches the sensor region output width.
model_params["spParams"]["inputWidth"] = sensorRegion.encoder.getWidth()

# Add the SP and TM regions.
network.addRegion("SP", "py.SPRegion", json.dumps(model_params["spParams"]))
network.addRegion("TM", "py.TPRegion", json.dumps(model_params["tmParams"]))

# Add the classifier region.
clName = "py.%s" % model_params[]
network.addRegion("classifier", , json.dumps(model_params["clParams"]))



# Add all links
createSensorToClassifierLinks(network, "sensor", "classifier")

# Link the sensor region to the SP region so that it can pass it data.
createDataOutLink(network, "sensor", "SP")

# Create feed-forward links between regions.
createFeedForwardLink(network, "SP", "TM")
createFeedForwardLink(network, "TM", "classifier")

# Propagate reset signals to SP and TM regions.
# Optional if you know that your sensor regions does not send resets.
createResetLink(network, "sensor", "SP")
createResetLink(network, "sensor", "TM")



# Set the data source to the sensor region
sensorRegion = network.regions["sensor"].getSelf()
sensorRegion.dataSource = dataSource

# Set the encoder to the sensor region
sensorRegion.encoder = createEncoder(model_params["sensorParams"]["encoders"])



# Make sure all objects are initialized.
network.initialize()



8 changes: 8 additions & 0 deletions docs/examples/network/example-data-source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from nupic.data.file_record_stream import FileRecordStream

_INPUT_FILE_PATH = "/path/to/your/data.csv"
dataSource = FileRecordStream(streamID=_INPUT_FILE_PATH)

# Add the data source to the sensor region.
sensorRegion = network.regions["sensor"].getSelf()
sensorRegion.dataSource = dataSource
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Enable learning for all regions.
network.regions["SP"].setParameter("learningMode", 1)
network.regions["TM"].setParameter("learningMode", 1)
network.regions["classifier"].setParameter("learningMode", 1)

# Enable inference for all regions.
network.regions["SP"].setParameter("inferenceMode", 1)
network.regions["TM"].setParameter("inferenceMode", 1)
network.regions["classifier"].setParameter("inferenceMode", 1)
20 changes: 20 additions & 0 deletions docs/examples/network/example-extract-results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def getPredictionResults(network, clRegionName):
"""Helper function to extract results for all prediction steps."""


classifierRegion = network.regions[clRegionName]
actualValues = classifierRegion.getOutputData("actualValues")
probabilities = classifierRegion.getOutputData("probabilities")
steps = classifierRegion.getSelf().stepsList

N = classifierRegion.getSelf().maxCategoryCount
results = {step: {} for step in steps}
for i in range(len(steps)):
# stepProbabilities are probabilities for this prediction step only.
stepProbabilities = probabilities[i * N:(i + 1) * N - 1]
mostLikelyCategoryIdx = stepProbabilities.argmax()
predictedValue = actualValues[mostLikelyCategoryIdx]
predictionConfidence = stepProbabilities[mostLikelyCategoryIdx]
results[steps[i]]["predictedValue"] = predictedValue
results[steps[i]]["predictionConfidence"] = predictionConfidence
return results
32 changes: 32 additions & 0 deletions docs/examples/network/example-link-all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def createDataOutLink(network, sensorRegionName, regionName):
"""Link sensor region to other region so that it can pass it data."""
network.link(sensorRegionName, regionName, "UniformLink", "",
srcOutput="dataOut", destInput="bottomUpIn")


def createFeedForwardLink(network, regionName1, regionName2):
"""Create a feed-forward link between 2 regions: regionName1 -> regionName2"""
network.link(regionName1, regionName2, "UniformLink", "",
srcOutput="bottomUpOut", destInput="bottomUpIn")


def createSensorToClassifierLinks(network, sensorRegionName, classifierRegionName):
"""Create required links from a sensor region to a classifier region."""
network.link(sensorRegionName, classifierRegionName, "UniformLink", "",
srcOutput="bucketIdxOut", destInput="bucketIdxIn")
network.link(sensorRegionName, classifierRegionName, "UniformLink", "",
srcOutput="actValueOut", destInput="actValueIn")
network.link(sensorRegionName, classifierRegionName, "UniformLink", "",
srcOutput="categoryOut", destInput="categoryIn")


# 1. Add data link between sensor and SP.
createDataOutLink(network, "sensor", "SP")

# 2. Add feed forward links.
createFeedForwardLink(network, "SP", "TM")
createFeedForwardLink(network, "TM", "classifier")

# 3. Add links between sensor and classifier.
createSensorToClassifierLinks(network, "sensor", "classifier")

6 changes: 6 additions & 0 deletions docs/examples/network/example-run-network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Make sure all objects are initialized.
network.initialize()

N = 1 # Run the network, N iterations at a time.
for iteration in range(0, numRecords, N):
network.run(N)
3 changes: 3 additions & 0 deletions docs/examples/network/example-set-predicted-field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
predictedIdx = dataSource.getFieldNames().index("consumption")

network.regions["sensor"].setParameter("predictedFieldIdx", predictedIdx)
6 changes: 6 additions & 0 deletions docs/examples/network/example-yaml-import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import yaml

_PARAMS_PATH = "/path/to/model.yaml"

with open(_PARAMS_PATH, "r") as f:
modelParams = yaml.safe_load(f)["modelParams"]
18 changes: 0 additions & 18 deletions docs/examples/opf/example-model-param-encoders.py

This file was deleted.

18 changes: 18 additions & 0 deletions docs/examples/opf/example-model-param-encoders.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# List of encoders and their parameters.
encoders:
consumption:
fieldname: consumption
name: consumption
resolution: 0.88
seed: 1
type: RandomDistributedScalarEncoder
timestamp_timeOfDay:
fieldname: timestamp
name: timestamp_timeOfDay
timeOfDay: [21, 1]
type: DateEncoder
timestamp_weekend:
fieldname: timestamp
name: timestamp_weekend
type: DateEncoder
weekend: 21
Loading