-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSCC.h
306 lines (234 loc) · 8.93 KB
/
SCC.h
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef SPARSE_COORDINATE_CODING_H
#define SPARSE_COORDINATE_CODING_H
namespace dpl{
static unsigned int myseed;
double getAbs( double value ){
if( value < 0 )
return -1*value;
else
return value;
}
double **FeatureInitialization( int featureDim, int sampleNumber ){
double **feature = (double**)malloc(sampleNumber*sizeof(double*));
for( unsigned int i=0; i<sampleNumber; i++ ){
feature[i] = (double*)malloc(featureDim*sizeof(double));
for( unsigned int j=0; j<featureDim; j++ )
feature[i][j] = 0;
}
return feature;
}
std::vector<int> *NonZeroIndexInitialization( int sampleNumber ){
std::vector<int> *nonZeroIndex = new std::vector<int> [sampleNumber];
return nonZeroIndex;
}
double ShrinkageFunction( double value, double theta ){
if( value < -theta )
return value+theta;
else if( value > theta )
return value-theta;
else
return 0;
}
double *Initialize_A_Copy( int featureDim ){
double *A_Copy = (double*)malloc(featureDim*sizeof(double));
for( unsigned int i=0; i<featureDim; i++ )
A_Copy[i]=0;
return A_Copy;
}
double *Initialize_A( int featureDim ){
double *A = (double*)malloc(featureDim*sizeof(double));
for( unsigned int i=0; i<featureDim; i++ )
A[i]=0;
return A;
}
void Initialize_A( double *A, double *A_Copy, int featureDim ){
for( unsigned int i=0; i<featureDim; i++ ){
A[i]=A_Copy[i];
A_Copy[i]=0;
}
}
void Update_A( double *A, double *A_Copy, double *feature, std::vector<int> &nonZeroIndex ){
for( unsigned int i=0; i<nonZeroIndex.size(); i++ ){
A[nonZeroIndex[i]] += feature[nonZeroIndex[i]]*feature[nonZeroIndex[i]];
A_Copy[nonZeroIndex[i]] += feature[nonZeroIndex[i]]*feature[nonZeroIndex[i]];
}
}
double getNonNegativeFeature( double featureElement, double optimalT ){
if( featureElement+optimalT>=0 )
return optimalT;
else
return -1*featureElement;
}
int *getRandomIndex( int size ){
std::vector<int> index (size);
int *data=(int*)malloc(size*sizeof(int));
for( unsigned int i=0; i<size; i++ )
index[i] = i;
for( unsigned int i=0; i<size; i++ ){
int randomIndex = rand_r(&myseed)%index.size();
data[i] = index[randomIndex];
index.erase(index.begin()+randomIndex);
}
return data;
}
void UpdateFeature( double **Wd, double *sample, double *residuals, double *feature, std::vector<int> &nonZeroIndex, double lambda, int layers, int featureDim, int sampleDim, bool NonNegative ){
for( unsigned int i = 0; i<sampleDim; i++ ){
residuals[i] = -sample[i];
for( unsigned int j = 0; j<nonZeroIndex.size(); j++ )
residuals[i] += Wd[nonZeroIndex[j]][i]*feature[nonZeroIndex[j]];
}
nonZeroIndex.resize(0);
int *randomIndex = getRandomIndex(featureDim );
for ( unsigned int i = 0; i < featureDim; i++ ){
double optimalT;
double derivative = 0;
for (unsigned int j = 0;j < sampleDim; j++)
derivative += (residuals[j]*Wd[randomIndex[i]][j]);
optimalT = ShrinkageFunction( feature[randomIndex[i]]-derivative, lambda )-feature[randomIndex[i]];
if( NonNegative )
optimalT = getNonNegativeFeature( feature[randomIndex[i]], optimalT );
feature[randomIndex[i]] += optimalT;
if ( optimalT!=0 ){
for (unsigned int j = 0;j < sampleDim; j++)
residuals[j] += optimalT*Wd[randomIndex[i]][j];
}
if( feature[randomIndex[i]]!=0 )
nonZeroIndex.push_back(randomIndex[i]);
}
for ( unsigned int k = 1; k < layers; k++ ){
for ( unsigned int i = 0; i < nonZeroIndex.size(); i++ ){
double optimalT;
double derivative = 0;
for (unsigned int j = 0;j < sampleDim; j++)
derivative += (residuals[j]*Wd[nonZeroIndex[i]][j]);
optimalT = ShrinkageFunction( feature[nonZeroIndex[i]]-derivative, lambda )-feature[nonZeroIndex[i]];
if( NonNegative )
optimalT = getNonNegativeFeature( feature[nonZeroIndex[i]], optimalT );
feature[nonZeroIndex[i]] += optimalT;
if ( optimalT!=0 ){
for (unsigned int j = 0;j < sampleDim; j++)
residuals[j] += optimalT*Wd[nonZeroIndex[i]][j];
}
}
}
nonZeroIndex.resize(0);
for ( unsigned int i = 0; i < featureDim; i++ ){
if( feature[i]!=0 )
nonZeroIndex.push_back(i);
}
free(randomIndex);
}
void UpdateWd( double **Wd, double *residuals, double *feature, double *A, std::vector<int> &nonZeroIndex, int sampleDim, bool NonNegative ){
for ( unsigned int i = 0; i < sampleDim; i++ ){
for ( unsigned int j = 0; j < nonZeroIndex.size(); j++ ){
if( NonNegative && Wd[nonZeroIndex[j]][i] - feature[nonZeroIndex[j]]*residuals[i]*dpl::learningRate(A,nonZeroIndex[j])<0 )
Wd[nonZeroIndex[j]][i] = 0;
else
Wd[nonZeroIndex[j]][i] = Wd[nonZeroIndex[j]][i] - feature[nonZeroIndex[j]]*residuals[i]*dpl::learningRate(A,nonZeroIndex[j]);
}
}
}
void NormalizeWd( double **Wd, std::vector<int> &nonZeroIndex, int sampleDim ){
for( unsigned int i=0; i<nonZeroIndex.size(); i++ ){
double sum = 0;
for( unsigned int j=0; j<sampleDim; j++ )
sum += Wd[nonZeroIndex[i]][j]*Wd[nonZeroIndex[i]][j];
sum = sqrt(sum);
if( sum!=0 ){
for( unsigned int j=0; j<sampleDim; j++ )
Wd[nonZeroIndex[i]][j] = Wd[nonZeroIndex[i]][j]/sum;
}
}
}
void saveFeature( double **feature, char *FeatureFileName, int featureDim, int sampleNumber ){
printf("Save Features in %s\n", FeatureFileName);
FILE *fp;
fp = fopen( FeatureFileName, "w");
if( fp == NULL ){
printf("could not find feature file %s\n", FeatureFileName);
exit(0);
}
for( unsigned int i=0; i<featureDim; i++ ){
for( unsigned int j=0; j<sampleNumber; j++)
fprintf(fp, "%.15lf ", feature[j][i]);
fprintf(fp, "\n");
}
fclose(fp);
}
void saveNonZeroIndex( std::vector<int> *nonZeroIndex, char *IndexFileName, int featureDim, int sampleNumber ){
printf("Save nonZero index in %s\n", IndexFileName);
FILE *fp;
fp = fopen( IndexFileName, "w");
if( fp == NULL ){
printf("could not find index file %s\n", IndexFileName);
exit(0);
}
for( unsigned int i=0; i<sampleNumber; i++ ){
for( unsigned int j=0; j<nonZeroIndex[i].size(); j++)
fprintf(fp, "%d ", nonZeroIndex[i][j]);
fprintf(fp, "\n");
}
fclose(fp);
}
void clearFeature( int sampleNumber, double **feature ){
for( unsigned int i=0; i<sampleNumber; i++ )
free(feature[i]);
free(feature);
}
double computeLassoResult( double **Wd, double *sample, double *feature, double lambda, int sampleDim, int featureDim ){
double LassoResult = 0;
double residuals;
for( unsigned int i=0; i<sampleDim; i++ ){
residuals = -sample[i];
for( unsigned int j=0; j<featureDim; j++ )
residuals += Wd[j][i]*feature[j];
LassoResult += residuals*residuals;
}
double sum_feature = 0;
for( unsigned int j=0; j<featureDim; j++ )
sum_feature += getAbs(feature[j]);
return 0.5*LassoResult+lambda*sum_feature;
}
void calculateError( double **Wd, double **sample, double **feature, double lambda, int sampleNumber, int sampleDim, int featureDim ) {
double TotalDecError = 0;
for( unsigned int t=0; t<sampleNumber; t++ ){
TotalDecError += computeLassoResult( Wd, sample[t], feature[t], lambda, sampleDim, featureDim);
}
TotalDecError /= sampleNumber;
std::cout<<"Total Decode Error is "<<TotalDecError<<std::endl;
}
void trainDecoder( double **Wd, double **feature, double **sample, double lambda, int layers, int featureDim, int sampleNumber, int sampleDim, int iterationNumber, bool NonNegative ){
double *residuals = (double*)malloc(sampleDim*sizeof(double));
double *A = Initialize_A( featureDim );
double *A_Copy = Initialize_A_Copy( featureDim );
std::vector<int> *nonZeroIndex = NonZeroIndexInitialization( sampleNumber );
srand((unsigned)time(0));
myseed = (unsigned int) RAND_MAX * rand();
std::cout<<"Train decoder"<<std::endl;
double ComputionalTime = 0;
clock_t BeginTime = clock();
for( unsigned int it=0; it<iterationNumber; it++ ){
int index = it%sampleNumber;
if( index==0 )
Initialize_A( A, A_Copy, featureDim );
UpdateFeature( Wd, sample[index], residuals, feature[index], nonZeroIndex[index], lambda, layers, featureDim, sampleDim, NonNegative );
Update_A( A, A_Copy, feature[index], nonZeroIndex[index] );
UpdateWd( Wd, residuals, feature[index], A, nonZeroIndex[index], sampleDim, NonNegative );
NormalizeWd( Wd, nonZeroIndex[index], sampleDim );
if( it%sampleNumber==sampleNumber-1 ){
std::cout<<it+1<<" iterations finished"<<std::endl;
//calculateError(Wd, sample, feature, lambda, sampleNumber, sampleDim, featureDim);
}
}
clock_t EndTime = clock();
ComputionalTime = (double)(EndTime-BeginTime)/CLOCKS_PER_SEC;
std::cout<<"Finish decoding process:"<<std::endl;
std::cout<<"Train Decode Time is "<<ComputionalTime<<" seconds."<<std::endl;
calculateError( Wd, sample, feature, lambda, sampleNumber, sampleDim, featureDim );
free(A_Copy);
free(A);
free(residuals);
delete [] nonZeroIndex;
}
}
#endif /* Sparse Coordinate Coding */