I’ve hacked together a proof of concept script from the hot gym example using the HTM community fork of htm.core. My goal is to eventually use HTMs to look for anomalies in multiple streams of data. The first hurdle I have come across is that htm.core is very slow. Here is a single step of the learning / predicting process:

```
def _time_step(self, index: int, s: float) -> None:
"""
Takes a time step in the data.
Parameters
----------
index : int
The number of this time step.
s : float
The value of the signal.
"""
t0 = time.time()
# create an SDR structure for this data point
activeColumns = SDR(self._pipeline['pooler'].getColumnDimensions())
t1 = time.time()
self.results['t_active'][index] = t1 - t0
# encode the value s
encoding = self._pipeline['encoder'].encode(s)
self._metrics['encoder'].addData(encoding)
t2 = time.time()
self.results['t_encode'][index] = t2 - t1
# execute spatial pooling over the encoded input data
self._pipeline['pooler'].compute(input=encoding, learn=True,
output=activeColumns)
self._metrics['pooler'].addData(activeColumns)
t3 = time.time()
self.results['t_pool'][index] = t3 - t2
# execute temporal memory over the active mini-columns
self._pipeline['memory'].compute(activeColumns=activeColumns,
learn=True)
activeCells = self._pipeline['memory'].getActiveCells()
self._metrics['memory'].addData(activeCells.flatten())
t4 = time.time()
self.results['t_memory'][index] = t4 - t3
# ---- predictions ----
# predict what will happen for each horizon
pdf = self._pipeline['predictor'].infer(activeCells)
for steps in pdf.keys():
if pdf[steps]:
self.results['predictions'][steps][index] = (
np.argmax(pdf[steps])
* self.args['predictor']['resolution']
)
else:
self.results['predictions'][steps][index] = float('nan')
tma = self._pipeline['memory'].anomaly
self.results['anomalyScore'][index] = tma
self.results['anomalyProb'][index] = \
self._pipeline['anom_history'].compute(tma)
t5 = time.time()
self.results['t_predict'][index] = t5 - t4
# let the predictor learn
self._pipeline['predictor'].learn(
index, activeCells,
int(np.abs(s) / self.args['predictor']['resolution'])
)
t6 = time.time()
self.results['t_learn'][index] = t6 - t5
self.results['runTime'][index] = t6 - t0
```

I’ve timed each of the steps, which you can see in the code above. When I plot the time to run each step, I see the following:

(The x-axis should be microseconds, not milliseconds.)

It is clear that the runTime is dominated by the Spatial Pooler (t_pool) and Temporal Memory Steps (t_memory), which are at least an order of magnitude larger than the other steps. Since the HTM school videos only have vague descriptions of the mathematics of HTM learning, I have little idea what is going on under the hood here. Is it possible to speed up these two processes by 10-100X?