-
Notifications
You must be signed in to change notification settings - Fork 11
/
squareloss.cpp
135 lines (113 loc) · 4.07 KB
/
squareloss.cpp
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
#include <vector>
#include <set>
#include <iostream>
#include <cmath>
#include <stdexcept>
#include "squareloss.h"
#include "matrixfactorization.h"
#include "rowwisematrix.h"
SquareLoss::SquareLoss(SparseMatrix* _matrix, const int& InitComs, bool _Verbose) : MatrixFactorization(_matrix, InitComs, _Verbose)
{
InitVSum();
InitHSum();
}
void SquareLoss::InitVSum()
{
// Initialization of SumV & matrixSumV to zero
SumV = std::vector<double>(NumComs, 0);
matrixSumV = std::vector<std::vector<double> >(NumComs, std::vector<double>(NumComs, 0));
// Computation of SumV & matrixSumV
for (int i = 0; i < V.size(); i++)
AddToSum(matrixSumV, SumV, V[i]);
/*
for (int i = 0; i < NumComs; i++)
{
std::cerr << "SumV" << std::endl;
for (int j = 0; j < NumComs; j++)
std::cerr << matrixSumV[i][j] << " ";
std::cerr << std::endl;
}*/
}
void SquareLoss::InitHSum()
{
// Initialization of SumH & matrixSumH to zero
SumH = std::vector<double>(NumComs, 0);
matrixSumH = std::vector<std::vector<double> >(NumComs, std::vector<double>(NumComs, 0));
// Computation of SumH & matrixSumH
for (int i = 0; i < H.size(); i++)
AddToSum(matrixSumH, SumH, H[i]);
/*
for (int i = 0; i < NumComs; i++)
{
std::cerr << "SumH" << std::endl;
for (int j = 0; j < NumComs; j++)
std::cerr << matrixSumH[i][j] << " ";
std::cerr << std::endl;
}*/
}
void SquareLoss::ModifyNode(node nodeAddr, TIntFltH & rowFeatures)
{
if (nodeAddr.orientation == ROW)
{
AddToSum(matrixSumV, SumV, V[nodeAddr.id], -1);
V[nodeAddr.id] = rowFeatures;
AddToSum(matrixSumV, SumV, V[nodeAddr.id]);
}
else
{
AddToSum(matrixSumH, SumH, H[nodeAddr.id], -1);
H[nodeAddr.id] = rowFeatures;
AddToSum(matrixSumH, SumH, H[nodeAddr.id]);
}
}
double SquareLoss::ErrorForRow(const RowWiseMatrix::row* rowEntries, TIntFltH & rowFeatures, const std::vector<TIntFltH> & F, node currentNode)
{
double L = 0.0;
std::vector<double> productWithSumF;
if (currentNode.orientation == ROW)
MatrixVectorProduct(matrixSumH, rowFeatures, productWithSumF);
else
MatrixVectorProduct(matrixSumV, rowFeatures, productWithSumF);
L = NegWeight*DotProduct(rowFeatures, productWithSumF);
for (int i = 0; i < rowEntries->size(); i++)
{
double dp = DotProduct(rowFeatures, F[(*rowEntries)[i].id]);
L += (*rowEntries)[i].value * ((*rowEntries)[i].value - 2*dp) + (1 - NegWeight)*dp*dp;
}
//add regularization
L += RegularizationPenaltyForRow(rowFeatures);
if (L < 0)
{
std::cerr << currentNode.id << " " << currentNode.orientation << " loss: " << L << " (" << RegularizationPenaltyForRow(rowFeatures) << ") w=" << NegWeight << std::endl;
std::cerr << "Matrix size " << matrix->n() << " " << matrix->m() << std::endl;
throw std::runtime_error("Negative reconstruction error");
}
return L;
}
void SquareLoss::GradientForRow(const RowWiseMatrix::row* rowEntries, TIntFltH & rowFeatures, const std::vector<TIntFltH> & F, node currentNode, TIntFltH& gradient, const std::set<int>& CIDSet)
{
std::vector<double> tempGradient(CIDSet.size(), 0.0);
if (currentNode.orientation == ROW)
PartialMatrixVectorProduct(matrixSumH, rowFeatures, tempGradient, CIDSet);
else
PartialMatrixVectorProduct(matrixSumV, rowFeatures, tempGradient, CIDSet);
//account for factor 2 and weight of negative instances
for (int i = 0; i < tempGradient.size(); ++i)
tempGradient[i] *= 2*NegWeight;
for (int i = 0; i < rowEntries->size(); i++)
{
int j = 0;
double dp = DotProduct(rowFeatures, F[(*rowEntries)[i].id]);
for (std::set<int>::iterator it=CIDSet.begin(); it!=CIDSet.end(); ++it, ++j)
tempGradient[j] -= 2*( (*rowEntries)[i].value - (1 - NegWeight)*dp )*GetCom(F[(*rowEntries)[i].id], *it);
}
//add regularization
RegularizationGradientForRow(tempGradient, CIDSet, rowFeatures);
// Copy from vector storing to hashmap
std::set<int>::iterator it = CIDSet.begin();
for (int i = 0; i < tempGradient.size(); i++, ++it)
{
//if (fabs(tempGradient[i]) < 0.0001) { continue; }
gradient[*it] = tempGradient[i];
}
}