8 const std::vector<int>& s,
10 const Eigen::VectorXd& w,
11 const Eigen::VectorXd& residual,
12 const Eigen::VectorXd& x_centers,
13 const Eigen::VectorXd& x_scales,
18 Eigen::VectorXd x_s = Eigen::VectorXd::Zero(n);
20 auto s_it = s.cbegin();
21 auto c_it = clusters.
cbegin(j);
23 for (; c_it != clusters.
cend(j); ++c_it, ++s_it) {
27 switch (jit_normalization) {
29 x_s += x.col(k) * (s_k / x_scales(k));
30 x_s.array() -= x_centers(k) * s_k / x_scales(k);
34 x_s += x.col(k) * s_k;
35 x_s.array() -= x_centers(k) * s_k;
39 x_s += x.col(k) * (s_k / x_scales(k));
43 x_s += x.col(k) * s_k;
48 double hessian_j = x_s.cwiseAbs2().dot(w) / n;
49 double gradient_j = x_s.cwiseProduct(w).dot(residual) / n;
51 return { hessian_j, gradient_j };
57 const std::vector<int>& s,
59 const Eigen::VectorXd& w,
60 const Eigen::VectorXd& residual,
61 const Eigen::VectorXd& x_centers,
62 const Eigen::VectorXd& x_scales,
67 Eigen::VectorXd weighted_residual = w.cwiseProduct(residual);
69 std::vector<Eigen::Triplet<double>> triplets;
71 Eigen::SparseMatrix<double> x_s(n, 1);
75 auto s_it = s.cbegin();
76 auto c_it = clusters.
cbegin(j);
78 for (; c_it != clusters.
cend(j); ++c_it, ++s_it) {
82 switch (jit_normalization) {
84 offset += x_centers(k) * s_k;
87 offset += x_centers(k) * s_k / x_scales(k);
97 for (Eigen::SparseMatrix<double>::InnerIterator it(x, k); it; ++it) {
98 switch (jit_normalization) {
102 v = it.value() * s_k;
108 v = it.value() * s_k / x_scales(k);
112 triplets.emplace_back(it.row(), 0, v);
116 x_s.setFromTriplets(triplets.begin(), triplets.end());
121 assert(x_s.nonZeros() > 0);
123 switch (jit_normalization) {
127 hess = (x_s.col(0).cwiseAbs2().dot(w) - 2 * offset * x_s.col(0).dot(w) +
128 std::pow(offset, 2) * w.sum()) /
130 grad = x_s.col(0).dot(weighted_residual) / n -
131 offset * weighted_residual.sum() / n;
137 hess = x_s.col(0).cwiseAbs2().dot(w) / n;
138 grad = x_s.col(0).dot(weighted_residual) / n;
142 return { hess, grad };
std::pair< double, double > computeClusterGradientAndHessian(const Eigen::MatrixXd &x, const int j, const std::vector< int > &s, const Clusters &clusters, const Eigen::VectorXd &w, const Eigen::VectorXd &residual, const Eigen::VectorXd &x_centers, const Eigen::VectorXd &x_scales, const JitNormalization jit_normalization)