28 using namespace Eigen;
34 m_tolerance=tolerance;
35 m_store_cov=store_cov;
44 m_tolerance=tolerance;
45 m_store_cov=store_cov;
77 void CMCLDA::cleanup()
87 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
108 for (
int i = 0; i < num_vecs; i++)
110 vec = rf->get_feature_vector(i, vlen, vfree);
115 X.row(i) = Evec - Em_xbar;
117 rf->free_feature_vector(vec, i, vfree);
121 SG_PRINT(
"\n>>> Displaying X ...\n")
131 SG_PRINT(
"\n>>> Displaying Xs ...\n")
136 MatrixXd d(num_vecs, m_num_classes);
139 d = (Xs*Em_coef.transpose()).rowwise() + Em_intercept.transpose();
142 SG_PRINT(
"\n>>> Displaying d ...\n")
148 for (
int i = 0; i < num_vecs; i++)
157 SG_ERROR(
"No labels allocated in MCLDA training\n")
162 SG_ERROR(
"Speficied features are not of type CDotFeatures\n")
168 SG_ERROR(
"No features allocated in MCLDA training\n")
173 SG_ERROR(
"No train_labels allocated in MCLDA training\n")
181 if (num_vec != train_labels.
vlen)
182 SG_ERROR(
"Dimension mismatch between features and labels in MCLDA training")
184 int32_t* class_idxs = SG_MALLOC(int32_t, num_vec*m_num_classes);
185 int32_t* class_nums = SG_MALLOC(int32_t, m_num_classes);
186 memset(class_nums, 0, m_num_classes*
sizeof(int32_t));
188 for (
int i = 0; i < train_labels.
vlen; i++)
190 int32_t class_idx = train_labels.
vector[i];
192 if (class_idx < 0 || class_idx >= m_num_classes)
194 SG_ERROR(
"found label out of {0, 1, 2, ..., num_classes-1}...")
199 class_idxs[ class_idx*num_vec + class_nums[class_idx]++ ] = i;
203 for (
int i = 0; i < m_num_classes; i++)
205 if (class_nums[i] <= 0)
207 SG_ERROR(
"What? One class with no elements\n")
218 cov_dims[2] = m_num_classes;
224 MatrixXd X = MatrixXd::Zero(num_vec, m_dim);
233 for (
int k = 0; k < m_num_classes; k++)
236 MatrixXd buffer(class_nums[k], m_dim);
238 for (
int i = 0; i < class_nums[k]; i++)
245 buffer.row(i) = Evec;
250 Em_mean /= class_nums[k];
253 for (
int i = 0; i < class_nums[k]; i++)
255 buffer.row(i) -= Em_mean;
256 X.row(iX) += buffer.row(i);
264 Em_cov_k = buffer.transpose() * buffer;
269 SG_PRINT(
"\n>>> Displaying means ...\n")
279 for (
int k = 0; k < m_num_classes; k++)
285 Em_cov /= m_num_classes;
291 SG_PRINT(
"\n>>> Displaying cov ...\n")
303 Em_xbar = X.colwise().sum();
306 VectorXd
std = VectorXd::Zero(m_dim);
307 std = (X.rowwise() - Em_xbar.transpose()).array().pow(2).colwise().sum();
308 std = std.array() / num_vec;
310 for (
int j = 0; j < m_dim; j++)
314 float64_t fac = 1.0 / (num_vec - m_num_classes);
317 SG_PRINT(
"\n>>> Displaying m_xbar ...\n")
320 SG_PRINT(
"\n>>> Displaying std ...\n")
326 for (
int i = 0; i < num_vec; i++)
327 X.row(i) = sqrt(fac) * X.row(i).array() / std.transpose().array();
334 Eigen::JacobiSVD<MatrixXd> eSvd;
335 eSvd.compute(X,Eigen::ComputeFullV);
336 memcpy(S.data(), eSvd.singularValues().data(), m_dim*
sizeof(
float64_t));
337 memcpy(V.data(), eSvd.matrixV().data(), m_dim*m_dim*
sizeof(
float64_t));
338 V.transposeInPlace();
341 while (rank < m_dim && S[rank] > m_tolerance)
347 SG_ERROR(
"Warning: Variables are collinear\n")
350 for (
int i = 0; i < m_dim; i++)
351 for (
int j = 0; j < rank; j++)
352 scalings(i,j) = V(j,i) / std[j] / S[j];
355 SG_PRINT(
"\n>>> Displaying scalings ...\n")
365 Xc = (Em_means.transpose()*scalings);
367 for (
int i = 0; i < m_num_classes; i++)
368 Xc.row(i) *= sqrt(class_nums[i] * fac);
376 eSvd.compute(Xc,Eigen::ComputeFullV);
377 memcpy(S.data(), eSvd.singularValues().data(), rank*
sizeof(
float64_t));
378 memcpy(V.data(), eSvd.matrixV().data(), rank*rank*
sizeof(
float64_t));
381 while (m_rank < rank && S[m_rank] > m_tolerance*S[0])
389 Em_scalings = scalings * V.leftCols(m_rank);
392 SG_PRINT(
"\n>>> Displaying m_scalings ...\n")
397 MatrixXd meansc(m_dim, m_num_classes);
398 meansc = Em_means.colwise() - Em_xbar;
402 Em_coef = meansc.transpose() * Em_scalings;
405 SG_PRINT(
"\n>>> Displaying m_coefs ...\n")
412 for (
int j = 0; j < m_num_classes; j++)
413 m_intercept[j] = -0.5*m_coef[j]*m_coef[j] + log(class_nums[j]/
float(num_vec));
416 SG_PRINT(
"\n>>> Displaying m_intercept ...\n")
void display_matrix(const char *name="matrix") const
CMCLDA(float64_t tolerance=1e-4, bool store_cov=false)
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
experimental abstract native multiclass machine class
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
virtual bool train_machine(CFeatures *data=NULL)
The class Labels models labels, i.e. class assignments of objects.
virtual int32_t get_num_vectors() const =0
virtual void set_features(CDotFeatures *feat)
Features that support dot products among other operations.
T * get_matrix(index_t matIdx) const
virtual int32_t get_dim_feature_space() const =0
void display_vector(const char *name="vector", const char *prefix="") const
Multiclass Labels for multi-class classification.
Class SGObject is the base class of all shogun objects.
T * get_column_vector(index_t col) const
Matrix< float64_t,-1,-1, 0,-1,-1 > MatrixXd
all of classes and functions are contained in the shogun namespace
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
The class Features is the base class of all feature objects.
bool has_property(EFeatureProperty p) const
virtual void set_labels(CLabels *lab)