Showing posts with label pattern recognition. Show all posts
Showing posts with label pattern recognition. Show all posts

Monday, March 29, 2010

Understanding the Bitworm NuPIC HTM Example Program , Part 1

Now for my least favorite part of intellectual projects, figuring out someone else's computer code.
When I installed the NuPIC package, a program called Bitworm was run to show that NuPIC installed correctly. Bitworm's main program, RunOnce.py is written in Python script and might be characterized as a simplest meaningul example program, which makes it considerably more complicated than your typical Hello World one liner.

The explanation of, and instructions for running and playing with Bitworm can be found in Getting Started With NuPIC (see pages 14-23). If you open RunOnce.py (mine conveniently opened in IDLE, "Python's Integrated Development Environment") there is a good outline of the process too.

The point is to test an HTM (Hierarchical Temporal Memory) with a simple data set. If you got here without knowing about HTMs, see www.numenta.com or my glosss starting with Evaluating HTMs, Part 1.

Bitworm, or RunOnce, starts by creating a minimal HTM. It does this by importing nodes and components using functions that are part of the NuPIC package. It also sets some parameters which have already been built elsewhere. Then the HTM is trained using another already-created data set of bitworms, which are essentially short binary strings easily visualized if 1's as interpreted as black and 0's as white (or whatever colors you like). Later I'll want to look inside the nodes, and at how nodes are interconnected, in order to understand why this works, but for now I'll keep to the top-level-view.

To test if the NuPIC HTM network learned to distinguish 2 types of bitworms, the training data set is again presented to see what outputs the HTM gives. This is also known as pattern recognition, but in temporal memory talk we prefer the term inference. The bitworms are examples of causes (objects in most other systems), and the HTM infers, from the data, which causes are being presented to it.

That seems like too easy of a trick, infering causes based on the training set, so RunOnce also sees how the trained network does trying to infer cuases from a somewhat different set of data.

As output RunOnce gives us the percentages of correct inferences for the training set and second data set, plus some information about the network itself.

Presuming that you are using Windows and downloaded and setup the NuPIC package (see prior blog entry), to run Bitworm with RunOnce.py, open a command prompt (press Start, in the search box type Command. This should show Command Prompt at the top of the program list. Click it once. Since you will need Command Prompt often, you might also return to Start, right-click on Command Prompt, and Pin to Start Menu. Then it is always in your Start Menu. Or create a shortcut).

Type:

cd %NTA%\share\projects\bitworm

and hit Enter. That will get you in the right directory.

Then run RunOnce by typing the following and hitting Enter:

python RunOnce.py

If you get errors, you need to run the Command Prompt as an Administrator. Close the window, then right click on Command Prompt and choose Run As Administrator. Click through security warnings.

The output says there were two sets off 420 data vectors written. Inference with the training set as input data was 100% accurate. Inference with the 2nd data set was 97.85...% accurate.

As it says, you can also open report.txt. Here's what mine says:

General network statistics:
Network has 5 nodes.
Node names are:
category
fileWriter
level1
sensor
topNode

Node Level1 has 40 coincidences and 7 groups.
Node Level2 has 8 coincidences.
------------------------------
Performance statistics:

Comparing: training_results.txt with training_categories.txt
Performance on training set: 100.00%, 420 correct out of 420 vectors
Comparing: test_results.txt with test_categories.txt
Performance on test set: 97.86%, 411 correct out of 420 vectors
------------------------------
Getting groups and coincidences from the node Level1 in network ' trained_bitworm.xml


====> Group = 0
1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0

0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0

0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0

0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0

0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1



====> Group = 1

0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0

0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0

0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0

0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0

0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0

0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1



====> Group = 2

0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0

0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0

0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0

0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0

0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0

0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0



====> Group = 3

0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0

0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0

0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0

0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1



====> Group = 4

0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0

0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0

0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0

0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0

0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0

0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0

0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1



====> Group = 5

0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0

0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0

0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0

0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0

0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1



====> Group = 6

0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1



Full set of Level 2 coincidences:

0 -> [ 0. 0. 1. 0. 0. 0. 0. 0.]

1 -> [ 1. 0. 0. 0. 0. 0. 0. 0.]

2 -> [ 0. 0. 0. 1. 0. 0. 0. 0.]

3 -> [ 0. 0. 0. 0. 0. 1. 0. 0.]

4 -> [ 0. 1. 0. 0. 0. 0. 0. 0.]

5 -> [ 0. 0. 0. 0. 0. 0. 1. 0.]

6 -> [ 0. 0. 0. 0. 0. 0. 0. 1.]

7 -> [ 0. 0. 0. 0. 1. 0. 0. 0.]

Thursday, March 18, 2010

Evaluating HTMs: CPT details; specific memories

This is the last essay on "Hierarchical Temporary Memory, Concepts, Theory, and Terminology" by Hawkins and George. Here I review two issues raised in Section 6, Questions: details on how conditional probability tables (CPTs) work with HTMs, and why humans can have specific memories of events, but HTMs as currently described do not. The first is very technical, the second has more interesting implications.

