Jocelyn Paine

Dr. Dobb's Bloggers

Binary Holographic Reduced Representations in SWI-Prolog

August 25, 2008

It's a forbidding title: what is a "binary holographic reduced representation"? Answer: a lovely way to do analogical reasoning on information encoded as high-dimensional vectors. I've just put an implementation for Jan Wielemaker's SWI-Prolog which you can download from my Web site. In this posting, I'll explain about it and HRRs, ending with a session listing where you can see Prolog answering the question "What is the Paris of Sweden?"

It's a forbidding title: what is a "binary holographic reduced representation"? Answer: a lovely way to do analogical reasoning on information encoded as high-dimensional vectors. I've just put an implementation for Jan Wielemaker's SWI-Prolog which you can download from my Web site. In this posting, I'll explain about it and HRRs, ending with a session listing where you can see Prolog answering the question "What is the Paris of Sweden?"

The idea of HRRs is to represent symbolic information by high-dimensional vectors, and use these vectors for analogical reasoning. In real life, one would make this efficient by using special hardware; my Prolog implementation is not meant to be efficient, but only to demonstrate principles. There are several versions of HRR, developed by different researchers: because it was easy to implement, I decided to use the one from Kanerva's paper.

Vector functions, and some algebraic properties

In Kanerva's paper, and in my code, vectors are binary, each component being 0 or 1. The fundamental functions are:

⊗;

merge;

clean;

dot.

There is also a function for generating random vectors, each component of which has an equal probability of being 0 or 1. Other versions of HRR have analogous functions.

To understand how HRRs work, you have to know their algebraic properties, and use these in simplifying "HRR equations". If you're not familiar with ideas such as commutativity and associativity, it doesn't matter — you can see HRRs at work by skipping to the Prolog session at the end of this post.

Using ⊗ to store a field in, and select it from, a record

With these in mind, suppose we have two vectors called paris and capital. We define a third vector:

france = capital ⊗ paris.

Then:

capital ⊗ france = capital ⊗ capital ⊗ paris

= paris.

By commutativity and associativity, france ⊗ capital is also paris.

The point is that we can regard france as a data structure with a capital field whose value is paris. (When I use these names, I mean the vectors they denote.) Then applying ⊗ to france and capital acts as a field selector, and extracts the value paris.

Symmetry between field selectors and fillers

The above reasoning would work the same way for paris ⊗ france, returning capital. Although I may want to treat capital as a field selector and paris as its value, nothing about the vectors themselves forces us to treat one as essentially different from the other. I shan't say much more about this symmetry, but it is an important point of HRR research.

Storing more than one field

Now suppose we have six vectors: capital, paris, location, we (standing for "Western Europe"), money, and euro. And let us now define france by a more complicated equation:

france = merge( capital ⊗ paris, location ⊗ we, money ⊗ euro ).

You can see from the names that we're building a record structure.

Selecting when there is more than one field

What happens if we ⊗ with capital, as before? We get these equations. The second follows because ⊗ distributes over merge, and the third because ⊗ is self-inverse:

Using dot product and "clean-up" to discard irrelevant data

This is interesting because there are more properties of the vector functions which are important.

First, we assume that A dot B, the vector dot-product, measures how similar A is to B. So if A dot B > A dot C, A is more like B than it is like C.

We also assume that our implementation has an "item memory" or "clean-up memory" that stores all vectors that act as symbols we shall want to recognise. In the above example, these would be capital, paris, location, we, money, and euro.

The point of the item memory is that in a real-life implementation in hardware, and given a vector V, we could very quickly test which vector in the memory V is most similar to. This is "clean up".

Because of the third line and the fact that merge( A, B ) is similar to both A and to B, capital ⊗ france is similar to paris, and to capital ⊗ location ⊗ we, and to capital ⊗ money ⊗ euro.

How this relates to the item memory

I said earlier that in this example, the item memory holds capital, paris, location, we, money, and euro.

Assume also that these six vectors have been randomly generated, so that they are extremely unlikely to be similar to one another. This is a crucial assumption, part of any HRR implementation. It is also why we use high-dimensional vectors: the higher the dimension, the more room there is for random vectors to be unlike one another.

Then if we search the item memory for capital ⊗ france, paris will be the most similar to it. The vectors capital ⊗ location ⊗ we and capital ⊗ money ⊗ euro are similar to capital ⊗ france too, but they're not in the item memory, so a search won't find them.

Analogical reasoning

By extending the example, we can do some interesting analogical reasoning. Let's introduce some new vectors:

sweden = merge( capital ⊗ stockholm, location ⊗ nwe, money ⊗ krona ).

(I am using nwe to stand for North-West Europe.)

Now, what is

sweden ⊗ (france ⊗ paris) ?

Well, france ⊗ paris is similar to capital, by the same reasoning as above. And then if we ⊗ that with sweden, and clean up the result, we should get stockholm. We have used HRRs to solve the analogy problem "what is the Paris of Sweden"? This is one of the examples in Kanerva's paper, and it's why researchers find HRRs so appealing: because of their promise for efficient analogical reasoning.

A session with HRRs in Prolog

The listing below shows a sample Prolog session with the predicates. It ends with two examples from Kanerva's paper: storing information about France, and answering the questions "what is the Paris of Sweden?" and "what is the Krona of France?" Note that for the ⊗ function, I use two names: bind, and probe. These do exactly the same as one another, but the names make it easier to see whether one is storing data in, or retrieving it from, a vector.

In this listing, queries that I've typed begin with ?- and are in normal font. Output is also normal font, not starting with ?-. My comments are in italics. Devotees of SWI-Prolog will notice that I've removed a few pieces of output: the 'true' after some queries, and the query numbers.

Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.6.58)

Copyright (c) 1990-2008 University of Amsterdam.

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to redistribute it under certain conditions.

Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- [paths].

% paths compiled 0.00 sec, 1,104 bytes true.

<em>Loads search paths used by the modules when referring to other modules.</em>

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!