Skip to content

Commit

Permalink
Implementation Spatial Pooler/Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ich123 committed May 15, 2016
1 parent d1dc5bd commit 2c2a726
Show file tree
Hide file tree
Showing 28 changed files with 3,614 additions and 347 deletions.
68 changes: 53 additions & 15 deletions bin/res_viewer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
import os, sys
import getopt
import csv
import logging as log
log.basicConfig(format='%(message)s', level=log.DEBUG)

basedir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.abspath(os.path.join(basedir, '../lib'));
libtest = os.path.abspath(os.path.join(basedir, '../lib/test'));
Expand All @@ -10,22 +15,55 @@
import matplotlib.pylab as plt
from results import *

fname = sys.argv[1]
def load(fname): return Results.load(fname)



def metrics(res):
# res.print_all_metrics()
# print res.metrics
with open('metrics.csv', 'a') as f :
w = csv.writer(f)
i = 0
for name, item in sorted(res.metrics.items()) :
shape,wp = name.split('w')
nrows,ncols = shape.split('x')
if i == 0 :
w.writerow(["name","nrows","ncols","winp"] + item.keys())
i += 1
w.writerow( [name,nrows,ncols,wp] + item.values())


def plot(res):
for name in res.series.keys() :
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot( res.series[name]['ys'] )
plt.plot( res.series[name]['yhat'] )
plt.suptitle(name)
y = 0.998
for m in ['mape','nll', 'mae', 'rmse', 'r2'] :
metric = "%s: %.3f" % (str(m).upper(), res.metrics[name][m])
plt.text(0.998,y, metric, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes)
y -= 0.03
plt.tight_layout()

plt.show()

def parse_args(arguments):
opts = {}
try:
for arg,opt in getopt.getopt(arguments,'f:vm')[0] : opts[arg] = opt
return opts
except getopt.GetoptError:
print "wrong args"
sys.exit(2)


res = Results.load(fname)
for name in res.series.keys() :
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot( res.series[name]['ys'] )
plt.plot( res.series[name]['yhat'] )
plt.suptitle(name)
y = 0.998
for m in ['mape','nll', 'mae', 'rmse', 'r2'] :
metric = "%s: %.3f" % (str(m).upper(), res.metrics[name][m])
plt.text(0.998,y, metric, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes)
y -= 0.03
plt.tight_layout()

opts = parse_args(sys.argv[1:])
res = load(opts['-f'])

plt.show()
if '-v' in opts : plot(res)
if '-m' in opts : metrics(res)

