summaryrefslogtreecommitdiff
path: root/model/ranksvmtn.cpp
blob: fec7935d88891bef3ab904bf836d39fd82de1ab0 (plain)
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
#include "ranksvmtn.h"
#include<iostream>
#include<list>
#include"../tools/matrixIO.h"

using namespace std;
using namespace Eigen;

const int maxiter = 10;
const double prec=1e-4;
const double C=1;
const double cg_prec=1e-10;
const double line_prec=1e-10;
const double line_turb=1e-12;

int cal_Hs(const MatrixXd &D,const vector<int> &rank,const VectorXd &corr,const VectorXd &alpha,const vector<int> &A1,const vector<int> &A2,const double &C,const VectorXd s,VectorXd &Hs)
{
    Hs = VectorXd::Zero(s.rows());
    VectorXd Ds=D*s;
    VectorXd gamma(D.rows());
    for (int i=0;i<A1.size();++i)
    {
        double g=0;
        for (int j = A1[i];j<=A2[i];++j)
            if (corr[rank[j]]<0)
                gamma[rank[j]]=g;
            else
                g+=Ds[rank[j]];
        g=0;
        for (int j = A2[i];j>=A1[i];--j)
            if (corr[rank[j]]>0)
                gamma[rank[j]]=g;
            else
                g+=Ds[rank[j]];
    }
    Hs = s + (D.transpose()*(alpha.cwiseProduct(Ds) - gamma));
    return 0;
}

int cg_solve(const MatrixXd &D,const vector<int> &rank,const VectorXd &corr,const VectorXd &alph,const vector<int> &A1,const vector<int> &A2,const VectorXd &b,const double &C, VectorXd &x)
{
    double alpha,beta,r_1,r_2;
    int step=0;
    VectorXd q;
    VectorXd Hs;
    cal_Hs(D,rank,corr,alph,A1,A2,C,x,Hs);
    VectorXd res = b - Hs;
    VectorXd p = res;
    while (1)
    {
        // Non preconditioned version
        r_1 = res.dot(res);
        if (r_1<cg_prec) // Terminate condition
            break;
        if (step){
            beta = r_1 / r_2;
            p = res + p * beta;
        }
        cal_Hs(D,rank,corr,alph,A1,A2,C,p,q);
        alpha = r_1/p.dot(q);
        x=x+p*alpha;
        res=res-q*alpha;
        ++step;
        r_2=r_1;
    }
    return 0;
}

void ranksort(int l,int r,vector<int> &rank,VectorXd &ref)
{
    int i=l,j=r,k;
    double mid=ref(rank[(l+r)>>1]);
    while (i<=j)
    {
        while (ref[rank[i]]<mid) ++i;
        while (ref[rank[j]]>mid) --j;
        if (i<=j)
        {
            k=rank[i];
            rank[i]=rank[j];
            rank[j]=k;
            ++i;
            --j;
        }
    }
    if (j>l)
        ranksort(l,j,rank,ref);
    if (i<r)
        ranksort(i,r,rank,ref);
}

int cal_alpha_beta(const VectorXd &dw,const VectorXd &corr,const vector<int> &A1,const vector<int> &A2,vector<int> &rank,VectorXd &yt,VectorXd &alpha,VectorXd &beta)
{
    long n = dw.rows();
    yt = dw - corr;
    alpha=VectorXd::Zero(n);
    beta=VectorXd::Zero(n);
    for (int i=0;i<n;++i) rank[i]=i;
    for (int i=0;i<A1.size();++i)
    {
        ranksort(A1[i],A2[i],rank,yt);
        double a=0,b=0;
        for (int j=A1[i];j<=A2[i];++j)
            if (corr[rank[j]]<0)
            {
                alpha[rank[j]]=a;
                beta[rank[j]]=b;
            }
            else
            {
                a+=1;
                b+=yt[rank[j]];
            }
        a=b=0;
        for (int j=A2[i];j>=A1[i];--j)
            if (corr[rank[j]]>0)
            {
                alpha[rank[j]]=a;
                beta[rank[j]]=b;
            }
            else
            {
                a+=1;
                b+=yt[rank[j]];
            }
    }
}

