Use a struct instead of touples in SVD aswell.

This commit is contained in:
Relintai 2023-02-07 21:46:59 +01:00
parent 256b7c9c14
commit 34cf810bd7
6 changed files with 25 additions and 19 deletions

View File

@ -938,12 +938,12 @@ std::vector<std::vector<real_t>> MLPPData::LSA(std::vector<std::string> sentence
MLPPLinAlg alg; MLPPLinAlg alg;
std::vector<std::vector<real_t>> docWordData = BOW(sentences, "Binary"); std::vector<std::vector<real_t>> docWordData = BOW(sentences, "Binary");
auto [U, S, Vt] = alg.SVD(docWordData); MLPPLinAlg::SDVResult svr_res = alg.SVD(docWordData);
std::vector<std::vector<real_t>> S_trunc = alg.zeromat(dim, dim); std::vector<std::vector<real_t>> S_trunc = alg.zeromat(dim, dim);
std::vector<std::vector<real_t>> Vt_trunc; std::vector<std::vector<real_t>> Vt_trunc;
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {
S_trunc[i][i] = S[i][i]; S_trunc[i][i] = svr_res.S[i][i];
Vt_trunc.push_back(Vt[i]); Vt_trunc.push_back(svr_res.Vt[i]);
} }
std::vector<std::vector<real_t>> embeddings = alg.matmult(S_trunc, Vt_trunc); std::vector<std::vector<real_t>> embeddings = alg.matmult(S_trunc, Vt_trunc);

View File

@ -1198,18 +1198,24 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(std::vector<std::vector<real_t>> A) {
return res; return res;
} }
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) { MLPPLinAlg::SDVResult MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) {
auto [left_eigenvecs, eigenvals] = eig(matmult(A, transpose(A))); EigenResult left_eigen = eigen(matmult(A, transpose(A)));
auto [right_eigenvecs, right_eigenvals] = eig(matmult(transpose(A), A)); EigenResult right_eigen = eigen(matmult(transpose(A), A));
std::vector<std::vector<real_t>> singularvals = sqrt(eigenvals); std::vector<std::vector<real_t>> singularvals = sqrt(left_eigen.eigen_values);
std::vector<std::vector<real_t>> sigma = zeromat(A.size(), A[0].size()); std::vector<std::vector<real_t>> sigma = zeromat(A.size(), A[0].size());
for (int i = 0; i < singularvals.size(); i++) { for (int i = 0; i < singularvals.size(); i++) {
for (int j = 0; j < singularvals[i].size(); j++) { for (int j = 0; j < singularvals[i].size(); j++) {
sigma[i][j] = singularvals[i][j]; sigma[i][j] = singularvals[i][j];
} }
} }
return { left_eigenvecs, sigma, right_eigenvecs };
SDVResult res;
res.U = left_eigen.eigen_vectors;
res.S = sigma;
res.Vt = right_eigen.eigen_vectors;
return res;
} }
MLPPLinAlg::SDVResult MLPPLinAlg::svd(std::vector<std::vector<real_t>> A) { MLPPLinAlg::SDVResult MLPPLinAlg::svd(std::vector<std::vector<real_t>> A) {

View File

@ -121,14 +121,14 @@ public:
EigenResult eigen(std::vector<std::vector<real_t>> A); EigenResult eigen(std::vector<std::vector<real_t>> A);
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> SVD(std::vector<std::vector<real_t>> A);
struct SDVResult { struct SDVResult {
std::vector<std::vector<real_t>> U; std::vector<std::vector<real_t>> U;
std::vector<std::vector<real_t>> S; std::vector<std::vector<real_t>> S;
std::vector<std::vector<real_t>> Vt; std::vector<std::vector<real_t>> Vt;
}; };
SDVResult SVD(std::vector<std::vector<real_t>> A);
SDVResult svd(std::vector<std::vector<real_t>> A); SDVResult svd(std::vector<std::vector<real_t>> A);
std::vector<real_t> vectorProjection(std::vector<real_t> a, std::vector<real_t> b); std::vector<real_t> vectorProjection(std::vector<real_t> a, std::vector<real_t> b);

View File

@ -21,12 +21,12 @@ std::vector<std::vector<real_t>> MLPPPCA::principalComponents() {
MLPPLinAlg alg; MLPPLinAlg alg;
MLPPData data; MLPPData data;
auto [U, S, Vt] = alg.SVD(alg.cov(inputSet)); MLPPLinAlg::SDVResult svr_res = alg.SVD(alg.cov(inputSet));
X_normalized = data.meanCentering(inputSet); X_normalized = data.meanCentering(inputSet);
U_reduce.resize(U.size()); U_reduce.resize(svr_res.U.size());
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
for (int j = 0; j < U.size(); j++) { for (int j = 0; j < svr_res.U.size(); j++) {
U_reduce[j].push_back(U[j][i]); U_reduce[j].push_back(svr_res.U[j][i]);
} }
} }
Z = alg.matmult(alg.transpose(U_reduce), X_normalized); Z = alg.matmult(alg.transpose(U_reduce), X_normalized);

View File

@ -21,12 +21,12 @@ std::vector<std::vector<real_t>> MLPPPCAOld::principalComponents() {
MLPPLinAlg alg; MLPPLinAlg alg;
MLPPData data; MLPPData data;
auto [U, S, Vt] = alg.SVD(alg.cov(inputSet)); MLPPLinAlg::SDVResult svr_res = alg.SVD(alg.cov(inputSet));
X_normalized = data.meanCentering(inputSet); X_normalized = data.meanCentering(inputSet);
U_reduce.resize(U.size()); U_reduce.resize(svr_res.U.size());
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
for (int j = 0; j < U.size(); j++) { for (int j = 0; j < svr_res.U.size(); j++) {
U_reduce[j].push_back(U[j][i]); U_reduce[j].push_back(svr_res.U[j][i]);
} }
} }
Z = alg.matmult(alg.transpose(U_reduce), X_normalized); Z = alg.matmult(alg.transpose(U_reduce), X_normalized);

View File

@ -721,7 +721,7 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) {
std::cout << "SVD" << std::endl; std::cout << "SVD" << std::endl;
MLPPLinAlg::SDVResult svd = alg.svd(inputSet); MLPPLinAlg::SDVResult svd = alg.SVD(inputSet);
std::cout << "U:" << std::endl; std::cout << "U:" << std::endl;
alg.printMatrix(svd.U); alg.printMatrix(svd.U);