-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_search_away.c
executable file
·187 lines (161 loc) · 5.47 KB
/
line_search_away.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
187
/*
Copyright 2015 Kuan Liu & Aurelien Bellet
This file is part of HDSL.
HDSL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HDSL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HDSL. If not, see <http://www.gnu.org/licenses/>.
#include "mex.h"
#include <stdint.h>
#include <math.h>
*/
#include "mex.h"
#include <stdint.h>
#include <math.h>
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#define SHL( a ) ( ((a) >= 1) ? 0 : ( ((a) <= 0) ? 0.5 - (a) : 0.5*(1-(a))*(1-(a)) ) ) /* computes smoothed hinge loss*/
#define SHL_gradls_away( AtM, AtB, alpha ) ( ( ((1+(alpha))*(AtM)-(alpha)*(AtB)) >= 1 ) ? 0 : ( ( ((1+(alpha))*(AtM)-(alpha)*(AtB)) <= 0 ) ? (AtB)-(AtM) : (1-((1+(alpha))*(AtM)-(alpha)*(AtB)))*((AtB)-(AtM)) ) ) /* computes gradient for line search*/
/* get value corresponding to element (i,j) in sparse matrix
i,j start at 1
based on binary search
*/
double getValue (mwIndex *Ir, mwIndex *Jc, double *Pr, int i, int j) {
int k, left, right, mid, cas;
left = Jc[j-1];
right = Jc[j]-1;
while (left <= right) {
mid = left + (right-left)/2;
if (Ir[mid]+1 == i)
return Pr[mid];
else if (Ir[mid]+1 > i)
right = mid - 1;
else
left = mid + 1;
}
return 0.0;
}
/* input 1: AtM
input 2: Cons
input 3: data (only 2 features, in sparse format)
input 4: signF
input 5: scale
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int numCons, i, xIdx, yIdx, zIdx , debug = 0;
uint64_t *Cons;
double *AtM, *AtMNew, *dataPr, *best_alpha, *val, *violation, *losses;
mwIndex *dataJc, *dataIr;
double alpha, coef, scale, signF, x1, x2, yz1, yz2, obj, obja, objb, grada, gradb, gradm, a, b, m;
if (nrhs != 7)
mexErrMsgTxt("Wrong number of input arguments.");
if (nlhs > 4)
mexErrMsgTxt("Too many output arguments.");
if (!mxIsSparse(prhs[2]))
mexErrMsgTxt("Data must be in sparse format.");
AtM = mxGetPr(prhs[0]);
Cons = (uint64_t *)mxGetPr(prhs[1]);
numCons = mxGetN(prhs[1]);
dataPr = mxGetPr(prhs[2]);
dataJc = mxGetJc(prhs[2]);
dataIr = mxGetIr(prhs[2]);
signF = mxGetScalar(prhs[3]);
scale = mxGetScalar(prhs[4]);
coef = mxGetScalar(prhs[5]);
debug = (int) mxGetScalar(prhs[6]);
plhs[0] = mxCreateDoubleMatrix(numCons, 1, mxREAL);
AtMNew = (double *) mxGetData(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
best_alpha = (double *) mxGetData(plhs[1]);
plhs[2] = mxCreateDoubleMatrix(numCons, 1, mxREAL);
violation = (double *) mxGetData(plhs[2]);
plhs[3] = mxCreateDoubleMatrix(numCons, 1, mxREAL);
losses = (double *) mxGetData(plhs[3]);
val = (double *) malloc(numCons*sizeof(double));
/* what if chosen vertex is the only existing */
if (debug == 1)
mexPrintf("Value of coefficient is: %g\n",coef);
if (coef != 1)
coef = coef / (1 - coef);
else
{
coef = 0;
/*mexErrMsgTxt("Only one, but to be kick.");*/
}
obja = 0;
objb = 0;
grada = 0;
gradb = 0;
for (i=0; i<numCons; i++) {
xIdx = Cons[i*3];
yIdx = Cons[i*3+1];
zIdx = Cons[i*3+2];
x1 = getValue(dataIr,dataJc,dataPr,xIdx,1);
x2 = getValue(dataIr,dataJc,dataPr,xIdx,2);
yz1 = getValue(dataIr,dataJc,dataPr,yIdx,1) - getValue(dataIr,dataJc,dataPr,zIdx,1);
yz2 = getValue(dataIr,dataJc,dataPr,yIdx,2) - getValue(dataIr,dataJc,dataPr,zIdx,2);
val[i] = x1*yz1 + x2*yz2;
if (signF > 0)
val[i] += x1*yz2 + x2*yz1;
else
val[i] -= x1*yz2 + x2*yz1;
obja += SHL(AtM[i]);
/*objb += SHL(val[i]);*/
objb += SHL(AtM[i] * (1+coef) - coef * scale * val[i]);
grada += SHL_gradls_away(AtM[i],scale*val[i],0);
gradb += SHL_gradls_away(AtM[i],scale*val[i],coef);
}
obja /= numCons;
objb /= numCons;
/*mexPrintf("%g %g %g %g\n",obja,objb,grada,gradb);*/
if (grada*gradb >= 0) { /* if gradients have same sign, then best step in [0,coef] is either 0 or coef*/
if (debug == 1)
mexPrintf("choose 0 or 1: %g, %g, %g,%g\n",grada,gradb,obja,objb);
if (obja < objb)
best_alpha[0] = 0;
else
best_alpha[0] = coef;
/*mexPrintf("choose 0 or 1\n");*/
} else { /* otherwise we do bisection search*/
a = 0; b = coef;
while (fabs(a-b) > 1e-3/scale) {
m = (a+b)/2;
if (debug == 1)
mexPrintf("inter %g %g %g\n",a,b,m);
gradm = 0;
for (i=0; i<numCons; i++) {
gradm += SHL_gradls_away(AtM[i],scale*val[i],m);
}
/*mexPrintf("grad %g %g %g%\n",grada,gradb,gradm);*/
if (gradm == 0) {
best_alpha[0] = m;
break;
}
if (gradm*grada < 0) {
b = m;
gradb = gradm;
}
if (gradm*gradb < 0) {
a = m;
grada = gradm;
}
}
/*mexPrintf("alpha %g grad %g%\n",m,gradm);*/
best_alpha[0] = m;
}
obj = 0;
for (i=0; i<numCons; i++) {
AtMNew[i] = (1+best_alpha[0])*AtM[i] - scale*best_alpha[0]*val[i];
violation[i] = max(0,1-AtMNew[i]);
losses[i] = SHL(AtMNew[i]);
obj += losses[i];
}
obj /= numCons;
/*mexPrintf("alpha %g obj %g%\n",best_alpha[0],obj);*/
return;
}