12 changes: 11 additions & 1 deletion docs/TM_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ <h4 id="[-HOME--]=====--How-to-compute-like-a-brain-=====[-Programming--]">[ <a
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>!! NLL (negactive log likelihood score uses 1000 bins for calulation by default)</strong></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
Expand Down Expand Up @@ -1279,7 +1289,7 @@ <h4 id="[-HOME--]=====--How-to-compute-like-a-brain-=====[-Programming--]">[ <a
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">dt</span><span class="o">.</span><span class="n">tests</span><span class="p">[</span><span class="s1">&#39;5x300&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">tm</span><span class="o">.</span><span class="n">pooler</span><span class="o">.</span><span class="n">show_mem</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">dt</span><span class="o">.</span><span class="n">tests</span><span class="p">[</span><span class="s1">&#39;5x300&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">tm</span><span class="o">.</span><span class="n">memory</span><span class="o">.</span><span class="n">show_mem</span><span class="p">()</span>
</pre></div>

</div>
Expand Down
9 changes: 8 additions & 1 deletion docs/TM_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@
"dt.results.metrics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**!! NLL (negactive log likelihood score uses 1000 bins for calulation by default)**"
]
},
{
"cell_type": "code",
"execution_count": 7,
Expand Down Expand Up @@ -178,7 +185,7 @@
}
],
"source": [
"dt.tests['5x300'].tm.pooler.show_mem()"
"dt.tests['5x300'].tm.memory.show_mem()"
]
},
{
Expand Down
40 changes: 25 additions & 15 deletions docs/bbHTM.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="[-HOME--]=====--How-to-compute-like-a-brain-=====[-Programming--]">[ <a href="http://ifni.co">HOME</a> ]===== How to compute like a brain =====[ <a href="http://ifni.co/toc/programming">Programming</a> ]<a class="anchor-link" href="#[-HOME--]=====--How-to-compute-like-a-brain-=====[-Programming--]">&#182;</a></h4>
<h3 id="============--How-to-compute-like-a-brain-==========">============ How to compute like a brain ==========<a class="anchor-link" href="#============--How-to-compute-like-a-brain-==========">&#182;</a></h3><h4 id="==============[-HOME--]=====[-Programming--]=====[-Source-code-]=====">==============[ <a href="http://ifni.co">HOME</a> ]=====[ <a href="http://ifni.co/toc/programming">Programming</a> ]=====[ <a href="https://github.com/vsraptor/bbhtm">Source code</a> ]=====<a class="anchor-link" href="#==============[-HOME--]=====[-Programming--]=====[-Source-code-]=====">&#182;</a></h4><hr>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">matplotlib</span> inline
Expand Down Expand Up @@ -512,6 +513,9 @@ <h3 id="Properties-of-SDR">Properties of SDR<a class="anchor-link" href="#Proper
</ul>
<p>You should look at the paper to see numerical examples of reliability of those properties for different values of n,w,b and threshold. Suffice to say as long as the vectors are Large &gt; 1000, Sparse &lt; 5% and Distributed the properties hold pretty well.</p>
<p>My current implementation uses exclusively the UNION property of SDR.</p>
<p>The big benefit of the union-property is that it allow us to store multiple SDRs in fixed-length bit-string.
Standardly we would use some structure like array or hash to store multiple elements, but the properties of SDR make it possible to achieve similar result with fixed structure.
This is possible because as the SDR size grow linearly the possibility of not getting false-positive grows exponentially.</p>

</div>
</div>
Expand Down Expand Up @@ -1910,7 +1914,7 @@ <h4 id="Lets-try-it-!">Lets try it !<a class="anchor-link" href="#Lets-try-it-!"
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr>
<h3 id="Let's-TEST-it-!!"><a href="TM_test.html" target="_blank">Let's TEST it !!</a><a class="anchor-link" href="#Let's-TEST-it-!!">&#182;</a></h3><blockquote><p>Because this document became too large there is separate page on testing.. I have build simplified test framework : <a href='TM_test.html' target='_blank'>Try it out !</a>.</p>
<h3 id="Test-Framework"><a href="TM_test.html" target="_blank">Test Framework</a><a class="anchor-link" href="#Test-Framework">&#182;</a></h3><blockquote><p>Because this document became too large there is separate page on testing.. I have build simplified test framework : <a href='TM_test.html' target='_blank'>Try it out !</a>.</p>
</blockquote>
<hr>

Expand All @@ -1922,9 +1926,8 @@ <h3 id="Let's-TEST-it-!!"><a href="TM_test.html" target="_blank">Let's TEST it !
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Spatial-pooler-:-part-2">Spatial pooler : part 2<a class="anchor-link" href="#Spatial-pooler-:-part-2">&#182;</a></h3><hr>
<blockquote><p>todo implementation ...</p>
</blockquote>
<h3 id="Spatial-pooler/mapper:-part-2"><a href="spatial_mapper.html" target="_blank">Spatial pooler/mapper</a>: part 2<a class="anchor-link" href="#Spatial-pooler/mapper:-part-2">&#182;</a></h3><hr>
<p>You can read the details here : <a href='spatial_mapper.html' target='_blank'>Spatial pooler/mapper</a></p>

</div>
</div>
Expand Down Expand Up @@ -2002,15 +2005,22 @@ <h1 id="Conclusion">Conclusion<a class="anchor-link" href="#Conclusion">&#182;</
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<hr>
<h2 id="References">References<a class="anchor-link" href="#References">&#182;</a></h2><p><a href='bmap2D.html' target='_blank'>BMap2D</a></p>
<p><a href='TM_test.html' target='_blank'>Testing</a></p>
<p><a href='http://ifni.co/articles/programming/recursion' target='_blank'>PatternLang()</a></p>
<p><a href='http://numenta.com/assets/pdf/whitepapers/hierarchical-temporal-memory-cortical-learning-algorithm-0.2.1-en.pdf' target='_blank'>HTM theory</a></p>
<p><a href="http://arxiv.org/pdf/1511.00083.pdf" target="_blank">HTM neuron</a></p>
<p><a href="http://arxiv.org/abs/1503.07469" target="_blank">Properties of SDR</a></p>
<p><a href='http://arxiv.org/abs/1602.05925' target='_blank'>Encoding Data for HTM Systems</a></p>
<p><a src='www.youtube.com/watch?v=qZM9JREjnp4&spfreload=5' target='_blank'>Intelligence and Machines: Creating Intelligent Machines by Modeling the Brain with Jeff Hawkins</a></p>

<h2 id="References">References<a class="anchor-link" href="#References">&#182;</a></h2><ul>
<li><a href='https://github.com/vsraptor/bbhtm'>bbHTM Source code</a></li>
<li><a href='bmap2D.html' target='_blank'>BMap2D</a></li>
<li><a href='TM_test.html' target='_blank'>Testing</a></li>
<li><a href='http://ifni.co/articles/programming/recursion' target='_blank'>PatternLang()</a></li>
</ul>
<hr>
<ul>
<li><a href='http://numenta.com/assets/pdf/whitepapers/hierarchical-temporal-memory-cortical-learning-algorithm-0.2.1-en.pdf' target='_blank'>HTM theory</a></li>
<li><a href="http://arxiv.org/pdf/1511.00083.pdf" target="_blank">HTM neuron</a></li>
<li><a href="http://arxiv.org/abs/1503.07469" target="_blank">Properties of SDR</a></li>
<li><a href='http://arxiv.org/abs/1602.05925' target='_blank'>Encoding Data for HTM Systems</a></li>
<li><a src='www.youtube.com/watch?v=qZM9JREjnp4&spfreload=5' target='_blank'>Intelligence and Machines: Creating Intelligent Machines by Modeling the Brain with Jeff Hawkins</a></li>
</ul>
<hr>
<h4 id="===[-HOME--]=====[-Programming--]=====[-Source-code-]=====">===[ <a href="http://ifni.co">HOME</a> ]=====[ <a href="http://ifni.co/toc/programming">Programming</a> ]=====[ <a href="https://github.com/vsraptor/bbhtm">Source code</a> ]=====<a class="anchor-link" href="#===[-HOME--]=====[-Programming--]=====[-Source-code-]=====">&#182;</a></h4>
</div>
</div>
</div>
Expand Down
44 changes: 26 additions & 18 deletions docs/bbHTM.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### [ <a href='http://ifni.co'>HOME</a> ]===== How to compute like a brain =====[ <a href='http://ifni.co/toc/programming'>Programming</a> ]"
"### ============ How to compute like a brain ==========\n",
"\n",
"#### ==============[ <a href='http://ifni.co'>HOME</a> ]=====[ <a href='http://ifni.co/toc/programming'>Programming</a> ]=====[ <a href='https://github.com/vsraptor/bbhtm'>Source code</a> ]=====\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -329,7 +333,11 @@
"\n",
"You should look at the paper to see numerical examples of reliability of those properties for different values of n,w,b and threshold. Suffice to say as long as the vectors are Large > 1000, Sparse < 5% and Distributed the properties hold pretty well.\n",
"\n",
"My current implementation uses exclusively the UNION property of SDR."
"My current implementation uses exclusively the UNION property of SDR.\n",
"\n",
"The big benefit of the union-property is that it allow us to store multiple SDRs in fixed-length bit-string.\n",
"Standardly we would use some structure like array or hash to store multiple elements, but the properties of SDR make it possible to achieve similar result with fixed structure.\n",
"This is possible because as the SDR size grow linearly the possibility of not getting false-positive grows exponentially."
]
},
{
Expand Down Expand Up @@ -1496,7 +1504,7 @@
"source": [
"---\n",
"\n",
"### <a href='TM_test.html' target='_blank'>Let's TEST it !!</a>\n",
"### <a href='TM_test.html' target='_blank'>Test Framework</a>\n",
"\n",
"> Because this document became too large there is separate page on testing.. I have build simplified test framework : <a href='TM_test.html' target='_blank'>Try it out !</a>.\n",
"\n",
Expand All @@ -1507,10 +1515,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Spatial pooler : part 2\n",
"### <a href='spatial_mapper.html' target='_blank'>Spatial pooler/mapper</a>: part 2\n",
"---\n",
"\n",
"> todo implementation ..."
"You can read the details here : <a href='spatial_mapper.html' target='_blank'>Spatial pooler/mapper</a>\n"
]
},
{
Expand Down Expand Up @@ -1588,22 +1596,22 @@
"\n",
"## References\n",
"\n",
"<a href='bmap2D.html' target='_blank'>BMap2D</a>\n",
"\n",
"<a href='TM_test.html' target='_blank'>Testing</a>\n",
"- <a href='https://github.com/vsraptor/bbhtm'>bbHTM Source code</a>\n",
"- <a href='bmap2D.html' target='_blank'>BMap2D</a>\n",
"- <a href='TM_test.html' target='_blank'>Testing</a>\n",
"- <a href='http://ifni.co/articles/programming/recursion' target='_blank'>PatternLang()</a>\n",
"\n",
"<a href='http://ifni.co/articles/programming/recursion' target='_blank'>PatternLang()</a>\n",
"\n",
"<a href='http://numenta.com/assets/pdf/whitepapers/hierarchical-temporal-memory-cortical-learning-algorithm-0.2.1-en.pdf' target='_blank'>HTM theory</a>\n",
"\n",
"<a href=\"http://arxiv.org/pdf/1511.00083.pdf\" target=\"_blank\">HTM neuron</a>\n",
"---\n",
"\n",
"<a href=\"http://arxiv.org/abs/1503.07469\" target=\"_blank\">Properties of SDR</a>\n",
"- <a href='http://numenta.com/assets/pdf/whitepapers/hierarchical-temporal-memory-cortical-learning-algorithm-0.2.1-en.pdf' target='_blank'>HTM theory</a>\n",
"- <a href=\"http://arxiv.org/pdf/1511.00083.pdf\" target=\"_blank\">HTM neuron</a>\n",
"- <a href=\"http://arxiv.org/abs/1503.07469\" target=\"_blank\">Properties of SDR</a>\n",
"- <a href='http://arxiv.org/abs/1602.05925' target='_blank'>Encoding Data for HTM Systems</a>\n",
"- <a src='www.youtube.com/watch?v=qZM9JREjnp4&spfreload=5' target='_blank'>Intelligence and Machines: Creating Intelligent Machines by Modeling the Brain with Jeff Hawkins</a>\n",
"\n",
"<a href='http://arxiv.org/abs/1602.05925' target='_blank'>Encoding Data for HTM Systems</a>\n",
"---\n",
"\n",
"<a src='www.youtube.com/watch?v=qZM9JREjnp4&spfreload=5' target='_blank'>Intelligence and Machines: Creating Intelligent Machines by Modeling the Brain with Jeff Hawkins</a>\n",
"\n"
"#### ===[ <a href='http://ifni.co'>HOME</a> ]=====[ <a href='http://ifni.co/toc/programming'>Programming</a> ]=====[ <a href='https://github.com/vsraptor/bbhtm'>Source code</a> ]====="
]
},
{
Expand Down
Loading

0 comments on commit 2c2a726

Please sign in to comment.