-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtemporalMemoryConnections.go
168 lines (136 loc) · 4.42 KB
/
temporalMemoryConnections.go
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
package htm
import (
// "fmt"
// "github.com/cznic/mathutil"
// "github.com/zacg/floats"
// "github.com/zacg/go.matrix"
//"github.com/nupic-community/htmutils"
"github.com/zacg/ints"
// //"math"
// "math/rand"
// //"sort"
)
type TmSynapse struct {
Segment int
SourceCell int
Permanence float64
}
/*
Structure holds data representing the connectivity of a layer of cells,
that the TM operates on.
*/
type TemporalMemoryConnections struct {
ColumnDimensions []int
CellsPerColumn int
segments []int
synapses []*TmSynapse
synapsesForSegment [][]int
synapsesForSourceCell [][]int
segmentsForCell [][]int
segmentIndex int
synIndex int
maxSynapseCount int
}
//Create a new temporal memory
func NewTemporalMemoryConnections(maxSynCount int, cellsPerColumn int, colDimensions []int) *TemporalMemoryConnections {
if len(colDimensions) < 1 {
panic("Column dimensions must be greater than 0")
}
if cellsPerColumn < 1 {
panic("Number of cells per column must be greater than 0")
}
c := new(TemporalMemoryConnections)
c.maxSynapseCount = maxSynCount
c.CellsPerColumn = cellsPerColumn
c.ColumnDimensions = colDimensions
c.synapses = make([]*TmSynapse, 0, c.maxSynapseCount)
//TODO: calc better size
c.segments = make([]int, 0, 50000)
c.segmentsForCell = make([][]int, cap(c.segments))
c.synapsesForSegment = make([][]int, cap(c.segments))
c.synapsesForSourceCell = make([][]int, cap(c.segments))
return c
}
// func (tmc *TemporalMemoryConnections) nextSegmentIndex() int {
// idx := tmc.segmentIndex
// tmc.segmentIndex++
// return idx
// }
// func (tmc *TemporalMemoryConnections) nextSynapseIndex() int {
// idx := tmc.synIndex
// tmc.synIndex++
// return idx
// }
func (tmc *TemporalMemoryConnections) CreateSynapse(segment int, sourceCell int, permanence float64) *TmSynapse {
syn := len(tmc.synapses)
data := new(TmSynapse)
data.Segment = segment
data.SourceCell = sourceCell
data.Permanence = permanence
tmc.synapses = append(tmc.synapses, data)
//Update indexes
tmc.synapsesForSegment[segment] = append(tmc.synapsesForSegment[segment], syn)
tmc.synapsesForSourceCell[sourceCell] = append(tmc.synapsesForSourceCell[sourceCell], syn)
return data
}
//Creates a new segment on specified cell, returns segment index
func (tmc *TemporalMemoryConnections) CreateSegment(cell int) int {
idx := len(tmc.segments)
// Add data
tmc.segments = append(tmc.segments, cell)
tmc.segmentsForCell[cell] = append(tmc.segmentsForCell[cell], idx)
return idx
}
//Updates the permanence for a synapse.
func (tmc *TemporalMemoryConnections) UpdateSynapsePermanence(synapse int, permanence float64) {
tmc.validatePermanence(permanence)
tmc.synapses[synapse].Permanence = permanence
}
//Returns the index of the column that a cell belongs to.
func (tmc *TemporalMemoryConnections) ColumnForCell(cell int) int {
return int(cell / tmc.CellsPerColumn)
}
//Returns the indices of cells that belong to a column.
func (tmc *TemporalMemoryConnections) CellsForColumn(column int) []int {
start := tmc.CellsPerColumn * column
result := make([]int, tmc.CellsPerColumn)
for idx, _ := range result {
result[idx] = start + idx
}
return result
}
//Returns the cell that a segment belongs to.
func (tmc *TemporalMemoryConnections) CellForSegment(segment int) int {
return tmc.segments[segment]
}
//Returns the segments that belong to a cell.
func (tmc *TemporalMemoryConnections) SegmentsForCell(cell int) []int {
return tmc.segmentsForCell[cell]
}
//Returns synapse data for specified index
func (tmc *TemporalMemoryConnections) DataForSynapse(synapse int) *TmSynapse {
return tmc.synapses[synapse]
}
//Returns the synapses on a segment.
func (tmc *TemporalMemoryConnections) SynapsesForSegment(segment int) []int {
return tmc.synapsesForSegment[segment]
}
//Returns the synapses for the source cell that they synapse on.
func (tmc *TemporalMemoryConnections) SynapsesForSourceCell(sourceCell int) []int {
return tmc.synapsesForSourceCell[sourceCell]
}
// Helpers
//Returns the number of columns in this layer.
func (tmc *TemporalMemoryConnections) NumberOfColumns() int {
return ints.Prod(tmc.ColumnDimensions)
}
//Returns the number of cells in this layer.
func (tmc *TemporalMemoryConnections) NumberOfcells() int {
return tmc.NumberOfColumns() * tmc.CellsPerColumn
}
//Validation
func (tmc *TemporalMemoryConnections) validatePermanence(permanence float64) {
if permanence < 0 || permanence > 1 {
panic("invalid permanence value")
}
}