-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfwdSparse.hpp
158 lines (106 loc) · 5.16 KB
/
fwdSparse.hpp
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
nOneSepLab = curFactor->nSepLab_[oneFactorInd];
nTwoSepLab = curFactor->nSepLab_[twoFactorInd];
nSumLab = curFactor->nSumLab_[twoFactorInd]; //total number of labels of the sum-set //^^^^
nDiffLab = curFactor->nDiffLab_[oneFactorInd];
sparseLab = curFactor->getSparseInd(); //^^^^
cEnergyConst = curFactor->getCEConst(); //^^^^
oneSumStride = curFactor->getSumStride(twoFactorInd); //^^^^
nSparseLab = sparseLab.size(); //^^^^
expValVecSum.resize(nOneSepLab); //^^^^
expValVecSparse.clear(); //^^^^
expValVecConst.clear(); //^^^^
expValMaxSumInitFlag = true; //^^^^
margConstSum = 0; //^^^^
twoMessageSiz = curFactor->margVecSiz_[twoFactorInd];
expValMaxInitFlag.resize(twoMessageSiz, true);
for (int iSepLab = 0; iSepLab != nOneSepLab; ++iSepLab) {
int sumStrideInd = 0;
double dualSum = 0;
for (std::vector<int>::iterator sumI = sumSet.begin(); sumI != sumSet.end(); ++sumI) {
std::vector<int>::iterator nodeIter = std::find(oneSepSet.begin(), oneSepSet.end(), *sumI);
int nodePos = std::distance(oneSepSet.begin(), nodeIter);
varAssign = static_cast<int>(floor(iSepLab/oneSepStride[nodePos])) % label[*sumI];
int subProbNodeIndex = subProbNodeMap.at(memNode[*sumI]);
dualSum += subProbDualVar[subProbNodeOffset[subProbNodeIndex] + varAssign] + uEnergy[uOffset[memNode[*sumI]] + varAssign];
++sumStrideInd;
} //for sumI
expVal = tau*((cEnergyConst/shareCnt) + dualSum) + log(oneMargVec[iSepLab]) + expValMaxFwd[iSepLab];
expValVecSum[iSepLab] = expVal;
if (expValMaxSumInitFlag) {
expValMaxSum = expVal - maxExponent;
expValMaxSumInitFlag = false;
}
else if (expVal - maxExponent > expValMax) {
expValMaxSum = expVal - maxExponent;
}
} //for iSepLab = [0:nOneSepLab)
margConstSum.resize(nDiffLab, 0);
for (int iSepLab = 0; iSepLab != nOneSepLab; ++iSepLab) {
int diffOffset = 0;
for (std::vector<int>::iterator diffI = diffSet.begin(); diffI != diffSet.end(); ++diffI) {
std::vector<int>::iterator nodeIter = std::find(oneSepSet.begin(), oneSepSet.end(), *diffI);
int nodePos = std::distance(oneSepSet.begin(), nodeIter);
varAssign = static_cast<int>(floor(iSepLab/oneSepStride[nodePos])) % label[*diffI];
diffOffset += varAssign*label[*diffI];
} //for diffI
expVal = exp(expValVecSum[iSepLab] - expValMaxSum);
myUtils::checkRangeError(expVal);
margConstSum[diffOffset] += expVal;
} //for iSepLab = [0:nOneSepLab)
twoMargVec.resize(twoMessageSiz);
for (int iSepLab = 0; iSepLab != nTwoSepLab; ++iSepLab) {
int diffOffset = 0;
for (std::vector<int>::iterator diffI = diffSet.begin(); diffI != diffSet.end(); ++diffI) {
std::vector<int>::iterator nodeIter = std::find(twoSepSet.begin(), twoSepSet.end(), *diffI);
int nodePos = std::distance(twoSepSet.begin(), nodeIter);
varAssign = static_cast<int>(floor(iSepLab/twoSepStride[nodePos])) % label[*diffI];
diffOffset += varAssign*label[*diffI];
} //for diffI
twoMargVec[iSepLab] = margConstSum[diffOffset];
} //for iSepLab = [0:nOneSepLab)
for (const std::vector<int>::iterator iSparseLab = sparseLab.begin(); iSparseLab != sparseLab.end(); ++iSparseLab) {
int twoMargInd = 0;
int sepStrideInd = 0;
for (std::vector<int>::iterator sepI = twoSepSet.begin(); sepI != twoSepSet.end(); ++sepI) {
varAssign = static_cast<int>(floor(*iSparseLab/stride[*sepI])) % label[*sepI];
twoMargInd += varAssign*twoSepStride[sepStrideInd];
++sepStrideInd;
}
double dualSum = 0;
for (std::vector<int>::iterator sumI = sumSet.begin(); sumI != sumSet.end(); ++sumI) {
varAssign = static_cast<int>(floor(*iSparseLab/stride[*sumI])) % label[*sumI];
int subProbNodeIndex = subProbNodeMap.at(memNode[*sumI]);
dualSum += subProbDualVar[subProbNodeOffset[subProbNodeIndex] + varAssign] + uEnergy[uOffset[memNode[*sumI]] + varAssign];
}
cEnergyVal = curFactor->getCE(*iSparseLab);
expVal = tau*((cEnergyVal/shareCnt) + dualSum); //$$$$
expValVecSparse.push_back(expVal);
if (expValMaxInitFlag[twoMargInd]) { //$$$$
expValMax[twoMargInd] = expVal - maxExponent;
expValMaxInitFlag[oneMargInd] = false;
}
else if (expVal - maxExponent > expValMax[twoMargInd]) { //$$$$
expValMax[twoMargInd] = expVal - maxExponent;
}
expVal = tau*((cEnergyConst/shareCnt) + dualSum);
expValVecConst.push_back(expVal);
} //for iSparseLab
int iterCnt = 0;
for (const std::vector<int>::iterator iSparseLab = sparseLab.begin(); iSparseLab != sparseLab.end(); ++iSparseLab) {
int twoMargInd = 0;
int sepStrideInd = 0;
for (std::vector<int>::iterator sepI = twoSepSet.begin(); sepI != twoSepSet.end(); ++sepI) {
varAssign = static_cast<int>(floor(*iSparseLab/stride[*sepI])) % label[*sepI];
twoMargInd += varAssign*twoSepStride[sepStrideInd];
++sepStrideInd;
}
expVal = exp(expValVecSparse[iterCnt] - expValMax[twoMargInd]);
myUtils::checkRangeError(expVal);
twoMargVec[twoMargInd] += expVal;
expVal = exp(expValVecConst[iterCnt] - expValMax[twoMargInd]);
myUtils::checkRangeError(expVal);
twoMargVec[twoMargInd] += expVal;
++iterCnt;
} //for iSparseLab
expValMaxFwd = expValMax;
oneMargVec = twoMargVec;