-
Notifications
You must be signed in to change notification settings - Fork 73
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
Hotgym predictor, anomaly tests #675
base: master
Are you sure you want to change the base?
Changes from 3 commits
5585038
d885653
432f98a
9f015f1
97023a8
ae192bd
0987941
0e1cc14
6c7678e
285490b
06f9701
4bed29d
f80a765
865f7e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ Example usage: | |
TODO | ||
)"); | ||
|
||
py::enum_<TemporalMemory::ANMode>(m, "ANMode") | ||
py::enum_<TemporalMemory::ANMode>(m, "ANMode") //TODO currently htm.bindings.algorithms.ANMode, make ANMode part of algorithms.TemporalMemory | ||
.value("DISABLED", TemporalMemory::ANMode::DISABLED) | ||
.value("RAW", TemporalMemory::ANMode::RAW) | ||
.value("LIKELIHOOD", TemporalMemory::ANMode::LIKELIHOOD) | ||
|
@@ -372,6 +372,8 @@ R"()"); | |
"Anomaly score updated with each TM::compute() call. " | ||
); | ||
|
||
py_HTM.def("setAnomalyMode", &HTM_t::setAnomalyMode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defined, compiles, but not found in py test?! |
||
|
||
py_HTM.def("__str__", | ||
[](HTM_t &self) { | ||
std::stringstream buf; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,11 +171,30 @@ def testPredictiveCells(self): | |
_print("activeCols:"+str(len(activeColumnsA.sparse))) | ||
_print("activeCells:"+str(len(tm.getActiveCells().sparse))) | ||
_print("predictiveCells:"+str(len(predictiveCellsSDR.sparse))) | ||
|
||
|
||
def testAnomaly(self): | ||
"""test convergence of TM and quality of anomaly methods""" | ||
from htm.bindings.algorithms import ANMode | ||
tm = TM(columnDimensions=[2048], anomalyMode=ANMode.RAW) #FIXME likelihood is always 0.5?! .LIKELIHOOD) | ||
|
||
modes = [ANMode.RAW, ANMode.LIKELIHOOD, ANMode.LOGLIKELIHOOD] | ||
for mod in modes: #this block test convergence of TM and anomaly score for select mode | ||
#FIXME why not visible from bidngings? tm.setAnomalyMode(mod) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#print("testing {}".format(mod)) | ||
inp = SDR([2048]).randomize(0.05) #starting SDR with 5% bits ON | ||
|
||
#learn TM a bit, anomaly should be low | ||
for _ in range(200): | ||
inp.addNoise(0.02) #change 2% bits -> 98% overlap => anomaly should ideally be 2% | ||
tm.compute(inp, learn=True) | ||
self.assertLess(tm.anomaly, 0.08) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new test that TM actually learns and anomalies converge. |
||
|
||
|
||
|
||
def _print(txt): | ||
if debugPrint: | ||
print(txt) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ class TemporalMemory : public Serializable | |
* cells and winner cells of these external inputs must be given to methods | ||
* TM.compute and TM.activateDendrites | ||
* | ||
* @param anomalyMode (optional, default `ANMode::RAW`)from enum ANMode, how is | ||
* @param anomalyMode (optional, default `TemporalMemory::ANMode::RAW`)from enum ANMode, how is | ||
* `TM.anomaly` computed. Options ANMode {DISABLED, RAW, LIKELIHOOD, LOGLIKELIHOOD} | ||
* | ||
*/ | ||
|
@@ -149,7 +149,7 @@ class TemporalMemory : public Serializable | |
SynapseIdx maxSynapsesPerSegment = 255, | ||
bool checkInputs = true, | ||
UInt externalPredictiveInputs = 0, | ||
ANMode anomalyMode = ANMode::RAW | ||
ANMode anomalyMode = TemporalMemory::ANMode::RAW | ||
); | ||
|
||
virtual void | ||
|
@@ -169,7 +169,7 @@ class TemporalMemory : public Serializable | |
SynapseIdx maxSynapsesPerSegment = 255, | ||
bool checkInputs = true, | ||
UInt externalPredictiveInputs = 0, | ||
ANMode anomalyMode = ANMode::RAW | ||
ANMode anomalyMode = TemporalMemory::ANMode::RAW | ||
); | ||
|
||
virtual ~TemporalMemory(); | ||
|
@@ -677,13 +677,17 @@ class TemporalMemory : public Serializable | |
Real anomaly_ = 0.5f; //default value | ||
ANMode mode_ = ANMode::RAW; | ||
AnomalyLikelihood anomalyLikelihood_; //TODO provide default/customizable params here | ||
}; | ||
void reset() { | ||
anomaly_ = 0.5f; | ||
mode_ = ANMode::RAW; | ||
//TODO provide anomalyLikelihood_.reset(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when calling |
||
} | ||
} tmAnomaly_; | ||
|
||
public: | ||
Connections connections; | ||
const UInt &externalPredictiveInputs = externalPredictiveInputs_; | ||
|
||
anomaly_tm tmAnomaly_; | ||
/* | ||
* anomaly score computed for the current inputs | ||
* (auto-updates after each call to TM::compute()) | ||
|
@@ -694,6 +698,14 @@ class TemporalMemory : public Serializable | |
const Real &anomaly = tmAnomaly_.anomaly_; //this is position dependant, the struct anomaly_tm must be defined before this use, | ||
// otherwise this surprisingly compiles, but a call to `tmAnomaly_.anomaly` segfaults! | ||
|
||
/** | ||
* set new mode for TM.anomaly computation. | ||
* This will reset existing anomaly. | ||
* | ||
* @param mode: Options TemporalMemory::ANMode {DISABLED, RAW, LIKELIHOOD, LOGLIKELIHOOD} | ||
*/ | ||
void setAnomalyMode(ANMode mode); | ||
|
||
}; | ||
|
||
} // namespace htm | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, how would it be possible to make enum
ANMode
exposed as a part ofhtm.bindings.algorithms.TemporalMemory.ANMode
, and not the current..bindings.algorithms.ANMode
?