In case you weren’t aware, htm.core now also provides a REST interface to it’s Network API. With this in place, it is possible to utilize the built-in encoders, Spatial Pooler and Temporal Memory from almost any programming language. In this post I briefly want to touch on how to use htm.core and C# together.
Before you can start playing around with the REST interface, you will need to have the example REST server running.
To do that, you can either
- download the source and build it yourself. Once built, the server executable (rest_server) should be available in the build/Release/bin subdirectory of the htm.core project root folder.
- start a docker container using the htm.core jupyter docker image:
docker run -d -p 8888:8888 -p 8050:8050 –name jupyter 3rdman/htm.core-jupyter:latest
Per default, the server is listening on port 8050.
The Network API is around for quite a while now and the example rest_server implementation basically sits on top of it. Using the API you can define a network of existing encoders and algorithms etc. Each component of such a network is called a region and regions are linked to each other by connecting its outputs/inputs.
Currently, the following regions are supported:
- ScalarEncoderRegion – encodes numeric and category data
- RDSEEncoderRegion – encodes numeric and category data using a hash
- DateEncoderRegion – encodes date and/or time data
- SPRegion – HTM Spatial Pooler implementation
- TMRegion – HTM Temporal Memory implementation
- FileOutputRegion – Writes data to a file
- FileInputRegion – Reads data from a file
- ClassifierRegion – An SDR classifier
If you want to learn more about the different regions available, here’s the documentation.
If you want to use the Network API REST Interface, you first will have to create a new Network via an HTTP POST request. After that use PUT and GET to send/receive parameter values and finally HTTP DELETE to remove the complete network (or parts of it), if no longer needed.
Details about the REST interface are available here.
htm.core and C# REST Examples
To make it easier for you to get started with writing your own htm.core and C# application, I’ve created the following repository on GitHub that contains two basic .NET Core 3.1 examples.
The project is divided up into two main areas/folders.
The actual examples, including the network configuration and REST calls, are located in the Examples directory. See further down for more details.
A lot of communication between the different Hierarchical Temporal Memory components happens by using Sparse Distributed Representation objects. Please let me point you towards the post below, to get an initial overview of the basic components of a HTM.
Therefore, the first thing required for the communication with the REST API is a basic implementation of an SDR in C#. I’ve called it SdrLite, as it isn’t a full port of the original SDR implementation, but instead it just provides the functionality needed for the examples to work. Please feel free to contribute to the project to extend SdrLite, if needed.
This example is inspired by the original htm.core python REST client example authored by David Keeney and, as the name implies, gets the HTM to learn a sine wave pattern.
All the configuration settings are centralized in ExampleConfiguration.cs. So if you want to play around with other settings, or need to change the URL to the REST API, this is the place to go.
This example is an adoption of the python script provided at the end of the following blog post.
It might be a good starting point to understand how the Temporal Algorithm is working, by learning a very simple number sequence.
Like with the previous example, you can adjust the network configuration and other settings in ExampleConfiguration.cs.
Build and run
To run the examples yourself on either Windows, Linux or macOS, you’ll need to have .NET Core 3.1 installed and build the solution by executing the following command in the root folder of the repository:
After a successful build, the executables should be available in the bin/Debug/netcoreapp3.1 sub-folder of the related example project.
That’s it for now. Enjoy and happy coding!