-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwisard.hpp
197 lines (180 loc) · 9.01 KB
/
wisard.hpp
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
//
// wisard.hpp
//
//
// Created by Maurizio Giordano on 20/03/2014
//
//
#ifndef _wisard_h
#define _wisard_h
#define PI 3.1415926535
#include "wnet_lib.hpp"
#include <assert.h>
#include <stdio.h>
extern "C" //Tells the compile to use C-linkage for the next scope.
{
#define BUFSIZE 1024
#define ceiling(X) (X-(int)(X) > 0 ? (int)(X+1) : (int)(X))
typedef unsigned char uchar;
typedef struct wisard wisard_t;
/// discriminator data structure
/**
represent a disciminator and its configuration and size info
*/
typedef struct {
int n_ram; /**< number of rams for the discriminator */
int n_bit; /**< number of bits (resolution) */
wkey_t n_loc; /**< number of location in each ram (minus 1) */
int size; /**< size of input binary image */
unsigned long int tcounter; /**< train counter */
wentry_t **rams; /**< the ram list of disciminator */
wkeymax_t *maxkeys; /**< the list ofmore frequent keys */
int *map; /**< pointer to the retina (mapping) */
int *rmap; /**< pointer to the inverse retina (mapping) */
wvalue_t *mi; /**< pointer to mental image */
wvalue_t maxmi; /**< max mental image value */
char *name; /**< the name of the discriminator */
} discr_t;
/// discriminator data structure
/**
represent a array of discriminators
*/
typedef struct {
int n_ram; /**< number of rams per discriminator */
int n_bit; /**< number of bits (resolution) */
wkey_t n_loc; /**< number of location in each ram (minus 1) */
int size; /**< size of input binary image */
unsigned long int tcounter; /**< train counter */
wentry_t ***rams; /**< the ram matrix of the array discriminator */
wkeymax_t **maxkeys; /**< the matrix of more frequent keys */
int *map; /**< pointer to the retina (mapping) */
int *rmap; /**< pointer to the inverse retina (mapping) */
wvalue_t *mi; /**< pointer to mental image */
wvalue_t maxmi; /**< max mental image value */
char *name; /**< the name of the discriminator */
} discrarray_t;
//! discriminator constructor function.
/*!
\param n_bit number of bits (resolution).
\param size input binary image size.
\param name discriminator name.
\param mode retina creation mode (linear, random).
\return a pointer to the discriminator data structure
*/
discr_t *makeDiscr(int n_bit, int size, char *name, char *mode, unsigned int seed);
discr_t *makeTrainImgDiscr(int n_bit, int size, char *name, char *mode, const void * imgv, int cols, int bx, int by, int width, int tics, unsigned int seed);
discr_t *makeTrainImgBinDiscr(int n_bit, int size, char *name, char *mode, const void * imgv, int cols, unsigned int seed);
discrarray_t *makeDiscrArray(int n_bit, int size, char *name, char *mode, int tics, unsigned int seed);
discrarray_t *makeTrainImgDiscrArray(int n_bit, int size, char *name, char *mode, const void * imgv, int cols, int bx, int by, int width, int tics, unsigned int seed);
//! discriminator copying function.
/*!
\param discr pointer to the discriminato to duplicate
\return a pointer to the copied discriminator
*/
discr_t *copyDiscr(discr_t *discr);
//! discriminator training function.
/*!
\param discr pointer to the discriminato to train
\param in_tuples input tuple for training the discriminator
\return void
*/
void trainDiscr(discr_t *discr, wkey_t *in_tuples);
void trainSvmHistoDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
void trainSvmPointDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
void trainSvmCursorDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
void trainSvmBinDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
void trainImgDiscr(discr_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
void trainImgBinDiscr(discr_t *discr, const void * imgv, int cols);
void trainresetImgDiscr(discr_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
void trainImgDiscrArray(discrarray_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
//! discriminator training and forgetting function.
/*!
\param discr pointer to the discriminato to train
\param in_tuples input tuple for training the discriminator (the rest wil be decremented)
\return void
*/
void trainforgetDiscr(discr_t *discr, wkey_t *in_tuples, wvalue_t incr, wvalue_t decr);
void trainforgettopDiscr(discr_t *discr, wkey_t *in_tuples, wvalue_t incr, wvalue_t decr, wvalue_t top);
void trainforgetImgDiscr(discr_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
void trainforgetImgBinDiscr(discr_t *discr, const void * imgv, int cols);
void trainforgetImgDiscrArray(discrarray_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
//! discriminator classifyning functions.
/*!
\param discr pointer to the discriminato to train
\param in_tuples input tuple for having response from the discriminator
\return the classification response
*/
double classifyDiscr(discr_t *discr, wkey_t *in_tuples);
double classifySvmHistoDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
double classifySvmPointDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
double classifySvmCursorDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
double classifySvmBinDiscr(discr_t *discr, double *data, double *den, double *off, int nt, int nattr);
double classifyImgDiscr(discr_t *discr, const void * imgv, int cols, int bx, int by, int width, int tics);
double classifyImgBinDiscr(discr_t *discr, const void * imgv, int cols);
void classifyImgDiscrArray(discrarray_t *discr, const void * imgv, void * oimgv, int cols, int bx, int by, int width, int tics, double thresh);
/*!
\param discr pointer to the discriminato to train
\param in_tuples input tuple for having response from the discriminator
\param threshold value to enable neuron outputs
\return the classification response
*/
double classifyDiscrThresholded(discr_t *discr, wkey_t *in_tuples, double threshold);
double classifySvmHistoDiscrThresholded(discr_t *discr, double *data, double *den, double *off, int nt, int nattr, double threshold);
double classifySvmPointDiscrThresholded(discr_t *discr, double *data, double *den, double *off, int nt, int nattr, double threshold);
double classifySvmCursorDiscrThresholded(discr_t *discr, double *data, double *den, double *off, int nt, int nattr, double threshold);
double classifySvmBinDiscrThresholded(discr_t *discr, double *data, double *den, double *off, int nt, int nattr, double threshold);
//! discriminator punishing function.
/*!
\param discr pointer to the discriminato to punish
\param in_tuples input tuple for punishing the discriminator
\return void
*/
void punishDiscr(discr_t *discr, wkey_t *in_tuples, wvalue_t);
//! discriminator printing function.
/*!
\param discr pointer to the discriminato to train
\return void
*/
void printDiscr(discr_t *discr);
// ! discriminator mental image update function
/*!
\param discr the discriminator
\return athe max value in the mentalimage
*/
wvalue_t mentalDiscr(discr_t *discr);
// ! Halfing Discriminator Resolution
/*!
\param discr the discriminator to be halfed
\return a new discrminator with half resolution
*/
discr_t *cut_ram(discr_t *discr);
// ! Doubling Discriminator Resolution
/*!
\param discr the discriminator to be halfed
\return a new discrminator with half resolution
*/
discr_t *join_ram(discr_t *discr);
// ! Shrinking Discriminator (for Bleaching)
/*!
\param discr the discriminator to be shrinked
\return void
*/
void shrinkDiscr(discr_t *discr);
// ! Emptying Rams of Discriminator (for Bleaching)
/*!
\param discr the discriminator to be initialized
\return void
*/
void initDiscr(discr_t *discr);
// ! Free Discriminator
/*!
\param discr the discriminator to be initialized
\return void
*/
void freeDiscr(discr_t *discr);
void sink_discriminator(wentry_t **discr, int n_ram);
int *filter(double *ls, int *idx, int n, int *cnt, double factor);
int new_chsize(double *ls, int n, double factor);
int bleaching(int *challengers, wentry_t ***discr_tobleach, int csize, double *responses, wisard_t *wiznet, double factor, wkey_t *in_tuples, double *bres, int flag);
}
#endif