在脑皮层学习算法 —nupic的深入学习(一)中大致分析了HTM算法的架构,在接下来的篇幅会详细学习空间池和时间池的大概。
首先复习下htm算法的大概经过:
- 构建输入的稀疏离散表征
近端树突接受encoder输入,将二进制的向量变成稀疏离散表征(SDR),柱状区域会全部激活
潜在突触: - 在有先前输入的环境中构建输入的表征
若一个柱状区域被激活,如果有一个或多个细胞已经处于预测状态,激活,否则激活整个柱状区域 - 在有先前输入的环境中根据当前环境构建预测
下面这段话摘自白皮书,说明算法是如何进行预测的
How does a region make a prediction? When input patterns change over time, different sets of columns and cells become active in sequence. When a cell becomes active, it forms connections to a subset of the cells nearby that were active immediately prior. These connections can be formed quickly or slowly depending on the learning rate required by the application. Later, all a cell needs to do is to look at these connections for coincident activity. If the connections become active, the cell can expect that it might become active shortly and enters a predictive state. Thus the feed-forward activation of a set of cells will lead to the predictive activation of other sets of cells that typically follow. Think of this as the moment when you recognize a song and start predicting the next notes.
——–
In summary, when a new input arrives, it leads to a sparse set of active columns. One or more of the cells in each column become active, these in turn cause other cells to enter a predictive state through learned connections between cells in the region. The cells activated by connections within the region constitute a prediction of what is likely to happen next. When the next feed-forward input arrives, it selects another sparse set of active columns. If a newly active column is unexpected, meaning it was not predicted by any cells, it will activate all the cells in the columns. If a newly active column has one or more predicted cells, only those cells will become active. The output of a region is the activity of all cells in the region, including the cells active because of feed-forward input and the cells active in the predictive state
上面两段话解释了两点:
细胞如何进入预测状态?
随着时间的演进,不同的SDR被激活,每一次激活的SDR都会跟上一次激活的SDR通过突触,构建起联系(细看的话,就是cell与cell之间的连接)。然后,根据某种原则,这些构建的突触会变得活跃,与之相关联的cell就被视为predicted。
如果输入是个unexpected的值,区域内部会怎么处理?
会激活柱状区域的处于预测态的细胞,如果没有,激活全部。
第2,3步表示时间沉积池,1步是空间沉积池
在讲述时间池和空间池之前,先讲述一些重要概念:
- 树突区域
两种树突,远端树突—接受前馈输入
近端树突—与其他cell连接。一个cell有多个近端树突,如果一个近端树突区域的活跃突触高于阈值,细胞就进入预测状态 - 学习
涉及树突区域的潜在突触的连通值的增加和减少
空间池Spatial Pooler
作用: 促进不活跃的柱状区域,抑制周围的柱状区域来维持稀疏度,确定最小的输入阈值,维持一个庞大的潜在突触池,基于突触的贡献来进行增减权值
如何确定哪些柱状区域被激活?
计算column的overlap,选出overlap大于0的值,作为active的备选(The overlap for each column is simply the number of connected synapses with active inputs, multiplied by its boost. If this value is below minOverlap, we set the overlap score to zero)
在抑制半径里选择一定百分比的柱状区域激活,成为winner。
在学习阶段,需要更新权值,一般希望特定的突触对于特定的输入具有响应,这样达到不同模式具有不同稀疏表征的效果,所以使得活跃column的潜在突触中,所有连接活跃bits(=1)的突触权值自增,而连接不活跃bits(=0)的突触权值自减。其他column的突触权值不变。
connectedPerm (the minimum permanence value at which a synapse is considered “connected”).
代码实现:
1. overlap计算,overlap就是是连接活跃bits的活跃突触数目,overlap大于0的columns,会被视为active的备选
2. 抑制。对于1的结果,抑制半径内的一定百分比columns会激活
3. 学习。会跟新激活columns的潜在突触的连通值。潜在突触中,所有连接活跃bits(=1)的突触权值自增,而连接不活跃bits(=0)的突触权值自减。其他column的突触权值不变。进行boost操作,对于不经常被激活以及overlap不大于0的columns,增加其潜在突触权值。更新抑制半径。
参数(大致):
空间池:
sp = SpatialPooler(
# How large the input encoding will be.
inputDimensions=(encodingWidth),
# How many mini-columns will be in the Spatial Pooler.
columnDimensions=(2048),
# What percent of the columns's receptive field is available for potential
# synapses?
potentialPct=0.85,
# This means that the input space has no topology.
globalInhibition=True,
localAreaDensity=-1.0,
# Roughly 2%, giving that there is only one inhibition area because we have
# turned on globalInhibition (40 / 2048 = 0.0195)
numActiveColumnsPerInhArea=40.0,
# How quickly synapses grow and degrade.
synPermInactiveDec=0.005,
synPermActiveInc=0.04,
synPermConnected=0.1,
# boostStrength controls the strength of boosting. Boosting encourages
# efficient usage of SP columns.
boostStrength=3.0,
# Random number generator seed.
seed=1956,
# Determines if inputs at the beginning and end of an input dimension should
# be considered neighbors when mapping columns to inputs.
wrapAround=False
)
时间池Temporary pooler
Phase 1: compute the active state, activeState(t), for each cell
Phase 2: compute the predicted state, predictiveState(t), for each cell
Phase 3: update synapses
下面是时间池的步骤,讲述了时间池是怎么预测的,以及如何实现多步预测的。
- 空间池的计算计算结束后,得到一些取胜的柱状区域,计算其中每个细胞的activeState。如果该细胞在前一刻是处于预测态,则该细胞被激活,如果前馈输入是非预期的,即取胜的柱状区域里,没有细胞是前一刻处于预测态的,则column全部细胞都变成activeState。有一个最关键的点:如果被激活的细胞,其某个树突是被learningState为1的细胞激活的,那这个被激活的细胞会被选为学习细胞(learningState),只有学习细胞的突触连通值才会被增加。这是为了防止突触过活跃的表现。
- 每个细胞都会有很多的远端树状区域。这些树状区域与同区域的其他细胞基于“突触”进行连接。对于winner柱状区域,如果某细胞的某个树状区域连接的活跃细胞超过一定阈值,则该树突区域置为活跃,该细胞被置为预测态。
- 对于活跃的树状区域,更改其潜在突触的权值。对于链接到活跃细胞的突触,增加其连通值,对于那些链接到不活跃细胞的突触,减少其连通值。并且,对于该细胞的其他树状区域,按照以上原则,增加或减少最匹配当前细胞的活动的突触的连通值(即也能导致该细胞当前被置为预测态)。这是实现多步预测的关键。值得注意的是,遵循这个“增加/减少”原则的只有learningState细胞,还有一种细胞会进行突触连通值的变化:上一刻是预测态,但当前刻不是。这种细胞会被视为预测错了,改变的原则是,活跃突触的连通值会一并减少。
参数:
时间池:
tm = TemporalMemory(
# Must be the same dimensions as the SP
columnDimensions=(2048, ),
# How many cells in each mini-column.
cellsPerColumn=32,
# A segment is active if it has >= activationThreshold connected synapses
# that are active due to infActiveState
activationThreshold=16,
initialPermanence=0.21,
connectedPermanence=0.5,
# Minimum number of active synapses for a segment to be considered during
# search for the best-matching segments.
minThreshold=12,
# The max number of synapses added to a segment during learning
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.1,
predictedSegmentDecrement=0.0,
maxSegmentsPerCell=128,
maxSynapsesPerSegment=32,
seed=1960
)
总结
Nupic输入,处理,输出步骤(自己总结的):
1. 接受外部输入,基于近端突触是否连通以及连接的活跃bits的数量,决定该柱状区域是否成为激活的备选,再基于抑制半径,进一步稀疏激活的数量,最终确定激活的柱状区域。这一步在sp中,被称为SDR的构建。
当然,在sp中,有许多细节需要注意,比如说HTM希望所有的columns都要被用来进行一定模式的表征,所以那些因为覆蓋值(overlap,就是连接活跃bits的活跃突触数目)长期不够,与那些因为覆蓋值长期不能进入抑制半径内前列的columns突触权值进行boosting,即增加其权值。
2. 在完成了SDR后,对于之后进来的外部输入,按照第1步的做法,激活相应的柱状区域,但这次有个原则:对于一个柱状区域的细胞,如前一步处于预测态,则激活,如果没有,则激活所有细胞。
3. 第2步激活细胞后,这些细胞会和前一步处于激活状态的细胞建立起突触。然后遍历各个细胞,如果某细胞的某个树突区域连接的活跃细胞高于阈值,则将该细胞处于预测态。总的处于预测态的细胞,就是我们所要的预测。
4. 对于那些是learningState的细胞(其某个树突是被learningState为1的细胞激活),对于其活跃树突区域,对于链接到活跃细胞的突触,增加其连通值,对于那些链接到不活跃细胞的突触,减少其连通值。
下面是一个Nupic的应用实例,利用Nupic实现用户网站类别点击预测
如果对htm算法有问题,可以私聊我,我们一起讨论。