-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastruct.h
139 lines (111 loc) · 4.1 KB
/
datastruct.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
#ifndef _DATASTRUCT_H_
#define _DATASTRUCT_H_
#include <iostream>
#include <vector>
#include <numeric>
#include "GKlib.h"
#include "io.h"
#include "const.h"
class Params {
public:
int nUsers;
int nItems;
int facDim;
int maxIter;
int svdFacDim;
int seed;
float uReg;
float iReg;
float learnRate;
float rhoRMS;
float alpha;
const char *trainMatFile;
const char *testMatFile;
const char *valMatFile;
const char *graphMatFile;
const char *origUFacFile;
const char *origIFacFile;
const char *initUFacFile;
const char *initIFacFile;
const char *prefix;
Params(int facDim, int maxIter, int svdFacDim, int seed,
float uReg, float iReg, float learnRate, float rhoRMS,
float alpha, std::string& trainMatFile, std::string& testMatFile, std::string& valMatFile,
std::string& graphMatFile, std::string& origUFacFile, std::string& origIFacFile,
std::string& initUFacFile, std::string& initIFacFile, std::string& prefix)
: nUsers(-1), nItems(-1), facDim(facDim), maxIter(maxIter), svdFacDim(svdFacDim), seed(seed),
uReg(uReg), iReg(iReg), learnRate(learnRate), rhoRMS(rhoRMS),
alpha(alpha), trainMatFile(trainMatFile.c_str()), testMatFile(testMatFile.c_str()),
valMatFile(valMatFile.c_str()),
graphMatFile(graphMatFile.empty()?NULL:graphMatFile.c_str()),
origUFacFile(origUFacFile.empty()?NULL:origUFacFile.c_str()),
origIFacFile(origIFacFile.empty()?NULL:origIFacFile.c_str()),
initUFacFile(initUFacFile.empty()?NULL:initUFacFile.c_str()),
initIFacFile(initIFacFile.empty()?NULL:initIFacFile.c_str()),
prefix(prefix.c_str()){}
void display() {
std::cout << "*** PARAMETERS ***" << std::endl;
std::cout << "nUsers: " << nUsers << " nItems: " << nItems << std::endl;
std::cout << "facDim: " << facDim << " svdFacDim: " << svdFacDim << std::endl;
std::cout << "maxIter: " << maxIter << std::endl;
std::cout << "uReg: " << uReg << " iReg: " << iReg << std::endl;
std::cout << "rhoRMS: " << rhoRMS << " alpha: " << alpha << std::endl;
std::cout << "learnRate: " << learnRate << std::endl;
std::cout << "trainMat: " << trainMatFile << std::endl;
std::cout << "testMat: " << testMatFile << std::endl;
std::cout << "valMat: " << valMatFile << std::endl;
std::cout << "graphMat: " << (NULL != graphMatFile? graphMatFile : " ") << std::endl;
std::cout << "origUFac: " << (NULL != origUFacFile? origUFacFile : " ") << std::endl;
std::cout << "origIFac: " << (NULL != origIFacFile? origIFacFile : " ") << std::endl;
std::cout << "initUFac: " << (NULL != initUFacFile? initUFacFile : " ") << std::endl;
std::cout << "initIFac: " << (NULL != initIFacFile? initIFacFile : " ") << std::endl;
}
};
class Data {
public:
const char *prefix;
gk_csr_t *trainMat;
gk_csr_t *testMat;
gk_csr_t *valMat;
gk_csr_t *graphMat;
std::vector<std::vector<double>> origUFac;
std::vector<std::vector<double>> origIFac;
int facDim;
int trainNNZ;
int nUsers;
int nItems;
double meanKnownSubMatRat(int uStart, int uEnd, int iStart, int iEnd) {
int u, item;
double rmse = 0;
for (u = iStart; u <= uEnd; u++) {
for (item = iStart; item <= iEnd; item++) {
rmse += std::inner_product(origUFac[u].begin(), origUFac[u].end(),
origIFac[item].begin(), 0.0);
}
}
rmse = sqrt(rmse/((uEnd-uStart+1)*(iEnd-iStart+1)));
return rmse;
}
Data(gk_csr_t *p_trainMat, gk_csr_t *p_testMat) {
trainMat = p_trainMat;
testMat = p_testMat;
nUsers = trainMat->nrows;
nItems = trainMat->ncols;
}
Data(const Params& params);
~Data() {
if (NULL != trainMat) {
gk_csr_Free(&trainMat);
}
if (NULL != testMat) {
gk_csr_Free(&testMat);
}
if (NULL != valMat) {
gk_csr_Free(&valMat);
}
if (NULL != graphMat) {
gk_csr_Free(&graphMat);
}
}
};
#endif