-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtemporalmemory.c
186 lines (128 loc) · 4.8 KB
/
temporalmemory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
temporalmemory.c
Elementary Temporal Memory and Command Line Tool Wrapper
Copyright (c) 2022-2024 Peter Overmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
This is a command line wrapper for the elementary Temporal Memory algorithm published in
https://github.com/PeterOvermann/Writings/blob/main/TriadicMemory.pdf
Temporal Memory processes a temporal stream of sparse distributed representations (SDRs),
at each step predicting the next step, based on items seen before.
This algorithm can be used to predict the next step in a continuous stream of items, or to
learn individual sequences terminated by a zero-value SDR.
This command line tool instantiates a new instance of a Temporal Memory.
An SDR is given by a set of p integers in the range from 1 to n.
Typical values are n = 1000 and p = 10 to 20.
Command line arguments: temporalmemory <n> <p>
Command line usage:
29 129 238 356 451 457 589 620 657 758
Print random SDR:
random
Print version number:
version
Terminate process:
quit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include "triadicmemory.h"
// ---------- Temporal Memory ----------
typedef struct
{
TriadicMemory *M1, *M2;
SDR *x, *y, *c, *u, *v, *prediction;
} TemporalMemory;
TemporalMemory* temporalmemory_new (int n, int p); // constructor
SDR* temporalmemory (TemporalMemory *, SDR *); // predictor
// ---------- Temporal Memory ----------
TemporalMemory* temporalmemory_new (int n, int p)
{
TemporalMemory *T = malloc( sizeof(TemporalMemory));
T->M1 = triadicmemory_new(n, p);
T->M2 = triadicmemory_new(n, p);
T->x = sdr_new(n); // persistent circuit state variables
T->y = sdr_new(n);
T->c = sdr_new(n);
T->u = sdr_new(n);
T->v = sdr_new(n);
T->prediction = sdr_new(n);
return T;
}
SDR* temporalmemory (TemporalMemory *T, SDR *inp)
{
// flush state variables if input is zero -- needed for usage as sequence memory
if (inp->p == 0)
{
T->y->p = T->c->p = T->u->p = T->v->p = T->prediction->p = 0;
return T->prediction;
}
sdr_or (T->x, T->y, T->c);
sdr_set(T->y, inp);
if ( ! sdr_equal (T->prediction, T->y) )
// less aggressive test: if ( sdr_overlap (T->prediction, T->y) < T->M2->p)
triadicmemory_write( T->M2, T->u, T->v, T->y );
triadicmemory_read_z (T->M1, T->x, T->y, T->c); // recall c
triadicmemory_read_x (T->M1, T->u, T->y, T->c); // recall u
if (sdr_overlap(T->x, T->u) < T->M1->px)
{
sdr_random( T->c, T->M1->pz);
triadicmemory_write( T->M1, T->x, T->y, T->c);
}
return triadicmemory_read_z (T->M2, sdr_set(T->u, T->x), sdr_set(T->v, T->y), T->prediction);
// important: the return value is used in the next iteration and should not be changed by
// the embedding function
}
static int VERSIONMAJOR = 1;
static int VERSIONMINOR = 1;
int main(int argc, char *argv[])
{
char inputline[10000];
if (argc != 3)
{
printf("usage: temporalmemory <n> <p>\n");
printf("n is the hypervector dimension (typical value 1000)\n");
printf("p is the target sparse population (typical value 10 to 20)\n");
exit(1);
}
int N, P; // SDR dimension and target sparse population, received from command line
sscanf( argv[1], "%d", &N);
sscanf( argv[2], "%d", &P);
TemporalMemory *T = temporalmemory_new (N, P);
SDR *inp = sdr_new(N);
SDR *out = sdr_new(N);
while ( fgets(inputline, sizeof(inputline), stdin) != NULL)
{
if (! strcmp(inputline, "quit\n"))
exit(0);
if (! strcmp(inputline, "random\n"))
sdr_print(sdr_random(out, P));
else if ( strcmp(inputline, "version\n") == 0)
printf("temporalmemory %d.%d\n", VERSIONMAJOR, VERSIONMINOR);
else // parse input SDR
{
if (* sdr_parse(inputline, inp) )
{
printf("unexpected input: %s", inputline);
exit(5);
}
sdr_print( temporalmemory(T, inp));
}
}
return 0;
}