CPTs are used in Bayesian networks to allow the belief (a set of probabilities about causes) of one node to modify another node. They can be create from algorithms using probability theory in conjunction with known data, the beliefs already established in the two nodes. In HTMs they are learned. As the quantization points are learned, the CPTs are the same as the learned quantization function that links the points to the temporal variables. There are two separate algorithms, but they run in parallel, creating an output to send up the hierarchy to the next node. This will probably because more transparent when we look at the actual algorithms used by the HTM nodes.

It is claimed that humans can remember specific details and events, as well as model the world, whereas HTMs don't keep specific memories. The authors talk about how the human brain might accomplish this feat, and how the capability might be added to HTMs. I instead wonder whether they are right about humans remembering specific details of specific events.

It certainly is the naive view, and since I subscribe to the common sense school of philosophy (with my own updates), assailing the view is mainly just an exercise at this point. But consider this: numerous studies have shown that eye witnesses are unreliable. I suspect that a visual memory is not like a photograph, nor is the memory of a song like a recording, nor is the memory of an event a sort of whole sensory record of a period of time. I believe humans do remember things, and can train their memories to be more like recorders, and in particular can memorize speeches, poems, sequences of numbers, etc. But I think the HTM model actually is at least approximately the way that the brain works. Different levels of the neurological system remember, or become capable of recognizing, different levels of details about things. There are mechanisms in the brain that allow recall of these memories on different levels. But I would be careful about assuming that because we can recall an event (or picture, etc.) in more or less detail we must be calling up a recording. We seldom learn anything of any length in detail by simply hearing or seeing it. If you have memorized that the first digits of pi are 3.14159, what is that a recording of? The words for the number sequence as sounded out in English, a visual memory of seeing this number in a particular typeface in a particular paragraph on paper of a particular tone, or an abstract memory corresponding to abstract groups of abstract units? Typically we must be exposed to something many times to be able to remember it or recognize it, just like an HTM.

I think we are so good at reconstructing certain types of memories that we think we have photograph or video-like recordings of them. That is why eye witnesses think they are telling the truth, when they often substitute details from other events into a "memory" [notably, a face from a lineup that actually was not present at a crime scene]. That is why our memories are so often mistaken (I could have sworn I turned off that burner!) and why we can recall so much without having a roomful of DVDs in our brains. Our memories are largely indistinguishable from our intelligence, and are both fragmented in detail and yet easily molded into a whole as necessary. This is why recognition is usually much better than recall.

The more I study HTMs, the more curious I get. I don't know what the next step will be in my investigations, but hopefully I'll let you know soon.

Tuesday, March 9, 2010

Why Time is Necessary for HTM's Learning

In "Hierarchical Temporary Memory, Concepts, Theory, and Terminology " by Hawkins and George, Section 5, Why is Time Necessary to Learn? clarifies the role of temporal sequences and temporal pattern points in both learning and recognition (inference) by HTMs.

The authors use a good example, a cut versus uncut watermelon, to distinguish between pattern matching algorithms, and how HTM's learn to recognize patterns that are created by objects (causes, in HTM vocabulary). Any real world animal, when viewed, presents an almost infinite number of different visual representations. If you use a type of animal, say horses instead of a particular horse, the data is even more divergent. Pattern matching does not work well. But allow an HTM to view an animal or set of animals over time, and it will build up the ability to recognize an animal from different viewpoints: front, back, profile, or against most sorts of backgrounds.To do that requires data presented over time. Data that is close sequentially should be similar but not identical. Early data might be of a horse, head on, far away, which gradually resolves to a horse viewed close up. So over time the HTM can capture the totality of the horse.

Combining recognition of causes with names given by an outside source is also considered. Thus no amount of viewing a horse will tell an HTMs that human's call the thing "horse." You can do "supervised learning" with an HTM, training it to associate a name with a cause by imposing states on the top level of the HTM hierarchy. But it should be a simple extension to have a vocabulary learning HTM and an object learning HTM in a hierarchy with a learn-to-name-the-object HTM on top.

Once an HTM has learned to recognize images (or other types of data) it can recognize static images (or data). The authors say "The Belief Propagation techniques of the hierarchy will try to resolve the ambiguity of which sequences are active." I am not clear on that. It seems to me that static temporal patterns happen often enough in the real world so that some temporal pattern points will represent static causes. If the horse stands still in the real world, it would generate such temporal patterns. As the data goes up the hierarchy it tends to filter out ambiguity and stabilize causes, so a leaping horse should still be the same as a frozen image of a leaping horse at some point high enough in the hierarchy.

Section 6 is a sort of frequently asked questions part of the paper. I'm not sure if I'll cover all the sections or in what order, and I do want to go back to section 3.3 on belief propagation before closing out this series.