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

Database #6

Merged
merged 37 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
eb8c67a
Baker working draft; select tested also
Zbysekz May 25, 2020
344cab2
continue
Zbysekz May 26, 2020
e69436f
split to bakerReader; now possible to read some data
Zbysekz May 27, 2020
a5be58e
iteration jump works
Zbysekz May 27, 2020
e48f567
Proximal synapses working
Zbysekz May 31, 2020
822b1b0
Working on baking distal synapses
Zbysekz Jun 4, 2020
bddac6e
Multithreaded
Zbysekz Jun 4, 2020
8d61468
Without multithreading accesses to DB it is slightly faster
Zbysekz Jun 4, 2020
253d659
WIP
Zbysekz Jun 4, 2020
1a01d28
Baker thread pool working
Zbysekz Jun 11, 2020
b80eb90
HTm update sorted out; proximal/distal synapses refresh
Zbysekz Jun 11, 2020
10bfc43
Dash vis draft
Zbysekz Jun 11, 2020
6d4ea63
dashVis draft
Zbysekz Jun 11, 2020
7008469
Working dash
Zbysekz Jun 11, 2020
76a9f1d
plots corrected; baking lots of streams
Jun 12, 2020
4a0cf18
Images moved to separate folder
Zbysekz Jun 14, 2020
8e15273
Image scaled
Zbysekz Jun 14, 2020
cf2f4d4
Bugfix for proximal synapses; plots are colorful now
Zbysekz Jun 14, 2020
7e7104e
Taking predicition for t+1
Zbysekz Jun 14, 2020
810f875
Moved dash files into separate folder
Jun 17, 2020
44862f2
Setup installs pandaBaker instead of pandaComm
Jun 17, 2020
1aa5fc2
Max iteration; general improvements
Jun 18, 2020
463290d
Problem with path linux/windows solved
Zbysekz Jun 18, 2020
c5115b4
#3 Distal synapses working; focus bugfix;
Zbysekz Jun 18, 2020
3522895
Capture feature
Zbysekz Jun 18, 2020
368bae4
Gitignore for generated images
Zbysekz Jun 21, 2020
44a335f
layouts configurations; DASH run from pandaVis
Jun 21, 2020
db4a4ec
Merge branch 'database' of https://github.com/htm-community/HTMpandaV…
Jun 21, 2020
8cf60f4
Corrected behavior of "show prediction correctness" feature;dashVis move
Zbysekz Jun 21, 2020
32adf3d
minor change
Zbysekz Jun 21, 2020
5bab1e9
Update README.md
Zbysekz Jun 22, 2020
a3816cd
Update README.md
Zbysekz Jun 22, 2020
61cb172
doc modified
Jun 22, 2020
8f776ff
Merge branch 'database' of https://github.com/htm-community/HTMpandaV…
Jun 22, 2020
782f4cc
Update README.md
Zbysekz Jun 22, 2020
c5e8175
Update README.md
Zbysekz Jun 22, 2020
8cfd849
Update README.md
Zbysekz Jun 22, 2020
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ PandaVis/guiValues.ini
build/*
dist/*
*.egg-info/*
*.log
PandaVis/screenshots/*
HotgymExample/bakedDatabase/*/*.dump
HotgymExample/bakedDatabase/*.db
File renamed without changes.
147 changes: 88 additions & 59 deletions HotgymExample/hotgym.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import datetime
import os
import numpy as np
import random
import math

# Panda vis
from PandaVis.pandaComm.server import PandaServer
from PandaVis.pandaComm.dataExchange import ServerData, dataHTMObject, dataLayer, dataInput
from pandaBaker.pandaBaker import PandaBaker
from pandaBaker.pandaBaker import cLayer, cInput, cDataStream


from htm.bindings.sdr import SDR, Metrics
from htm.encoders.rdse import RDSE, RDSE_Parameters
Expand All @@ -23,6 +23,8 @@
_EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__))
_INPUT_FILE_PATH = os.path.join(_EXAMPLE_DIR, "gymdata.csv")

BAKE_DATABASE_FILE_PATH = os.path.join(os.getcwd(),'bakedDatabase','hotgym.db')

default_parameters = {
# there are 2 (3) encoders: "value" (RDSE) & "time" (DateTime weekend, timeOfDay)
'enc': {
Expand Down Expand Up @@ -57,7 +59,7 @@
}
}

pandaServer = PandaServer()
pandaBaker = PandaBaker(BAKE_DATABASE_FILE_PATH)

def main(parameters=default_parameters, argv=None, verbose=True):
if verbose:
Expand Down Expand Up @@ -133,12 +135,18 @@ def main(parameters=default_parameters, argv=None, verbose=True):
predictor = Predictor(steps=[1, 5], alpha=parameters["predictor"]['sdrc_alpha'])
predictor_resolution = 1

BuildPandaSystem(sp,tm, parameters["enc"]["value"]["size"],dateEncoder.size)

# Iterate through every datum in the dataset, record the inputs & outputs.
inputs = []
anomaly = []
anomalyProb = []
predictions = {1: [], 5: []}
iterationNo = 0

dateBits_last = None
consBits_last = None

for count, record in enumerate(records):

# Convert date string into Python date object.
Expand Down Expand Up @@ -168,45 +176,10 @@ def main(parameters=default_parameters, argv=None, verbose=True):
tm.activateDendrites(True)
predictiveCellsSDR = tm.getPredictiveCells()

pandaServer.currentIteration = iterationNo # update server's iteration number
# do not update if we are running GOTO iteration command
if (not pandaServer.cmdGotoIteration or (
pandaServer.cmdGotoIteration and pandaServer.gotoIteration == pandaServer.currentIteration)):
# ------------------HTMpandaVis----------------------
# fill up values
serverData.iterationNo = pandaServer.currentIteration

serverData.HTMObjects["HTM1"].inputs["SL_Consumption"].stringValue = "consumption: {:.2f}".format(consumption)
serverData.HTMObjects["HTM1"].inputs["SL_Consumption"].bits = consumptionBits.sparse
serverData.HTMObjects["HTM1"].inputs["SL_Consumption"].count = consumptionBits.size

serverData.HTMObjects["HTM1"].inputs["SL_TimeOfDay"].stringValue = record[0]
serverData.HTMObjects["HTM1"].inputs["SL_TimeOfDay"].bits = dateBits.sparse
serverData.HTMObjects["HTM1"].inputs["SL_TimeOfDay"].count = dateBits.size

serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeColumns = activeColumns.sparse
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].winnerCells = tm.getWinnerCells().sparse
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].predictiveCells = predictiveCellsSDR.sparse

pandaServer.serverData = serverData

pandaServer.spatialPoolers["HTM1"] = sp
pandaServer.temporalMemories["HTM1"] = tm
pandaServer.NewStateDataReady()

print("One step finished")
pandaServer.BlockExecution()
print("Proceeding one step...")

# ------------------HTMpandaVis----------------------

tm.activateCells(activeColumns, True)

tm_info.addData(tm.getActiveCells().flatten())

activeCells = tm.getActiveCells()
print("ACTIVE" + str(len(activeCells.sparse)))

# Predict what will happen, and then train the predictor based on what just happened.
pdf = predictor.infer(tm.getActiveCells())
for n in (1, 5):
Expand All @@ -217,15 +190,63 @@ def main(parameters=default_parameters, argv=None, verbose=True):

rawAnomaly = Anomaly.calculateRawAnomaly(activeColumns,
tm.cellsToColumns(predictiveCellsSDR))
print("rawAnomaly:" + str(rawAnomaly))

anomalyLikelihood = anomaly_history.anomalyProbability(consumption, rawAnomaly) # need to use different calculation as we are not calling sp.compute(..)
anomaly.append(rawAnomaly)
anomalyProb.append(anomalyLikelihood)

predictor.learn(count, tm.getActiveCells(), int(consumption / predictor_resolution))

# ------------------HTMpandaVis----------------------
# fill up values

pandaBaker.inputs["Consumption"].stringValue = "consumption: {:.2f}".format(consumption)
pandaBaker.inputs["Consumption"].bits = consumptionBits.sparse

pandaBaker.inputs["TimeOfDay"].stringValue = record[0]
pandaBaker.inputs["TimeOfDay"].bits = dateBits.sparse

pandaBaker.layers["Layer1"].activeColumns = activeColumns.sparse
pandaBaker.layers["Layer1"].winnerCells = tm.getWinnerCells().sparse
pandaBaker.layers["Layer1"].predictiveCells = predictiveCellsSDR.sparse
pandaBaker.layers["Layer1"].activeCells = tm.getActiveCells().sparse

# customizable datastreams to be show on the DASH PLOTS
pandaBaker.dataStreams["rawAnomaly"].value = rawAnomaly
pandaBaker.dataStreams["powerConsumption"].value = consumption
pandaBaker.dataStreams["numberOfWinnerCells"].value = len(tm.getWinnerCells().sparse)
pandaBaker.dataStreams["numberOfPredictiveCells"].value = len(predictiveCellsSDR.sparse)
pandaBaker.dataStreams["consumptionInput_sparsity"].value = consumptionBits.getSparsity()
pandaBaker.dataStreams["dateInput_sparsity"].value = dateBits.getSparsity()
pandaBaker.dataStreams["consumptionInput_overlap_with_prev_step"].value = 0 if consBits_last is None else consumptionBits.getOverlap(consBits_last)
consBits_last = consumptionBits
pandaBaker.dataStreams["dateInput_overlap_with_prev_step"].value = 0 if dateBits_last is None else dateBits.getOverlap(dateBits_last)
dateBits_last = dateBits

pandaBaker.dataStreams["Layer1_SP_overlap_metric"].value = sp_info.overlap.overlap
pandaBaker.dataStreams["Layer1_TM_overlap_metric"].value = sp_info.overlap.overlap
pandaBaker.dataStreams["Layer1_SP_activation_frequency"].value = sp_info.activationFrequency.mean()
pandaBaker.dataStreams["Layer1_TM_activation_frequency"].value = tm_info.activationFrequency.mean()
pandaBaker.dataStreams["Layer1_SP_entropy"].value = sp_info.activationFrequency.mean()
pandaBaker.dataStreams["Layer1_TM_entropy"].value = tm_info.activationFrequency.mean()


pandaBaker.StoreIteration(iterationNo)

print("ITERATION: "+str(iterationNo))

# ------------------HTMpandaVis----------------------



iterationNo = iterationNo + 1

#pandaBaker.CommitBatch()
if iterationNo == 1000:
break

pandaBaker.CommitBatch()

# Print information & statistics about the state of the HTM.
print("Encoded Input", enc_info)
print("")
Expand Down Expand Up @@ -253,8 +274,11 @@ def main(parameters=default_parameters, argv=None, verbose=True):
accuracy[n] += (inp - val) ** 2
accuracy_samples[n] += 1
for n in sorted(predictions):
accuracy[n] = (accuracy[n] / accuracy_samples[n]) ** .5
print("Predictive Error (RMS)", n, "steps ahead:", accuracy[n])
if accuracy_samples[n]!=0:
accuracy[n] = (accuracy[n] / accuracy_samples[n]) ** .5
print("Predictive Error (RMS)", n, "steps ahead:", accuracy[n])
else:
print("Unable to calculate RMS error!")

# Show info about the anomaly (mean & std)
print("Anomaly Mean", np.mean(anomaly))
Expand Down Expand Up @@ -290,32 +314,37 @@ def main(parameters=default_parameters, argv=None, verbose=True):
return -accuracy[5]


def BuildPandaSystem():
global serverData
serverData = ServerData()
serverData.HTMObjects["HTM1"] = dataHTMObject()
serverData.HTMObjects["HTM1"].inputs["SL_Consumption"] = dataInput()
serverData.HTMObjects["HTM1"].inputs["SL_TimeOfDay"] = dataInput()
#with this method, the structure for visualisation is defined
def BuildPandaSystem(sp,tm,consumptionBits_size,dateBits_size):

serverData.HTMObjects["HTM1"].layers["SensoryLayer"] = dataLayer(
default_parameters["sp"]["columnCount"],
default_parameters["tm"]["cellsPerColumn"],
)
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].proximalInputs = [
"SL_Consumption",
"SL_TimeOfDay",
#we have two inputs connected to proximal synapses of Layer1
pandaBaker.inputs["Consumption"] = cInput(consumptionBits_size)
pandaBaker.inputs["TimeOfDay"] = cInput(dateBits_size)

pandaBaker.layers["Layer1"] = cLayer(sp,tm) # Layer1 has Spatial Pooler & Temporal Memory
pandaBaker.layers["Layer1"].proximalInputs = [
"Consumption",
"TimeOfDay",
]

#data for dash plots
streams = ["rawAnomaly","powerConsumption","numberOfWinnerCells","numberOfPredictiveCells",
"consumptionInput_sparsity","dateInput_sparsity","consumptionInput_overlap_with_prev_step",
"dateInput_overlap_with_prev_step","Layer1_SP_overlap_metric","Layer1_TM_overlap_metric",
"Layer1_SP_activation_frequency","Layer1_TM_activation_frequency","Layer1_SP_entropy",
"Layer1_TM_entropy"
]

pandaBaker.dataStreams = dict((name,cDataStream()) for name in streams)# create dicts for more comfortable code
#could be also written like: pandaBaker.dataStreams["myStreamName"] = cDataStream()

pandaBaker.PrepareDatabase()

if __name__ == "__main__":
try:
pandaServer.Start()
BuildPandaSystem()

#while True: # run infinitely
main()

except KeyboardInterrupt:
print("Keyboard interrupt")
pandaServer.MainThreadQuitted()
print("Script finished")
Empty file added HotgymExample/logs/dummy.txt
Empty file.
Loading