-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTsetlinMachine.c
More file actions
289 lines (224 loc) · 10.2 KB
/
Copy pathTsetlinMachine.c
File metadata and controls
289 lines (224 loc) · 10.2 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright (c) 2026 Ole-Christoffer Granmo and the University of Agder
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 code implements the Tsetlin Machine from paper arXiv:1804.01508
https://arxiv.org/abs/1804.01508
*/
#include <stdio.h>
#include <stdlib.h>
#include "TsetlinMachine.h"
/**************************************/
/*** The Multiclass Tsetlin Machine ***/
/**************************************/
/*** Initialize Tsetlin Machine ***/
struct TsetlinMachine *CreateTsetlinMachine()
{
struct TsetlinMachine *tm = (void *)malloc(sizeof(struct TsetlinMachine));
/* Set up the Tsetlin Machine structure */
tm_initialize(tm);
return tm;
}
void tm_initialize(struct TsetlinMachine *tm)
{
for (int i = 0; i < CLAUSES; i++) {
for (int j = 0; j < ROOT_FACTORS; j++) {
for (int k = 0; k < INTERIOR_ALTERNATIVES; k++) {
for (int l = 0; l < INTERIOR_FACTORS; l++) {
for (int m = 0; m < LEAF_ALTERNATIVES; m++) {
for (int n = 0; n < LEAF_FACTORS; n++) {
if (1.0 * rand()/RAND_MAX <= 0.5) {
(*tm).ta_state[i][j][k][l][m][n] = NUMBER_OF_STATES;
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] = NUMBER_OF_STATES + 1;
} else {
(*tm).ta_state[i][j][k][l][m][n] = NUMBER_OF_STATES + 1;
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] = NUMBER_OF_STATES;
}
}
}
}
}
}
}
}
/* Translates automata state to action */
static inline int action(int state)
{
return state > NUMBER_OF_STATES;
}
/* Calculate the output of each clause using the actions of each Tsetline Automaton. */
/* Output is stored an internal output array. */
static inline void calculate_clause_output(struct TsetlinMachine *tm, int Xi[], int predict)
{
int action_include;
for (int i = 0; i < CLAUSES; i++) {
(*tm).clause_output[i] = 1;
for (int j = 0; j < ROOT_FACTORS; j++) {
// Evaluates interior subtrees, adding up votes from each
(*tm).interior_vote_sums[i][j] = 0;
for (int k = 0; k < INTERIOR_ALTERNATIVES; k++) {
// Multiplies vote sums from multiple leaf groups (AND-combination)
(*tm).interior_vote_products[i][j][k] = 1; // Stores how many class votes you get per interior alternative (product of leaf vote sums)
for (int l = 0; l < INTERIOR_FACTORS; l++) {
// Evaluates leaf alternatives (clause components), adding up the votes
(*tm).leaf_vote_sum[i][j][k][l] = 0; // Stores how many class votes you get per feature group (vote summation over leaf alternatives)
for (int m = 0; m < LEAF_ALTERNATIVES; m++) {
// Evaluates clause component on its feature group
(*tm).clause_component_output[i][j][k][l][m] = 1;
for (int n = 0; n < LEAF_FACTORS; n++) {
int feature = j * INTERIOR_FACTORS * LEAF_FACTORS + l * LEAF_FACTORS + n;
action_include = action((*tm).ta_state[i][j][k][l][m][n]);
if ((action_include == 1 && Xi[feature] == 0)) {
(*tm).clause_component_output[i][j][k][l][m] = 0;
break;
}
action_include = action((*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS]);
if ((action_include == 1 && Xi[feature + FEATURES] == 0)) {
(*tm).clause_component_output[i][j][k][l][m] = 0;
break;
}
}
// Adds up clause component votes (OR-alternatives)
(*tm).leaf_vote_sum[i][j][k][l] += (*tm).clause_component_output[i][j][k][l][m];
}
// Multiplies leaf vote sums (AND-grouping).
(*tm).interior_vote_products[i][j][k] *= (*tm).leaf_vote_sum[i][j][k][l];
}
// Adds up interior leaf vote sum products (OR-alternatives)
(*tm).interior_vote_sums[i][j] += (*tm).interior_vote_products[i][j][k];
}
// Multiplies interior vote sums (AND-grouping).
(*tm).clause_output[i] *= (*tm).interior_vote_sums[i][j];
}
}
}
/* Sum up the votes for each class (this is the multiclass version of the Tsetlin Machine) */
static inline int sum_up_class_votes(struct TsetlinMachine *tm)
{
int class_sum = 0;
for (int i = 0; i < CLAUSES; i++) {
int sign = 1 - 2 * (i & 1);
class_sum += (*tm).clause_output[i]*sign;
}
class_sum = (class_sum > THRESHOLD) ? THRESHOLD : class_sum;
class_sum = (class_sum < -THRESHOLD) ? -THRESHOLD : class_sum;
return class_sum;
}
/* Get the state of a specific automaton, indexed by clause, feature, and automaton type (include/include negated). */
int tm_get_state(struct TsetlinMachine *tm, int clause, int root_factor, int interior_alternative, int interior_factor, int leaf_alternative, int leaf_factor)
{
return (*tm).ta_state[clause][root_factor][interior_alternative][interior_factor][leaf_alternative][leaf_factor];
}
/*************************************************/
/*** Type I Feedback (Combats False Negatives) ***/
/*************************************************/
static inline void type_i_feedback(struct TsetlinMachine *tm, int Xi[], int i, int j, int k, int l, int m, float s)
{
if ((*tm).clause_output[i] == 0 || (*tm).interior_vote_products[i][j][k] == 0 || (*tm).clause_component_output[i][j][k][l][m] == 0) {
for (int n = 0; n < LEAF_FACTORS; n++) {
(*tm).ta_state[i][j][k][l][m][n] -= ((*tm).ta_state[i][j][k][l][m][n] > 1) && (1.0*rand()/RAND_MAX <= 1.0/s);
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] -= ((*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] > 1) && (1.0*rand()/RAND_MAX <= 1.0/s);
}
} else {
int feature_index = j * INTERIOR_FACTORS * LEAF_FACTORS + l * LEAF_FACTORS;
for (int n = 0; n < LEAF_FACTORS; n++) {
if (Xi[feature_index + n] == 1) {
(*tm).ta_state[i][j][k][l][m][n] += ((*tm).ta_state[i][j][k][l][m][n] < NUMBER_OF_STATES*2) && (BOOST_TRUE_POSITIVE_FEEDBACK == 1 || 1.0*rand()/RAND_MAX <= (s-1)/s);
} else {
(*tm).ta_state[i][j][k][l][m][n] -= ((*tm).ta_state[i][j][k][l][m][n] > 1) && (1.0*rand()/RAND_MAX <= 1.0/s);
}
if (Xi[feature_index + n + FEATURES] == 1) {
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] += ((*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] < NUMBER_OF_STATES*2) && (BOOST_TRUE_POSITIVE_FEEDBACK == 1 || 1.0*rand()/RAND_MAX <= (s-1)/s);
} else {
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] -= ((*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] > 1) && (1.0*rand()/RAND_MAX <= 1.0/s);
}
}
}
}
/**************************************************/
/*** Type II Feedback (Combats False Positives) ***/
/**************************************************/
static inline void type_ii_feedback(struct TsetlinMachine *tm, int Xi[], int i, int j, int k, int l, int m) {
int action_include;
if ((*tm).clause_output[i] > 0 && (*tm).interior_vote_products[i][j][k] > 0 && (*tm).clause_component_output[i][j][k][l][m] == 1) {
int feature_index = j * INTERIOR_FACTORS * LEAF_FACTORS + l * LEAF_FACTORS;
for (int n = 0; n < LEAF_FACTORS; n++) {
action_include = action((*tm).ta_state[i][j][k][l][m][n]);
(*tm).ta_state[i][j][k][l][m][n] += (action_include == 0 && (*tm).ta_state[i][j][k][l][m][n] < NUMBER_OF_STATES*2) && (Xi[feature_index + n] == 0);
action_include = action((*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS]);
(*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] += (action_include == 0 && (*tm).ta_state[i][j][k][l][m][n + LEAF_FACTORS] < NUMBER_OF_STATES*2) && (Xi[feature_index + n + FEATURES] == 0);
}
}
}
/******************************************/
/*** Online Training of Tsetlin Machine ***/
/******************************************/
// The Tsetlin Machine can be trained incrementally, one training example at a time.
// Use this method directly for online and incremental training.
void tm_update(struct TsetlinMachine *tm, int Xi[], int target, float s) {
/*******************************/
/*** Calculate Clause Output ***/
/*******************************/
calculate_clause_output(tm, Xi, UPDATE);
/***************************/
/*** Sum up Clause Votes ***/
/***************************/
int class_sum = sum_up_class_votes(tm);
/*************************************/
/*** Calculate Feedback to Clauses ***/
/*************************************/
// Calculate feedback to clauses
for (int i = 0; i < CLAUSES; i++) {
int sign = 1 - 2 * (i & 1);
for (int j = 0; j < ROOT_FACTORS; j++) {
for (int k = 0; k < INTERIOR_ALTERNATIVES; k++) {
for (int l = 0; l < INTERIOR_FACTORS; l++) {
for (int m = 0; m < LEAF_ALTERNATIVES; m++) {
(*tm).feedback_to_components[i][j][k][l][m] = sign*(2*target-1)*(1.0*rand()/RAND_MAX <= (1.0/(THRESHOLD*2))*(THRESHOLD + (1 - 2*target)*class_sum));
}
}
}
}
}
/*********************************/
/*** Train Individual Automata ***/
/*********************************/
for (int i = 0; i < CLAUSES; i++) {
for (int j = 0; j < ROOT_FACTORS; j++) {
for (int k = 0; k < INTERIOR_ALTERNATIVES; k++) {
for (int l = 0; l < INTERIOR_FACTORS; l++) {
for (int m = 0; m < LEAF_ALTERNATIVES; m++) {
if ((*tm).feedback_to_components[i][j][k][l][m] > 0) {
type_i_feedback(tm, Xi, i, j, k, l, m, s);
} else if ((*tm).feedback_to_components[i][j][k][l][m] < 0) {
type_ii_feedback(tm, Xi, i, j, k, l, m);
}
}
}
}
}
}
}
int tm_score(struct TsetlinMachine *tm, int Xi[]) {
/*******************************/
/*** Calculate Clause Output ***/
/*******************************/
calculate_clause_output(tm, Xi, PREDICT);
/***************************/
/*** Sum up Clause Votes ***/
/***************************/
return sum_up_class_votes(tm);
}