Skip to content

A sample workflow with the REPL

mrcslws edited this page Nov 7, 2014 · 18 revisions

Let's use the REPL to run the isolated-1d demo (blog post, comportexviz).

As described in README.md, start a REPL:

lein repl

Now, in the REPL, let's load the demo code, referring its public vars into the current namespace:

(use 'org.nfrac.comportex.demos.isolated-1d)

We're now equipped to create a model:

(def model (n-region-model 2))

You can explore the model. You probably shouldn't print the whole thing at once. Instead, explore it with keys.

user> (keys model)
(:ff-deps-map :fb-deps-map :strata :inputs-map :regions-map :uuid->id)
user> (keys (:regions-map model))
(:r0 :r1)
user> (keys (:r0 (:regions-map model)))
(:column-field :layer-3 :uuid :step-counter)
user> (keys (:column-field (:r0 (:regions-map model))))
(:spec :ff-sg :topology :input-topology :overlaps :sig-overlaps :prox-exc :inh-radius :boosts :active-duty-cycles :overlap-duty-cycles)
user> (keys (:layer-3 (:r0 (:regions-map model))))
(:spec :topology :distal-sg :active-cols :burst-cols :active-cells :learn-cells :signal-cells :tp-cells :prior-active-cells :prior-learn-cells :pred-cells :prior-pred-cells :distal-exc :tp-exc)

Before we move on and run the model, let's examine the input. To get pprint to work, you may need to (use 'clojure.pprint).

user> (pprint (initial-input))
{:run-0-5 {:seq [0 1 2 3 4 5], :index nil},
 :org.nfrac.comportex.demos.isolated-1d/current-pattern-index 0,
 :twos {:seq [0 2 4 6 8 10 12 14], :index nil},
 :org.nfrac.comportex.demos.isolated-1d/gap-countdown nil,
 :saw-10-15 {:seq [10 12 11 13 12 14 13 15], :index nil},
 :rev-5-1 {:seq [5 4 3 2 1], :index nil},
 :jump-6-12 {:seq [6 7 8 11 12], :index nil},
 :run-6-10 {:seq [6 7 8 9 10], :index nil}}
user> (pprint (input-transform (initial-input)))
{:run-0-5 {:seq [0 1 2 3 4 5], :index 0},
 :org.nfrac.comportex.demos.isolated-1d/current-pattern-index 0,
 :twos {:seq [0 2 4 6 8 10 12 14], :index nil},
 :org.nfrac.comportex.demos.isolated-1d/gap-countdown nil,
 :saw-10-15 {:seq [10 12 11 13 12 14 13 15], :index nil},
 :rev-5-1 {:seq [5 4 3 2 1], :index nil},
 :jump-6-12 {:seq [6 7 8 11 12], :index nil},
 :run-6-10 {:seq [6 7 8 9 10], :index nil}}

Erm, let's use some of Clojure's firepower to help us out. (use 'clojure.data). Now you can diff.

user> (pprint (diff (initial-input) (input-transform (initial-input))))
({:run-0-5 {:index nil}}
 {:run-0-5 {:index 0}}
 {:run-0-5 {:seq [0 1 2 3 4 5]},
  :org.nfrac.comportex.demos.isolated-1d/current-pattern-index 0,
  :twos {:seq [0 2 4 6 8 10 12 14], :index nil},
  :org.nfrac.comportex.demos.isolated-1d/gap-countdown nil,
  :saw-10-15 {:seq [10 12 11 13 12 14 13 15], :index nil},
  :rev-5-1 {:seq [5 4 3 2 1], :index nil},
  :jump-6-12 {:seq [6 7 8 11 12], :index nil},
  :run-6-10 {:seq [6 7 8 9 10], :index nil}})

The first two maps represent the diff, and the third represents the parts that are the same.

Let's set ourselves up for faster comparison.

user> (defn ntransforms [n] (reduce (fn [a _] (input-transform a)) (initial-input) (range n)))

We can now perform the previous diff with (pprint (diff (ntransforms 0) (ntransforms 1))). Explore with different numbers. You can start to see how this maps to the input seen in comportexviz.

Let's run the model.

user> (use 'org.nfrac.comportex.protocols)
user> (def model-t1 (htm-step model))

You can probe into model-t1, similar to above.

Let's use a similar helper function to run lots of steps at once.

user> (defn steps [model n] (reduce (fn [sofar _] (htm-step sofar)) model (range n)))
user> (def model-t2000 (steps model-t1 1999))

Wait a few seconds for it to compute. And then explore.

user> (pprint (:prior-pred-cells (:layer-3 (:r0 (:regions-map model-t2000)))))
#{[78 6] [251 1] [159 2] [188 0] [104 5] [202 2] [167 2] [115 7]
  [196 2] [150 3] [139 1] [88 4] [70 5] [60 3] [64 0] [213 4] [240 3]
  [99 2] [208 4] [42 2] [226 1] [92 2] [129 0]}
nil
user> (pprint (:active-cells (:layer-3 (:r0 (:regions-map model-t2000)))))
#{[78 6] [251 1] [159 2] [188 0] [202 2] [167 2] [115 7] [196 2]
  [150 3] [139 1] [88 4] [70 5] [60 3] [64 0] [213 4] [240 3] [99 2]
  [208 4] [42 2] [226 1] [92 2] [129 0]}
nil
user> (pprint (:burst-cols (:layer-3 (:r0 (:regions-map model-t2000)))))
#{}
nil

Every active cell was predicted in the previous timestep (among others), so no columns were bursted.

Hopefully this page has helped you:

  1. Get an idea of where to start with this API
  2. See how useful it is to have visualizations like comportexviz, and not just a REPL :)
Clone this wiki locally