-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm6.py
168 lines (128 loc) · 5.51 KB
/
tm6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2015-2016, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
#We have adapted network_api_demo.py for our own research.
#We are only using the SensorRegion from the Network API. Encoded data is being fed to the latest
#spatial pooler, temporal memory, and SDR classifier algorithms.
#The sample data is our own and is generated from our SketchUp ruby extension.
import copy
import csv
import json
import os
import numpy
from itertools import izip as zip, count
from nupic.research.temporal_memory import TemporalMemory as TM
from pkg_resources import resource_filename
from nupic.engine import Network
from nupic.encoders import MultiEncoder, ScalarEncoder
from nupic.data.file_record_stream import FileRecordStream
_VERBOSITY = 0 # how chatty the demo should be
_SEED = 1956 # the random seed used throughout
_INPUT_FILE_PATH = resource_filename(
"nupic.datafiles", "extra/tm6.csv"
)
_OUTPUT_PATH = "tm6out.csv"
_NUM_RECORDS = 9
tm = TM(columnDimensions = (10225,),
cellsPerColumn=18,
minThreshold=8,
maxNewSynapseCount=20,
permanenceIncrement=0.1,
activationThreshold=8,
seed=_SEED,
)
def createEncoder():
volume_encoder = ScalarEncoder(7, 0.0, 70.0, n=200, name="volume", forced=True)
floorheight_encoder = ScalarEncoder(1, 0.0, 70.0, n=25, name="floorheight", forced=True)
diagCoorA_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorA")
diagCoorB_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorB")
diagCoorC_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorC")
diagCoorD_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorD")
diagCoorE_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorE")
diagCoorF_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorF")
diagCoorG_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorG")
diagCoorH_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorH")
diagCoorI_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorI")
diagCoorJ_encoder = ScalarEncoder(105, 0.0, 200.0, n=1000, name="diagCoorJ")
global encoder
encoder = MultiEncoder()
encoder.addEncoder("volume", volume_encoder)
encoder.addEncoder("floorheight", floorheight_encoder)
encoder.addEncoder("diagCoorA", diagCoorA_encoder)
encoder.addEncoder("diagCoorB", diagCoorB_encoder)
encoder.addEncoder("diagCoorC", diagCoorC_encoder)
encoder.addEncoder("diagCoorD", diagCoorD_encoder)
encoder.addEncoder("diagCoorE", diagCoorE_encoder)
encoder.addEncoder("diagCoorF", diagCoorF_encoder)
encoder.addEncoder("diagCoorG", diagCoorG_encoder)
encoder.addEncoder("diagCoorH", diagCoorH_encoder)
encoder.addEncoder("diagCoorI", diagCoorI_encoder)
encoder.addEncoder("diagCoorJ", diagCoorJ_encoder)
return encoder
def createNetwork(dataSource):
network = Network()
network.addRegion("sensor", "py.RecordSensor",
json.dumps({"verbosity": _VERBOSITY}))
sensor = network.regions["sensor"].getSelf()
sensor.encoder = createEncoder()
sensor.dataSource = dataSource
return network
def runNetwork(network, writer):
sensorRegion = network.regions["sensor"]
listDataOut = []
for h in xrange(_NUM_RECORDS):
network.run(1)
testGV = sensorRegion.getOutputData("dataOut").nonzero()[0]
listDataOut.append(testGV)
src = sensorRegion.getOutputData("sourceOut")
if h == 3:
printList = []
for hSub in xrange(len(src)):
if src[hSub] != 0.0:
printList.append(src[hSub])
print ("should predict: " + str(printList))
listCount = len(listDataOut)
for j in xrange(listCount):
num = int(j)
mod = num % 2
if mod == 0:
if j < listCount-2:
for k in xrange(100):
tm.compute(listDataOut[j], learn = True)
tm.compute(listDataOut[j+1], learn = True)
tm.reset()
tm.compute(listDataOut[listCount-1], learn=False)
predictedColumnIndices = [tm.columnForCell(i) for i in tm.getPredictiveCells()]
predArray = numpy.zeros(tm.numberOfColumns(), dtype="int64")
predArray[list(predictedColumnIndices)] = 1
testDecodeAlt = encoder.topDownCompute(predArray)
printListPred = []
for l in xrange(len(testDecodeAlt)):
if testDecodeAlt[l][0] != 0.0:
printListPred.append(round(testDecodeAlt[l][0],7))
print ("predictions: " + str(printListPred))
if __name__ == "__main__":
dataSource = FileRecordStream(streamID=_INPUT_FILE_PATH)
network = createNetwork(dataSource)
network.initialize()
outputPath = os.path.join(os.path.dirname(__file__), _OUTPUT_PATH)
with open(outputPath, "w") as outputFile:
writer = csv.writer(outputFile)
runNetwork(network, writer)