summaryrefslogtreecommitdiff
path: root/model/ranksvm.cpp
blob: dd2d9e4433749b0acba173ec47d8ca501d0c3764 (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
#include"ranksvm.h"
#include"ranksvmtn.h"
#include"../tools/matrixIO.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace Eigen;
using namespace std;

double C=1e-4; // Compensating & scaling

int RSVM::saveModel(const string fname){

    std::ofstream fout(fname.c_str());
    fout<<this->getName()<<endl;
    fout<<this->fsize<<endl;
    Eigen::write_stream(fout, this->model.weight);
    fout<<this->model.beta<<endl;
    return 0;
}

RSVM* RSVM::loadModel(const string fname){
    std::ifstream fin(fname.c_str());
    std::string type;
    int fsize;
    fin>>type;
    fin>>fsize;

    RSVM* rsvm;

    if (type=="TN")
        rsvm = new RSVMTN();

    rsvm->fsize=fsize;
    SVMModel model;
    Eigen::read_stream(fin, model.weight);
    fin>>model.beta;
    rsvm->setModel(model);

    return rsvm;
}

int RSVM::setModel(const SVMModel &model) {
    if (model.weight.rows()!=fsize)
        LOG(FATAL) << "Feature size mismatch: "<<fsize<<" "<<model.weight.rows();
    this->model.weight=model.weight;
    this->model.beta=model.beta;
    return 0;
}