![]() ![]() ![]()
|
1 Bitworm: Getting Started Example
This chapter gets you started with the Numenta Platform for Intelligent Computing (NuPIC) by explaining how you can run a simple example HTM Network and by examining the example scripts.
Topics
The Bitworm Example
This chapter introduces a simple example called Bitworm. The example illustrates how you might structure your input and category input, how to run your HTM Network, and how to interpret the results. Bitworm is not intended to be a realistic problem, instead, it's used as a Hello World example to get you up and running with NuPIC.
What are Bitworms?
Bitworms are 16-bit vectors. There are solid bitworms, which consist of consecutive on-bits, and textured bitworms, which consist of alternating on/off bits. In each case, the part of the vector that's not a bitworm consists of off bits. Here are some examples:
Figure 1 Solid and Textured Bitworms
The Bitworm example program trains an HTM Network to model the world of bitworms. After the HTM Network has been trained, you can submit new data and the HTM Network uses the model of the bitworm world to discriminate between solid and textured bitworms.
Bitworm Example Components
The Bitworm example consists of the following files, discussed in more detail below:
Running the Example
This section explains how to run the example and how to explore what the HTM Network does by changing the example configuration. The example has been set up so you need to execute only one script.
To run the Bitworm example:
On Microsoft Windows, open a command prompt and type:
On OS X and Linux - assuming
$HOME/ntais the location where you installed the software - type the following at the command line:
The example is set up so you always make modifications to theRunOnce.pyscript, then rerunRunOnce.py.The script performs these tasks:
1. Calls
GenerateData.pywith the parameters set inRunOnce.pyto generate a set of training data. The default is to generate temporally coherent data, that is, sequences of solid and textured bitworms of variable bitworm length. The minimum and maximum length are specified in thetrainingMinLength/testMinLengthandtrainingMaxLength/testMaxLengthparameters.2. Creates the bitworm HTM Network, and calls helper functions to add nodes. The nodes are linked automatically.
3. Trains the HTM Network by calling the
TrainBasicNetwork()function.During training, the nodes learn, that is, they construct a model of their world. Training proceeds level by level, starting at the bottom.
4. When the top-level node receives input during training, it also receives category information. It groups the data based on the categories.
5.
RunOnce.pycallsRunBasicNetwork()to explore how the trained HTM Network handles new data. The trained HTM Network looks at each input bitworm and determines the probability that the bitworm belongs to one or the other category. This process of categorizing data based on previous training is called inference.6. Finally,
RunOnce.pycallsGenerateReport, which prints the coincidences for each group to a file calledreport.txt.Examining the Report
The
GenerateReport.pyscript that is run as part ofRunOncegenerates a report file namedreport.txtthat includes the following information:General network statistics: Network has 5 nodes. Node names are: Sensor CategorySensor Level1 Level2 FileOutput 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 vectorsNote that the Bitworm example gets very good results because this is a toy problem: The assumptions matched those of the current learning algorithm precisely. Achieving the same degree of success for more complex problems can be more challenging.
====> 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 1This display can be helpful in a simple program, such as bitworms. It's easy to see how clean the groups are. Each group contains a different kind of bitworm. For other programs, using Numenta Visualizer or other tools to explore the grouping might be better than looking at a file. See Using HTM Network Visualizer and Plotting and GUI Packages Bundled with NuPIC.
You can look at theGenerateReport.pyfile to see what Python calls you can use to retrieve information from the network. Comments in the file make it easy to understand your options.Displaying the Report
You can run
DisplayReport.pyto see a visual representation of the groups, for example:Running the Example with Temporally Incoherent Data
You can change the
useCoherentDataparameter inRunOnce.pyto generate solid and textured bitworms that are not presented in sequence, that is, that have no temporal relationship, as shown in Figure 2. Submitting those data to the trained HTM Network illustrates the importance of the temporal aspect of the training data.Figure 2 Bitworms Presented Without Temporal Coherence
To run the example with incoherent data:
1. In the
RunOnce.pyscript, change theuseCoherentDataparameter toFalse.2. Execute
RunOnce.pyagain.The example runs with data that include both solid and textured bitworms but don't present sequences of solid bitworms followed by sequences of textured bitworms.
3. Examine the
report.txtfile this run generated. You should see that the HTM system found it difficult to find the groups and to categorize the data.Running the Example with Noisy Data
The data generation script allows you to change the data by introducing some noise and to observe the results. There are two types of noise:
0 0 0 0 1 1 1 1 1 0 0 0 0 0 without noise -.1 0.1 0 1.01 1 1.1 .98 .98 1.05 0 0.05 0.09 0 0.07 with noiseIn the bitworm example, you can introduce noise to the data and see how the noise affects recognition.
To run the example with noisy data:
1. In the
RunOnce.pyscript, return theuseCoherentDataparameter toTrue.2. Change
additiveNoiseTrainingto 0.1. This setting adds uniform random noise in the range [-0.1, 0.1] to the inputs. Once you start adding noise to the inputs, it becomes harder for the learning algorithm to detect temporal coherence.3. Execute
RunOnce.py.You get a Python exception with the message
The current parameters generated 400 groups, which exceeds the maximum of 25 groups. This message means you didn't have enough outputs for the number of groups that were found. Although the number of underlying causes has not changed, the noise makes it harder for the algorithm to create a compact set of groups based on temporal coherence.4. In
RunOnce.pysetmaxDistanceto 0.1 and save the revised file.The
maxDistanceparameter sets the maximum Euclidean distance at which two input vectors are considered the same during learning. The default for this parameter is 0, so a change usually means better performance if some noise is present. See Affecting Learning Node Behavior With Node Parameters in Advanced NuPIC Programming.5. Call
RunOnce.pyagain. The script now runs without generating an exception.6. Examine the
report.txtfile this run generated. You should see that the results are good; however, notice that the number of groups is fairly large compared to the original number. This is an indication that the learning algorithm found it more difficult to find the groups and categorize the data.7. Set
maxDistanceto 0.2, rerunRunOnce.py, and examinereports.txt. This time the HTM gets the same number of groups as it did before you added noise.This example illustrates how a combination of parameters (
maxDistanceandmaxGroups) affects whether the HTM Network works well or does not work at all. If you wish, you can experiment with some of the otherRunOnce.pyparameters. See Table 2:, RunOnce.py parameters..Understanding the Example Scripts
This section briefly discusses the Bitworm example scripts.
RunOnce.py: Your Entry Point to Bitworms
The
RunOnce.pyscript runs the component scripts of the example in sequence. As a rule, you should always callRunOnce.py, not one of the component scripts.
RunOnceallows you to set the following parameters:
Table 2: RunOnce.py parameters.useCoherentData When set to true (the default), theGenerateData.pyscript creates sequences of solid bitworms followed by sequences of textured bitworms.When set to false, theGenerateData.pyscript mixes solid and textured bitworms randomly. In that case, the temporal element is missing from the data.numSequencesPerBitwormType Number of sequences for each bitworm type. For example, you could present ten sequences of textured bitworms and ten sequences of solid bitworms. The sequences are always separated by a row of zeros (0).GenerateData.pyalways generates the same number of sequences of each type.sequenceLength Length of each sequence (e.g 20 bitworm vectors, followed by one vector of zeros). Minimum and maximum length of the generated bitworms.inputSize Size of the input vector. Defaults to 16. Probability that a bit will be flipped from 0 to 1 or vice versa, that is, a 0 bit becomes 1 or a 1 bit becomes 0. Can be combined with additiveNoise. Default is 0. See Running the Example with Noisy Data.maxGroups Maximum number of groups that can be learned at level 1.maxGroupSize Specifies how large the groups in the temporal pooler can become. maxDistance Sets the maximum Euclidean distance at which two input vectors are considered the same during learning. See Affecting Learning Node Behavior With Node Parameters in Advanced NuPIC Programming.GenerateData.py: An Example of Data Generation
The
GenerateData.pyscript generates a file with training data or testing data, plus an associated category file, using the parameter settings specified inRunOnce.py. Data are generated in sequences: For each sequence, the code generates a bitworm specified bysequenceLengthusing a random length and position (within the current parameter constraints), and then slides the bitworm left or right. At the end of each sequence,GenerateData.pyinserts a line of zeros to reset the node so that the node does not attempt to learn temporal correlation between two bitworm sequences.The script includes methods to generate data in which no temporal correlation exists. Those methods are called when the
useCoherentDataparameter is set toFalse.Several aspects of this data setup are interesting:
- Each bitworm is not considered a sequence of bits but instead a single unit. The same way, a vision system processes input one "glance" at a time (not one pixel at a time) and compares "glances" over time.
- The data are presented as a set of logical sequences. Each sequence shows a single bitworm moving left or right. Within each sequence, each input pattern is correlated with the next pattern. This temporal coherence is critical for any HTM training data. HTMs (like people) expect a world that does not change drastically from one moment to the next. HTM learning algorithms exploit this property.
- In the data file, each sequence is followed by a row of zeros that separates it from the next sequence. At the start of each new sequence, the data generator decides which type of bitworm it wants to present next. This approach can be compared to viewing the frames of scenes in a movie. You need to see a set of frames to get a coherent picture, and then you can switch to the next scene.
RunOnce.py: Creating, Training, and Using the Trained Network
RunOnce.pyperforms the following tasks, discussed in this section:Creating the Untrained HTM Network File
RunOnce.pycreates theNetworkand adds the nodes using helper functions. See Constructing an HTM Network.Figure 3 shows the hierarchy of nodes in the bitworm example. This is the simplest possible HTM hierarchy.
Figure 3 Nodes in the Bitworm Example
To create this hierarchy, the script goes through these steps:
1. Creates the
Networkinstance.2. Uses the
AddSensor(),AddZeta1Level(), andAddClassifierNode()function to specify each level in turn.AddSensor(bitNet, featureVectorLength = inputSize)AddZeta1Level(bitNet, numNodes = 1) AddClassifierNode(bitNet, numCategories = 2)The bitworm network has a data sensor and a category sensor, one bottom-level node, one top-level node, and one effector. In this example,
3. The
Networkaccessor functions are used to set parameters, for example:bitNet['level1'].setParameter('maxDistance',maxDistance)
bitNet['level1'].setParameter('transitionMemory', transitionMemory)
Training the HTM Network
During training, each node in the HTM Network builds a model of its world using the available input data.
RunOnce.pyperforms training using theTrainBasicNetwork()function. This function trains the network created earlier and returns aRunTimeNetworkobject, which contains the trained network.TrainBasicNetwork()uses the training files generated byGenerateData.py(see GenerateData.py: An Example of Data Generation).bitNet = TrainBasicNetwork( bitNet, dataFiles = [trainingFile], categoryFiles = [trainingCategories])Running the Trained Network with New Data
RunOnce.pyincludes theRunBasicNetwork()function, which runs the trained HTM Network in inference mode using a given data file. You can invoke the function with the original data or with the new data.
RunOnce.pytests the network first with the training data.accuracy = RunBasicNetwork( bitNet, dataFiles = [trainingFile], categoryFiles = [trainingCategories], resultsFile = trainingResults) print "Training set accuracy with HTM = ", accuracy*100.0
RunOnce.pyalso submits new data to the trained HTM Network and judge how well the network learned the categories.
|
Numenta www.Numenta.com |
![]() ![]() ![]()
|