Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 118 lines (90 sloc) 3.51 kb
6fcc8f47 » Joe Ardent
2010-12-30 first commit
1 import sys, os, re, hashlib, rand
2
3 """In the spatial pooler, it's all first-order learning, so the column acts as
4 an atomic unit. The temporal pooler, conversely, treats each cell in a column
5 distinctly."""
6
7 # Some globals; I think these need to go into a Region class
8 ACTIVE_COLUMNS = {}
9 PERMANENCE_INC = 0.1
10 PERMANENCE_DEC = 0.05 # it's harder to forget
11
12 # if a synapse's permanence is greater than this, it is connected
13 CONNECTED_PERM = 0.2
14
15
16 ##-*****************************************************************************
17 class Synapse( object ):
18 def __init__( self, inputIndex ):
19 self.isactive = False
20 self.inputIndex = inputIndex
21 self.permanence = 0.0
22
23 def isConnnected( self ):
24 return self.permanence > CONNECTED_PERM
25
26 def setPermanence( self, perm = 0.0 ):
27 self.permanence = perm
28
29 def getPermanence( self ):
30 return self.permanence
31
32 def isActive( self ):
33 return self.isactive
34
35 def setActive( self, active = False ):
36 self.isactive = active
37
38 def incPerm( self, inc = PERMANENCE_INC ):
39 self.permanence += inc
40 if self.permanence > 1.0:
41 self.permanence = 1.0
42
43 def decPerm( self, dec = PERMANENCE_DEC ):
44 self.permanence -= dec
45 if self.permanence < 0.0:
46 self.permanence = 0.0
47
48 def getIndex( self ):
49 return self.inputIndex
50
51 ##-*****************************************************************************
52 class Cell( object ):
53 def __init__( self ):
54 self.synapses = []
55
56 ##-*****************************************************************************
57 class Column( object ):
58 """Columns are made of Cells, and make Regions"""
59 def __init__( self ):
60 self.boost = 1.0
61 self.overlap = 0.0
62 self.cells = []
63 self.synapses = []
64 for i in range( 4 ):
65 self.cells.append( Cell() )
66
67 def setBoost( self, boost = 1.0 ):
68 self.boost = boost
69
70 def setOverlap( self, overlap = 0.0 ):
71 self.overlap = overlap
72
73 def getOverlap( self ):
74 return self.overlap
75
76 def boostOverlap( self ):
77 self.overlap *= self.boost
78
79 def getConnectedSynapses( self ):
80 return map( lambda x: x.isConnected(), self.synapses )
81
82 ##-*****************************************************************************
83 def findOverlap( columns, t, minOverlap ):
84 """Spatial pooler overlap function"""
85 for c in columns:
86 c.setOverlap() # defaults to 0.0
87 for s in c.getConnectedSynapses():
88 c.setOverlap( c.getOverlap() + s.getSourcetInput( t ) )
89
90 if c.getOverlap() < minOverlap:
91 c.setOverlap()
92 else:
93 c.boostOverlap()
94
95
96 ##-*****************************************************************************
97 def findActiveColumns( columns, t, desiredLocalActivity = 5.0 ):
98 """Spatial pooler active-column-finder"""
99 activeColumns = []
100 for c in columns:
101 if not c.getOverlap() > 0:
102 continue
103
104 minLocalActivity = kthScore( getNeighbors( c ), desiredLocalActivity )
105
106 if c.getOverlap() >= minLocalActivity:
107 activeColumns.append( c )
108
109 ACTIVE_COLUMNS[t] = activeColumns
110
111 ##-*****************************************************************************
112 def learnAndInhibit( activeColumns, t ):
113 for c in activeColumns:
114 for s in c.getSynapses():
115 if s.isActive():
116 s.incPerm()
117 else:
118 s.decPerm()
Something went wrong with that request. Please try again.