htm.core and C#

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.

Introduction

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.

or

  • 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.

Network API

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.

REST

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.

https://github.com/indy-3rdman/htm.core-csharp-rest-examples

Project Structure

The project is divided up into two main areas/folders.

  • Common
  • Examples

In the Common directory you will find a very basic implementation of an SDR (utilizing NumSharp), along with some related tests, plus the actual REST API client.

The actual examples, including the network configuration and REST calls, are located in the Examples directory. See further down for more details.

SdrLite

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.

SineWave Example

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.

htm.core and C# - SineWave example

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.

UpDownSequence Example

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.

htm.core and C# - UpDownSequence example

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:

dotnet build

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!

1 Comment

  1. […] I worked on a small C# project that needed to consume multi-dimensional NumPy arrays via a REST API. As I wanted the solution to […]

Comments are closed.

Scroll to top