// line search using newton method
int line_search(const VectorXd &w,const MatrixXd &D,const VectorXd &corr,const vector<int> &A1,const vector<int> &A2,const VectorXd &step,double &t)
{
    double wd=w.dot(step),dd=step.dot(step);
    VectorXd Dd = D*step;
    VectorXd Xd = VectorXd::Zero(A1.size());
    VectorXd alpha,beta,yt;
    VectorXd grad;
    VectorXd Hs;
    vector<int> rank(D.rows());

    for (int i=0;i<A1.size();++i)
        Xd(i) = Dd(A1[i])-Dd(A2[i]);
    double g,h;
    t = 0;
    while (1)
    {
        grad=w+t*step;
        Dd = D*(w + t*step);
        cal_alpha_beta(Dd,corr,A1,A2,rank,yt,alpha,beta);
        grad = grad + (D.transpose()*(alpha.cwiseProduct(yt)-beta));
        g = grad.dot(step);
        cal_Hs(D,rank,corr,alpha,A1,A2,C,step,Hs);
        h = Hs.dot(step);
        g=g+line_turb;
        h = h+line_turb;
        t=t-g/h;
        if (g*g/h<line_prec)
            break;
    }
    return 0;
}

int train_orig(int fsize, MatrixXd &D,const vector<int> &A1,const vector<int> &A2,const VectorXd &corr,VectorXd &weight){
    int iter = 0;

    long n=D.rows();
    LOG(INFO) << "training with feature size:" << fsize << " Data size:" << n << " Query size:" << A1.size();
    VectorXd grad(fsize);
    VectorXd step(fsize);
    vector<int> rank(n);
    double obj,t;

    VectorXd dw = D*weight;
    VectorXd yt;
    VectorXd alpha,beta;
    while (true)
    {
        iter+=1;
        if (iter> maxiter)
        {
            LOG(INFO)<< "Maxiter :"<<maxiter<<" reached";
            break;
        }

        dw = D*weight;

        cal_alpha_beta(dw,corr,A1,A2,rank,yt,alpha,beta);

        // Generate support vector matrix sv & gradient
        obj = (weight.dot(weight) + (alpha.dot(yt.cwiseProduct(yt))-beta.dot(yt)))/2;//
        grad = weight + (D.transpose()*(alpha.cwiseProduct(yt)-beta));
        step = grad*0;
        // Solve
        cg_solve(D,rank,corr,alpha,A1,A2,grad,C,step);
        // do line search
        line_search(weight,D,corr,A1,A2,step,t);
        weight=weight+step*t;
        // When dec is small enough
        LOG(INFO)<<"Iter: "<<iter<<" Obj: " <<obj << " Newton decr:"<<step.dot(grad)/2 << " linesearch: "<< -t ;
        if (step.dot(grad) < prec * obj)
            break;
    }
    return 0;
}

int RSVMTN::train(DataList &D){
    MatrixXd Data(D.getSize(),D.getfSize());
    VectorXd corr(D.getSize());
    vector<int> A1,A2;
    int i,j;
    LOG(INFO)<<"Processing input";
    for (i=0;i<D.getSize();++i) {
        corr(i)=(D.getData()[i])->rank>0?0.5:-0.5;
        for (j = 0; j < D.getfSize(); ++j)
            Data(i, j) = (D.getData()[i])->feature(j);
    }
    i=j=0;
    while (i<D.getSize())
    {
        if ((i+1 == D.getSize())|| D.getData()[i]->qid!=D.getData()[i+1]->qid)
        {
            A1.push_back(j);
            A2.push_back(i);
            j = i+1;
        }
        ++i;
    }
    train_orig(fsize,Data,A1,A2,corr,model.weight);
    return 0;
};

int RSVMTN::predict(DataList &D, vector<double> &res){
    res.clear();
    for (int i=0;i<D.getSize();++i)
        res.push_back(((D.getData()[i])->feature).dot(model.weight));
    return 0;
};