mirror of
https://github.com/Relintai/pmlpp.git
synced 2025-02-21 19:57:58 +01:00
Clean up MLPPPCA.
This commit is contained in:
parent
e96431a577
commit
68db9a55f0
@ -938,7 +938,7 @@ std::vector<std::vector<real_t>> MLPPData::LSA(std::vector<std::string> sentence
|
||||
MLPPLinAlg alg;
|
||||
std::vector<std::vector<real_t>> docWordData = BOW(sentences, "Binary");
|
||||
|
||||
MLPPLinAlg::SDVResultOld svr_res = alg.SVD(docWordData);
|
||||
MLPPLinAlg::SVDResultOld svr_res = alg.SVD(docWordData);
|
||||
std::vector<std::vector<real_t>> S_trunc = alg.zeromat(dim, dim);
|
||||
std::vector<std::vector<real_t>> Vt_trunc;
|
||||
for (int i = 0; i < dim; i++) {
|
||||
@ -1054,6 +1054,33 @@ std::vector<real_t> MLPPData::reverseOneHot(std::vector<std::vector<real_t>> tem
|
||||
return outputSet;
|
||||
}
|
||||
|
||||
Ref<MLPPMatrix> MLPPData::mean_centering(const Ref<MLPPMatrix> &p_X) {
|
||||
MLPPLinAlg alg;
|
||||
MLPPStat stat;
|
||||
|
||||
Ref<MLPPMatrix> X;
|
||||
X.instance();
|
||||
X->resize(p_X->size());
|
||||
|
||||
Size2i x_size = X->size();
|
||||
|
||||
Ref<MLPPVector> x_row_tmp;
|
||||
x_row_tmp.instance();
|
||||
x_row_tmp->resize(x_size.x);
|
||||
|
||||
for (int i = 0; i < x_size.y; ++i) {
|
||||
X->get_row_into_mlpp_vector(i, x_row_tmp);
|
||||
|
||||
real_t mean_i = stat.meanv(x_row_tmp);
|
||||
|
||||
for (int j = 0; j < x_size.x; ++j) {
|
||||
X->set_element(i, j, p_X->get_element(i, j) - mean_i);
|
||||
}
|
||||
}
|
||||
|
||||
return X;
|
||||
}
|
||||
|
||||
void MLPPData::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("load_breast_cancer", "path"), &MLPPData::load_breast_cancer);
|
||||
ClassDB::bind_method(D_METHOD("load_breast_cancer_svc", "path"), &MLPPData::load_breast_cancer_svc);
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
#include "core/object/reference.h"
|
||||
|
||||
#include "../lin_alg/mlpp_matrix.h"
|
||||
#include "../lin_alg/mlpp_vector.h"
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
@ -147,6 +150,8 @@ public:
|
||||
std::vector<std::vector<real_t>> oneHotRep(std::vector<real_t> tempOutputSet, int n_class);
|
||||
std::vector<real_t> reverseOneHot(std::vector<std::vector<real_t>> tempOutputSet);
|
||||
|
||||
Ref<MLPPMatrix> mean_centering(const Ref<MLPPMatrix> &X);
|
||||
|
||||
template <class T>
|
||||
std::vector<T> vecToSet(std::vector<T> inputSet) {
|
||||
std::vector<T> setInputSet;
|
||||
|
@ -1075,6 +1075,37 @@ std::vector<std::vector<real_t>> MLPPLinAlg::cov(std::vector<std::vector<real_t>
|
||||
return covMat;
|
||||
}
|
||||
|
||||
Ref<MLPPMatrix> MLPPLinAlg::covm(const Ref<MLPPMatrix> &A) {
|
||||
MLPPStat stat;
|
||||
|
||||
Ref<MLPPMatrix> cov_mat;
|
||||
cov_mat.instance();
|
||||
|
||||
Size2i a_size = A->size();
|
||||
|
||||
cov_mat->resize(a_size);
|
||||
|
||||
Ref<MLPPVector> a_i_row_tmp;
|
||||
a_i_row_tmp.instance();
|
||||
a_i_row_tmp->resize(a_size.x);
|
||||
|
||||
Ref<MLPPVector> a_j_row_tmp;
|
||||
a_j_row_tmp.instance();
|
||||
a_j_row_tmp->resize(a_size.x);
|
||||
|
||||
for (int i = 0; i < a_size.y; ++i) {
|
||||
A->get_row_into_mlpp_vector(i, a_i_row_tmp);
|
||||
|
||||
for (int j = 0; j < a_size.x; ++j) {
|
||||
A->get_row_into_mlpp_vector(j, a_j_row_tmp);
|
||||
|
||||
cov_mat->set_element(i, j, stat.covariancev(a_i_row_tmp, a_j_row_tmp));
|
||||
}
|
||||
}
|
||||
|
||||
return cov_mat;
|
||||
}
|
||||
|
||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> MLPPLinAlg::eig(std::vector<std::vector<real_t>> A) {
|
||||
/*
|
||||
A (the entered parameter) in most use cases will be X'X, XX', etc. and must be symmetric.
|
||||
@ -1384,7 +1415,7 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref<MLPPMatrix> A) {
|
||||
|
||||
for (int i = 0; i < a_new_size.y; ++i) {
|
||||
for (int j = 0; j < a_new_size.x; ++j) {
|
||||
if (i != j && Math::round(a_new->get_element(i, j)) == 0) {
|
||||
if (i != j && Math::is_zero_approx(Math::round(a_new->get_element(i, j)))) {
|
||||
a_new->set_element(i, j, 0);
|
||||
}
|
||||
}
|
||||
@ -1393,7 +1424,7 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref<MLPPMatrix> A) {
|
||||
bool non_zero = false;
|
||||
for (int i = 0; i < a_new_size.y; ++i) {
|
||||
for (int j = 0; j < a_new_size.x; ++j) {
|
||||
if (i != j && Math::round(a_new->get_element(i, j)) == 0) {
|
||||
if (i != j && Math::is_zero_approx(Math::round(a_new->get_element(i, j)))) {
|
||||
non_zero = true;
|
||||
}
|
||||
}
|
||||
@ -1405,7 +1436,7 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref<MLPPMatrix> A) {
|
||||
diagonal = true;
|
||||
}
|
||||
|
||||
if (a_new == A) {
|
||||
if (a_new->is_equal_approx(A)) {
|
||||
diagonal = true;
|
||||
for (int i = 0; i < a_new_size.y; ++i) {
|
||||
for (int j = 0; j < a_new_size.x; ++j) {
|
||||
@ -1421,7 +1452,7 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref<MLPPMatrix> A) {
|
||||
|
||||
} while (!diagonal);
|
||||
|
||||
Ref<MLPPMatrix> a_new_prior = a_new;
|
||||
Ref<MLPPMatrix> a_new_prior = a_new->duplicate();
|
||||
|
||||
Size2i a_new_size = a_new->size();
|
||||
|
||||
@ -1460,7 +1491,7 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref<MLPPMatrix> A) {
|
||||
return res;
|
||||
}
|
||||
|
||||
MLPPLinAlg::SDVResultOld MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) {
|
||||
MLPPLinAlg::SVDResultOld MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) {
|
||||
EigenResultOld left_eigen = eigen_old(matmult(A, transpose(A)));
|
||||
EigenResultOld right_eigen = eigen_old(matmult(transpose(A), A));
|
||||
|
||||
@ -1472,7 +1503,7 @@ MLPPLinAlg::SDVResultOld MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) {
|
||||
}
|
||||
}
|
||||
|
||||
SDVResultOld res;
|
||||
SVDResultOld res;
|
||||
res.U = left_eigen.eigen_vectors;
|
||||
res.S = sigma;
|
||||
res.Vt = right_eigen.eigen_vectors;
|
||||
@ -1480,8 +1511,8 @@ MLPPLinAlg::SDVResultOld MLPPLinAlg::SVD(std::vector<std::vector<real_t>> A) {
|
||||
return res;
|
||||
}
|
||||
|
||||
MLPPLinAlg::SDVResult MLPPLinAlg::svd(const Ref<MLPPMatrix> &A) {
|
||||
SDVResult res;
|
||||
MLPPLinAlg::SVDResult MLPPLinAlg::svd(const Ref<MLPPMatrix> &A) {
|
||||
SVDResult res;
|
||||
|
||||
ERR_FAIL_COND_V(!A.is_valid(), res);
|
||||
|
||||
|
@ -118,6 +118,7 @@ public:
|
||||
Ref<MLPPMatrix> identitym(int d);
|
||||
|
||||
std::vector<std::vector<real_t>> cov(std::vector<std::vector<real_t>> A);
|
||||
Ref<MLPPMatrix> covm(const Ref<MLPPMatrix> &A);
|
||||
|
||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> eig(std::vector<std::vector<real_t>> A);
|
||||
|
||||
@ -135,21 +136,21 @@ public:
|
||||
|
||||
EigenResult eigen(Ref<MLPPMatrix> A);
|
||||
|
||||
struct SDVResultOld {
|
||||
struct SVDResultOld {
|
||||
std::vector<std::vector<real_t>> U;
|
||||
std::vector<std::vector<real_t>> S;
|
||||
std::vector<std::vector<real_t>> Vt;
|
||||
};
|
||||
|
||||
SDVResultOld SVD(std::vector<std::vector<real_t>> A);
|
||||
SVDResultOld SVD(std::vector<std::vector<real_t>> A);
|
||||
|
||||
struct SDVResult {
|
||||
struct SVDResult {
|
||||
Ref<MLPPMatrix> U;
|
||||
Ref<MLPPMatrix> S;
|
||||
Ref<MLPPMatrix> Vt;
|
||||
};
|
||||
|
||||
SDVResult svd(const Ref<MLPPMatrix> &A);
|
||||
SVDResult svd(const Ref<MLPPMatrix> &A);
|
||||
|
||||
std::vector<real_t> vectorProjection(std::vector<real_t> a, std::vector<real_t> b);
|
||||
|
||||
|
@ -592,6 +592,28 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ bool is_equal_approx(const Ref<MLPPMatrix> &p_with, real_t tolerance = static_cast<real_t>(CMP_EPSILON)) const {
|
||||
ERR_FAIL_COND_V(!p_with.is_valid(), false);
|
||||
|
||||
if (unlikely(this == p_with.ptr())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_size != p_with->size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int ds = data_size();
|
||||
|
||||
for (int i = 0; i < ds; ++i) {
|
||||
if (!Math::is_equal_approx(_data[i], p_with->_data[i], tolerance)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String to_string();
|
||||
|
||||
_FORCE_INLINE_ MLPPMatrix() {
|
||||
|
118
mlpp/pca/pca.cpp
118
mlpp/pca/pca.cpp
@ -8,44 +8,118 @@
|
||||
#include "../data/data.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
Ref<MLPPMatrix> MLPPPCA::get_input_set() {
|
||||
return _input_set;
|
||||
}
|
||||
void MLPPPCA::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||
_input_set = val;
|
||||
}
|
||||
|
||||
int MLPPPCA::get_k() {
|
||||
return _k;
|
||||
}
|
||||
void MLPPPCA::set_k(const int val) {
|
||||
_k = val;
|
||||
}
|
||||
|
||||
Ref<MLPPMatrix> MLPPPCA::principal_components() {
|
||||
ERR_FAIL_COND_V(!_input_set.is_valid() || _k == 0, Ref<MLPPMatrix>());
|
||||
|
||||
std::vector<std::vector<real_t>> MLPPPCA::principalComponents() {
|
||||
MLPPLinAlg alg;
|
||||
MLPPData data;
|
||||
|
||||
MLPPLinAlg::SDVResultOld svr_res = alg.SVD(alg.cov(inputSet));
|
||||
X_normalized = data.meanCentering(inputSet);
|
||||
U_reduce.resize(svr_res.U.size());
|
||||
for (int i = 0; i < k; i++) {
|
||||
for (int j = 0; j < svr_res.U.size(); j++) {
|
||||
U_reduce[j].push_back(svr_res.U[j][i]);
|
||||
MLPPLinAlg::SVDResult svr_res = alg.svd(alg.covm(_input_set));
|
||||
_x_normalized = data.mean_centering(_input_set);
|
||||
|
||||
Size2i svr_res_u_size = svr_res.U->size();
|
||||
|
||||
_u_reduce->resize(Size2i(_k, svr_res_u_size.y));
|
||||
|
||||
for (int i = 0; i < _k; ++i) {
|
||||
for (int j = 0; j < svr_res_u_size.y; ++j) {
|
||||
_u_reduce->set_element(j, i, svr_res.U->get_element(j, i));
|
||||
}
|
||||
}
|
||||
Z = alg.matmult(alg.transpose(U_reduce), X_normalized);
|
||||
return Z;
|
||||
|
||||
_z = alg.matmultm(alg.transposem(_u_reduce), _x_normalized);
|
||||
|
||||
return _z;
|
||||
}
|
||||
|
||||
// Simply tells us the percentage of variance maintained.
|
||||
real_t MLPPPCA::score() {
|
||||
ERR_FAIL_COND_V(!_input_set.is_valid() || _k == 0, 0);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
std::vector<std::vector<real_t>> X_approx = alg.matmult(U_reduce, Z);
|
||||
real_t num, den = 0;
|
||||
for (int i = 0; i < X_normalized.size(); i++) {
|
||||
num += alg.norm_sq(alg.subtraction(X_normalized[i], X_approx[i]));
|
||||
}
|
||||
num /= X_normalized.size();
|
||||
for (int i = 0; i < X_normalized.size(); i++) {
|
||||
den += alg.norm_sq(X_normalized[i]);
|
||||
|
||||
Ref<MLPPMatrix> x_approx = alg.matmultm(_u_reduce, _z);
|
||||
real_t num = 0;
|
||||
real_t den = 0;
|
||||
|
||||
Size2i x_normalized_size = _x_normalized->size();
|
||||
|
||||
int x_normalized_size_y = x_normalized_size.y;
|
||||
|
||||
Ref<MLPPVector> x_approx_row_tmp;
|
||||
x_approx_row_tmp.instance();
|
||||
x_approx_row_tmp->resize(x_approx->size().x);
|
||||
|
||||
Ref<MLPPVector> x_normalized_row_tmp;
|
||||
x_normalized_row_tmp.instance();
|
||||
x_normalized_row_tmp->resize(x_normalized_size.x);
|
||||
|
||||
for (int i = 0; i < x_normalized_size_y; ++i) {
|
||||
_x_normalized->get_row_into_mlpp_vector(i, x_normalized_row_tmp);
|
||||
x_approx->get_row_into_mlpp_vector(i, x_approx_row_tmp);
|
||||
|
||||
num += alg.norm_sqv(alg.subtractionnv(x_normalized_row_tmp, x_approx_row_tmp));
|
||||
}
|
||||
|
||||
den /= X_normalized.size();
|
||||
num /= x_normalized_size_y;
|
||||
|
||||
for (int i = 0; i < x_normalized_size_y; ++i) {
|
||||
_x_normalized->get_row_into_mlpp_vector(i, x_normalized_row_tmp);
|
||||
|
||||
den += alg.norm_sqv(x_normalized_row_tmp);
|
||||
}
|
||||
|
||||
den /= x_normalized_size_y;
|
||||
|
||||
if (den == 0) {
|
||||
den += 1e-10; // For numerical sanity as to not recieve a domain error
|
||||
}
|
||||
|
||||
return 1 - num / den;
|
||||
}
|
||||
|
||||
MLPPPCA::MLPPPCA(std::vector<std::vector<real_t>> inputSet, int k) :
|
||||
inputSet(inputSet), k(k) {
|
||||
MLPPPCA::MLPPPCA(const Ref<MLPPMatrix> &p_input_set, int p_k) {
|
||||
_k = p_k;
|
||||
_input_set = p_input_set;
|
||||
|
||||
_x_normalized.instance();
|
||||
_u_reduce.instance();
|
||||
_z.instance();
|
||||
}
|
||||
|
||||
MLPPPCA::MLPPPCA() {
|
||||
_k = 0;
|
||||
|
||||
_x_normalized.instance();
|
||||
_u_reduce.instance();
|
||||
_z.instance();
|
||||
}
|
||||
MLPPPCA::~MLPPPCA() {
|
||||
}
|
||||
|
||||
void MLPPPCA::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPPCA::get_input_set);
|
||||
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPPCA::set_input_set);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "get_input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_k"), &MLPPPCA::get_k);
|
||||
ClassDB::bind_method(D_METHOD("set_k", "val"), &MLPPPCA::set_k);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("principal_components"), &MLPPPCA::principal_components);
|
||||
ClassDB::bind_method(D_METHOD("score"), &MLPPPCA::score);
|
||||
}
|
||||
|
@ -10,21 +10,38 @@
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
|
||||
#include <vector>
|
||||
#include "core/object/reference.h"
|
||||
|
||||
#include "../lin_alg/mlpp_matrix.h"
|
||||
#include "../lin_alg/mlpp_vector.h"
|
||||
|
||||
class MLPPPCA : public Reference {
|
||||
GDCLASS(MLPPPCA, Reference);
|
||||
|
||||
class MLPPPCA {
|
||||
public:
|
||||
std::vector<std::vector<real_t>> principalComponents();
|
||||
Ref<MLPPMatrix> get_input_set();
|
||||
void set_input_set(const Ref<MLPPMatrix> &val);
|
||||
|
||||
int get_k();
|
||||
void set_k(const int val);
|
||||
|
||||
Ref<MLPPMatrix> principal_components();
|
||||
real_t score();
|
||||
|
||||
MLPPPCA(std::vector<std::vector<real_t>> inputSet, int k);
|
||||
MLPPPCA(const Ref<MLPPMatrix> &p_input_set, int p_k);
|
||||
|
||||
private:
|
||||
std::vector<std::vector<real_t>> inputSet;
|
||||
std::vector<std::vector<real_t>> X_normalized;
|
||||
std::vector<std::vector<real_t>> U_reduce;
|
||||
std::vector<std::vector<real_t>> Z;
|
||||
int k;
|
||||
MLPPPCA();
|
||||
~MLPPPCA();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Ref<MLPPMatrix> _input_set;
|
||||
int _k;
|
||||
|
||||
Ref<MLPPMatrix> _x_normalized;
|
||||
Ref<MLPPMatrix> _u_reduce;
|
||||
Ref<MLPPMatrix> _z;
|
||||
};
|
||||
|
||||
#endif /* PCA_hpp */
|
||||
|
@ -21,7 +21,7 @@ std::vector<std::vector<real_t>> MLPPPCAOld::principalComponents() {
|
||||
MLPPLinAlg alg;
|
||||
MLPPData data;
|
||||
|
||||
MLPPLinAlg::SDVResultOld svr_res = alg.SVD(alg.cov(inputSet));
|
||||
MLPPLinAlg::SVDResultOld svr_res = alg.SVD(alg.cov(inputSet));
|
||||
X_normalized = data.meanCentering(inputSet);
|
||||
U_reduce.resize(svr_res.U.size());
|
||||
for (int i = 0; i < k; i++) {
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
real_t MLPPStat::b0Estimation(const std::vector<real_t> &x, const std::vector<real_t> &y) {
|
||||
return mean(y) - b1Estimation(x, y) * mean(x);
|
||||
}
|
||||
@ -115,6 +114,37 @@ real_t MLPPStat::chebyshevIneq(const real_t k) {
|
||||
return 1 - 1 / (k * k);
|
||||
}
|
||||
|
||||
real_t MLPPStat::meanv(const Ref<MLPPVector> &x) {
|
||||
int x_size = x->size();
|
||||
const real_t *x_ptr = x->ptr();
|
||||
|
||||
real_t sum = 0;
|
||||
for (int i = 0; i < x_size; ++i) {
|
||||
sum += x_ptr[i];
|
||||
}
|
||||
|
||||
return sum / x_size;
|
||||
}
|
||||
|
||||
real_t MLPPStat::covariancev(const Ref<MLPPVector> &x, const Ref<MLPPVector> &y) {
|
||||
ERR_FAIL_COND_V(x->size() != y->size(), 0);
|
||||
|
||||
real_t x_mean = meanv(x);
|
||||
real_t y_mean = meanv(y);
|
||||
|
||||
int x_size = x->size();
|
||||
const real_t *x_ptr = x->ptr();
|
||||
const real_t *y_ptr = y->ptr();
|
||||
|
||||
real_t sum = 0;
|
||||
|
||||
for (int i = 0; i < x_size; ++i) {
|
||||
sum += (x_ptr[i] - x_mean) * (y_ptr[i] - y_mean);
|
||||
}
|
||||
|
||||
return sum / (x_size - 1);
|
||||
}
|
||||
|
||||
real_t MLPPStat::weightedMean(const std::vector<real_t> &x, const std::vector<real_t> &weights) {
|
||||
real_t sum = 0;
|
||||
real_t weights_sum = 0;
|
||||
|
@ -10,8 +10,10 @@
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
|
||||
#include <vector>
|
||||
#include "../lin_alg/mlpp_matrix.h"
|
||||
#include "../lin_alg/mlpp_vector.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class MLPPStat {
|
||||
public:
|
||||
@ -33,6 +35,9 @@ public:
|
||||
real_t R2(const std::vector<real_t> &x, const std::vector<real_t> &y);
|
||||
real_t chebyshevIneq(const real_t k);
|
||||
|
||||
real_t meanv(const Ref<MLPPVector> &x);
|
||||
real_t covariancev(const Ref<MLPPVector> &x, const Ref<MLPPVector> &y);
|
||||
|
||||
// Extras
|
||||
real_t weightedMean(const std::vector<real_t> &x, const std::vector<real_t> &weights);
|
||||
real_t geometricMean(const std::vector<real_t> &x);
|
||||
@ -50,5 +55,4 @@ public:
|
||||
real_t logMean(const real_t x, const real_t y);
|
||||
};
|
||||
|
||||
|
||||
#endif /* Stat_hpp */
|
||||
|
@ -38,6 +38,7 @@ SOFTWARE.
|
||||
|
||||
#include "mlpp/kmeans/kmeans.h"
|
||||
#include "mlpp/knn/knn.h"
|
||||
#include "mlpp/pca/pca.h"
|
||||
#include "mlpp/wgan/wgan.h"
|
||||
|
||||
#include "mlpp/mlp/mlp.h"
|
||||
@ -63,6 +64,7 @@ void register_pmlpp_types(ModuleRegistrationLevel p_level) {
|
||||
|
||||
ClassDB::register_class<MLPPMLP>();
|
||||
ClassDB::register_class<MLPPWGAN>();
|
||||
ClassDB::register_class<MLPPPCA>();
|
||||
|
||||
ClassDB::register_class<MLPPDataESimple>();
|
||||
ClassDB::register_class<MLPPDataSimple>();
|
||||
|
@ -48,8 +48,8 @@
|
||||
#include "../mlpp/wgan/wgan.h"
|
||||
|
||||
#include "../mlpp/mlp/mlp_old.h"
|
||||
#include "../mlpp/wgan/wgan_old.h"
|
||||
#include "../mlpp/pca/pca_old.h"
|
||||
#include "../mlpp/wgan/wgan_old.h"
|
||||
|
||||
Vector<real_t> dstd_vec_to_vec(const std::vector<real_t> &in) {
|
||||
Vector<real_t> r;
|
||||
@ -719,25 +719,53 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) {
|
||||
std::cout << "Eigenvalues:" << std::endl;
|
||||
alg.printMatrix(eigen.eigen_values);
|
||||
|
||||
std::cout << "SVD" << std::endl;
|
||||
std::cout << "SVD OLD START" << std::endl;
|
||||
|
||||
MLPPLinAlg::SDVResultOld svd = alg.SVD(inputSet);
|
||||
MLPPLinAlg::SVDResultOld svd_old = alg.SVD(inputSet);
|
||||
|
||||
std::cout << "U:" << std::endl;
|
||||
alg.printMatrix(svd.U);
|
||||
alg.printMatrix(svd_old.U);
|
||||
std::cout << "S:" << std::endl;
|
||||
alg.printMatrix(svd.S);
|
||||
alg.printMatrix(svd_old.S);
|
||||
std::cout << "Vt:" << std::endl;
|
||||
alg.printMatrix(svd.Vt);
|
||||
alg.printMatrix(svd_old.Vt);
|
||||
|
||||
std::cout << "SVD OLD FIN" << std::endl;
|
||||
|
||||
Ref<MLPPMatrix> input_set;
|
||||
input_set.instance();
|
||||
input_set->set_from_std_vectors(inputSet);
|
||||
|
||||
String str_svd = "SVD\n";
|
||||
|
||||
MLPPLinAlg::SVDResult svd = alg.svd(input_set);
|
||||
|
||||
str_svd += "U:\n";
|
||||
str_svd += svd.U->to_string();
|
||||
str_svd += "\nS:\n";
|
||||
str_svd += svd.S->to_string();
|
||||
str_svd += "\nVt:\n";
|
||||
str_svd += svd.Vt->to_string();
|
||||
str_svd += "\n";
|
||||
|
||||
PLOG_MSG(str_svd);
|
||||
|
||||
std::cout << "PCA" << std::endl;
|
||||
|
||||
// PCA done using Jacobi's method to approximate eigenvalues and eigenvectors.
|
||||
MLPPPCAOld dr(inputSet, 1); // 1 dimensional representation.
|
||||
MLPPPCAOld dr_old(inputSet, 1); // 1 dimensional representation.
|
||||
std::cout << std::endl;
|
||||
std::cout << "Dimensionally reduced representation:" << std::endl;
|
||||
alg.printMatrix(dr.principalComponents());
|
||||
std::cout << "SCORE: " << dr.score() << std::endl;
|
||||
std::cout << "OLD Dimensionally reduced representation:" << std::endl;
|
||||
alg.printMatrix(dr_old.principalComponents());
|
||||
std::cout << "SCORE: " << dr_old.score() << std::endl;
|
||||
|
||||
// PCA done using Jacobi's method to approximate eigenvalues and eigenvectors.
|
||||
MLPPPCA dr(input_set, 1); // 1 dimensional representation.
|
||||
|
||||
String str = "\nDimensionally reduced representation:\n";
|
||||
str += dr.principal_components()->to_string();
|
||||
str += "\nSCORE: " + String::num(dr.score()) + "\n";
|
||||
PLOG_MSG(str);
|
||||
}
|
||||
|
||||
void MLPPTests::test_nlp_and_data(bool ui) {
|
||||
|
Loading…
Reference in New Issue
Block a user