-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMAC.cpp
More file actions
231 lines (211 loc) · 6.98 KB
/
CMAC.cpp
File metadata and controls
231 lines (211 loc) · 6.98 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
#include "CMAC.h"
#include "dataKit.h"
#include <cmath>
#include<vector>
#include<cstdlib>
#include<iostream>
#include<ctime>
#define rand(x) rand()%x/(float)(100*x)
using namespace std;
template <typename T>
vector<vector<T> > transVec(vector<vector<T> > mat){
//转置
vector<vector<T> > tempVec;
int col = mat[0].size();
int row = mat.size();
for(int i = 0; i<col; i++){
vector<T> tmptmpVec;
for(int j = 0; j < row; j++){
tmptmpVec.push_back(mat[j][i]);
}
tempVec.push_back(tmptmpVec);
}
return tempVec;
}
void CMAC::initCMAC(vector<vector<float> > dataSetI, vector<float> labelsI,int numOfTierI/**层数*/
,int numOfQualifyI, int epoch, float learnRate){
CMAC::numOfEpoch = epoch;
printf("processing deal with dataset\n");
CMAC::dataSet = move(dataSetI);
printf("processing deal with labels\n");
CMAC::labels = move(labelsI);
printf("processing deal with tiers\n");
CMAC::numOfTier = numOfTierI;
printf("processing deal with qualify\n");
CMAC::numOfQualify = numOfQualifyI;
CMAC::numOfInput = (int) CMAC::dataSet[0].size();
// init with normalize all data
printf("processing deal with normalization\n");
CMAC::normalization();
// get the standard of qualification matrix
printf("processing deal with get qualify standard\n");
CMAC::getQualVec();
// qualified all data
printf("processing deal with qualify all data\n");
CMAC::getAllQualData();
// learning process
printf("processing deal with learning\n");
for(int i = 0; i < epoch; i++){
CMAC::learnAll(learnRate);
}
CMAC::lookLook();
printf("end processing, successful learning!\n");
}
void CMAC::lookLook(){
printf("-------------------------- parameters ---------------------------\n");
printf("num of tier: %d, learning epoch:%d\n",CMAC::numOfTier, CMAC::numOfEpoch);
printf("--------------------- all data and labels -----------------------\n");
printf("print all data \n");
printVec2D(CMAC::dataSet);
printf("----------------------- qualification ---------------------------\n");
printf("print the qualification rules\n");
for(auto & i : CMAC::qualVec){
for(auto & j : i){
printf("%lu \t",j);
}
printf("\n");
}
printf("print all qualified data:\n");
int count = 0;
for(auto & i : CMAC::qualData){
for(auto & j : i){
printf("[");
for(auto & k : j){
printf("%d ", k);
}
printf("],");
}
printf("label: %f",CMAC::labels[count++]);
printf("\n");
}
printf("------------------------- end with error %f -----------------------------------\n",*(CMAC::errorVec.end()));
}
void CMAC::normalization(){
vector<vector<float> > datasetT = transVec(CMAC::dataSet);
for(auto & v : datasetT){
// recording the min-max scale standard
float min = vecFindMinMax(v, 0);
float max = vecFindMinMax(v, 1);
CMAC::mini.push_back(min);
CMAC::maxi.push_back(max);
v = vecMinMaxScaler(v);
}
datasetT = transVec(datasetT);
CMAC::dataSet = datasetT;
}
void CMAC::getQualVec() {
for(int i = 0; i < CMAC::numOfTier; i++){
vector<int> tmpVec;
CMAC::qualVec.push_back(tmpVec);
}
for(int i = 0; i < CMAC::numOfQualify - 1; i++){
int row = i % CMAC::numOfTier;
CMAC::qualVec[row].push_back(i+1);
}
}
vector<vector<int> > CMAC::get1QualData(vector<float> data) {
vector<vector<int> > result;
for(auto & i : data){
vector<int> tmp;
for(int j = 0; j < CMAC::numOfTier; j++){
int tmpInt = 0;
for(auto & k : CMAC::qualVec[j]){
if (CMAC::numOfQualify*i >= k){
tmpInt++;
}else{
break;
}
}
tmp.push_back(tmpInt);
}
result.push_back(tmp);
}
result = transVec(result);
return result;
}
void CMAC::getAllQualData() {
for(auto & i : CMAC::dataSet){
CMAC::qualData.push_back(get1QualData(i));
}
}
int CMAC::vec2Int(vector<int> myVec, int indexOfTier) {
/*
* first modified myvec into a real num, like 1011,
* to avoid the conflict of 101 of tier 1 and 101 of tier 2,
* we use a bias of indexOfTier * 10^(myVec.size())
* */
int lenth = (int)myVec.size();
int numberAfterModify = (int)pow(10., (float)lenth) * indexOfTier;
for(int i = 0; i < lenth; i++){
numberAfterModify += (int)pow(10., (float)(lenth - i - 1)) * myVec[i];
}
return numberAfterModify;
}
int CMAC::hash(vector<int> myVec, int indexOfTier) {
srand((int)time(0));
int numberAfterModify = CMAC::vec2Int(myVec, indexOfTier);
//printf("%d\n", numberAfterModify);
if (CMAC::storageUnit.find(numberAfterModify) == CMAC::storageUnit.end()){
float randNum = rand(10);
CMAC::storageUnit[numberAfterModify] = randNum;
}
return 0;
}
void CMAC::learnOnce(vector<vector<int>> aData, float label, float learnRate) {
float result = 0.;
float count = (float)aData.size();
int index = 0;
for(auto & i : aData){
int numberAfterModify = CMAC::vec2Int(i, index++);
CMAC::hash(i, index);
result += CMAC::storageUnit[numberAfterModify];
}
float error = label - result;
// printf("error : %f\n", error);
CMAC::errorVec.push_back(error);
index = 0;
for(auto & i : aData){
int numberAfterModify = CMAC::vec2Int(i, index++);
CMAC::storageUnit[numberAfterModify] += learnRate * error / count;
}
}
void CMAC::learnAll(float learnRate) {
int mountOfData = (int) CMAC::qualData.size();
for(int i = 0; i < mountOfData; i++){
// printf("learning %d th data (total %d)\n", i+1, mountOfData);
CMAC::learnOnce(CMAC::qualData[i], CMAC::labels[i], learnRate);
}
}
float CMAC::predict(vector<float> vec, int show) {
vector<float> vec_ = vec;
int lenth = (int)vec_.size();
for(int i = 0; i < lenth; i++){
vec_[i] = (vec_[i] - CMAC::mini[i] )/(CMAC::maxi[i] - CMAC::mini[i]);
}
if(show == 1){
printf("after min max scale: \n");
printVecOneD(vec_);
}
vector<vector<int> > afterQual;
afterQual = CMAC::get1QualData(vec_);
if(show == 1){
printf("after qualification \n");
for(auto & i : afterQual){
printf("[");
for(auto & j : i){
printf("%d ",j);
}
printf("],");
}
}
float result = 0;
int tier = 0;
for(auto & i : afterQual){
int index = CMAC::vec2Int(i, tier++);
result += CMAC::storageUnit[index];
}
if(show==1){
printf("predict result :%f\n", result);
}
return result;
}