/usr/include/vmmlib/t3_hosvd.hpp is in libvmmlib-dev 1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | /*
* VMMLib - Tensor Classes
*
* @author Susanne Suter
*
* The Tucker3 tensor class is consists of the same components (core tensor, basis matrices u1-u3) as the tucker3 model described in:
* - Tucker, 1966: Some mathematical notes on three-mode factor analysis, Psychometrika.
* - Kroonenberg & De Leeuw, 1980: Principal component analysis of three-mode data by means of alternating least squares algorithms. Psychometrika. (TUCKALS)
* - De Lathauwer, De Moor, Vandewalle, 2000a: A multilinear singular value decomposition, SIAM J. Matrix Anal. Appl.
* - Kolda & Bader, 2009: Tensor Decompositions and Applications, SIAM Review.
* - Bader & Kolda, 2006: Algorithm 862: Matlab tensor classes for fast algorithm prototyping. ACM Transactions on Mathematical Software.
*
*/
#ifndef __VMML__T3_HOSVD__HPP__
#define __VMML__T3_HOSVD__HPP__
#include <vmmlib/tensor3.hpp>
#include <vmmlib/lapack_svd.hpp>
#include <vmmlib/lapack_sym_eigs.hpp>
#include <vmmlib/blas_dgemm.hpp>
enum hosvd_method {
eigs_e,
svd_e
};
namespace vmml
{
template< size_t R1, size_t R2, size_t R3, size_t I1, size_t I2, size_t I3, typename T = float >
class t3_hosvd
{
public:
typedef double T_svd;
typedef tensor3< I1, I2, I3, T > t3_type;
typedef matrix< I1, R1, T > u1_type;
typedef matrix< I2, R2, T > u2_type;
typedef matrix< I3, R3, T > u3_type;
typedef matrix< I1, I2*I3, T > u1_unfolded_type;
typedef matrix< I2, I1*I3, T > u2_unfolded_type;
typedef matrix< I3, I1*I2, T > u3_unfolded_type;
typedef matrix< I1, I1, T > u1_cov_type;
typedef matrix< I2, I2, T > u2_cov_type;
typedef matrix< I3, I3, T > u3_cov_type;
/* higher-order singular value decomposition (HOSVD) with full rank decomposition (also known as Tucker decomposition).
see: De Lathauer et al, 2000a: A multilinear singular value decomposition.
the hosvd can be computed (a) with n-mode PCA, i.e., an eigenvalue decomposition on the covariance matrix of every mode's matricization, and
(b) by performing a 2D SVD on the matricization of every mode. Matrix matricization means that a tensor I1xI2xI3 is unfolded/sliced into one matrix
with the dimensions I1xI2I3, which corresponds to a matrizitation alonge mode I1.
other known names for HOSVD: n-mode SVD, 3-mode factor analysis (3MFA, tucker3), 3M-PCA, n-mode PCA, higher-order SVD
*/
static void apply_mode1( const t3_type& data_, u1_type& u1_, hosvd_method method_ = eigs_e );
static void apply_mode2( const t3_type& data_, u2_type& u2_, hosvd_method method_ = eigs_e );
static void apply_mode3( const t3_type& data_, u3_type& u3_, hosvd_method method_ = eigs_e );
static void apply_all( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_, hosvd_method method_ = eigs_e );
static void hosvd( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_ );
static void hoeigs( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_ );
protected:
//hosvd
template< size_t M, size_t N, size_t R >
static void get_svd_u_red( const matrix< M, N, T >& data_, matrix< M, R, T >& u_ );
static void svd_mode1( const t3_type& data_, u1_type& u1_ );
static void svd_mode2( const t3_type& data_, u2_type& u2_ );
static void svd_mode3( const t3_type& data_, u3_type& u3_ );
//hosvd on eigenvalue decomposition = hoeigs
template< size_t N, size_t R >
static void get_eigs_u_red( const matrix< N, N, T >& data_, matrix< N, R, T >& u_ );
static void eigs_mode1( const t3_type& data_, u1_type& u1_ );
static void eigs_mode2( const t3_type& data_, u2_type& u2_ );
static void eigs_mode3( const t3_type& data_, u3_type& u3_ );
}; //end hosvd class
#define VMML_TEMPLATE_STRING template< size_t R1, size_t R2, size_t R3, size_t I1, size_t I2, size_t I3, typename T >
#define VMML_TEMPLATE_CLASSNAME t3_hosvd< R1, R2, R3, I1, I2, I3, T >
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::hosvd( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_ )
{
svd_mode1( data_, u1_ );
svd_mode2( data_, u2_ );
svd_mode3( data_, u3_ );
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::hoeigs( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_ )
{
eigs_mode1( data_, u1_ );
eigs_mode2( data_, u2_ );
eigs_mode3( data_, u3_ );
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::apply_all( const t3_type& data_, u1_type& u1_, u2_type& u2_, u3_type& u3_, hosvd_method method_ )
{
apply_mode1( data_, u1_, method_ );
apply_mode2( data_, u2_, method_ );
apply_mode3( data_, u3_, method_ );
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::apply_mode1( const t3_type& data_, u1_type& u1_, hosvd_method method_ )
{
switch ( method_ )
{
case 0:
eigs_mode1( data_, u1_ );
break;
case 1:
svd_mode1( data_, u1_ );
break;
default:
eigs_mode1( data_, u1_ );
}
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::apply_mode2( const t3_type& data_, u2_type& u2_, hosvd_method method_ )
{
switch ( method_ )
{
case 0:
eigs_mode2( data_, u2_ );
break;
case 1:
svd_mode2( data_, u2_ );
break;
default:
eigs_mode2( data_, u2_ );
}
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::apply_mode3( const t3_type& data_, u3_type& u3_, hosvd_method method_ )
{
switch ( method_ )
{
case 0:
eigs_mode3( data_, u3_ );
break;
case 1:
svd_mode3( data_, u3_ );
break;
default:
eigs_mode3( data_, u3_ );
}
}
/* SVD along mode 1, 2, and 3*/
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::svd_mode1( const t3_type& data_, u1_type& u1_ )
{
u1_unfolded_type* u = new u1_unfolded_type; // -> u1
data_.lateral_unfolding_bwd( *u );
get_svd_u_red( *u, u1_ );
delete u;
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::svd_mode2( const t3_type& data_, u2_type& u2_ )
{
u2_unfolded_type* u = new u2_unfolded_type; // -> u2
data_.frontal_unfolding_bwd( *u );
get_svd_u_red( *u, u2_ );
delete u;
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::svd_mode3( const t3_type& data_, u3_type& u3_ )
{
u3_unfolded_type* u = new u3_unfolded_type; // -> u3
data_.horizontal_unfolding_bwd( *u );
get_svd_u_red( *u, u3_ );
delete u;
}
/* EIGS for mode 1, 2 and 3*/
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::eigs_mode1( const t3_type& data_, u1_type& u1_ )
{
//unfolding / matricization
u1_unfolded_type* m_lateral = new u1_unfolded_type; // -> u1
data_.lateral_unfolding_bwd( *m_lateral );
//covariance matrix of unfolded data
u1_cov_type* cov = new u1_cov_type;
blas_dgemm< I1, I2*I3, I1, T>* blas_cov = new blas_dgemm< I1, I2*I3, I1, T>;
blas_cov->compute( *m_lateral, *cov );
delete blas_cov;
delete m_lateral;
//compute x largest magnitude eigenvalues; x = R
get_eigs_u_red( *cov, u1_ );
delete cov;
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::eigs_mode2( const t3_type& data_, u2_type& u2_ )
{
//unfolding / matricization
u2_unfolded_type* m_frontal = new u2_unfolded_type; // -> u2
data_.frontal_unfolding_bwd( *m_frontal );
//covariance matrix of unfolded data
u2_cov_type* cov = new u2_cov_type;
blas_dgemm< I2, I1*I3, I2, T>* blas_cov = new blas_dgemm< I2, I1*I3, I2, T>;
blas_cov->compute( *m_frontal, *cov );
delete blas_cov;
delete m_frontal;
//compute x largest magnitude eigenvalues; x = R
get_eigs_u_red( *cov, u2_ );
delete cov;
}
VMML_TEMPLATE_STRING
void
VMML_TEMPLATE_CLASSNAME::eigs_mode3( const t3_type& data_, u3_type& u3_)
{
//unfolding / matricization
u3_unfolded_type* m_horizontal = new u3_unfolded_type; // -> u3
data_.horizontal_unfolding_bwd( *m_horizontal );
//covariance matrix of unfolded data
u3_cov_type* cov = new u3_cov_type;
blas_dgemm< I3, I1*I2, I3, T>* blas_cov = new blas_dgemm< I3, I1*I2, I3, T>;
blas_cov->compute( *m_horizontal, *cov );
delete blas_cov;
delete m_horizontal;
//compute x largest magnitude eigenvalues; x = R
get_eigs_u_red( *cov, u3_ );
delete cov;
}
/* helper methods for SVD and EIGS*/
VMML_TEMPLATE_STRING
template< size_t N, size_t R >
void
VMML_TEMPLATE_CLASSNAME::get_eigs_u_red( const matrix< N, N, T >& data_, matrix< N, R, T >& u_ )
{
typedef matrix< N, N, T_svd > cov_matrix_type;
typedef vector< R, T_svd > eigval_type;
typedef matrix< N, R, T_svd > eigvec_type;
//typedef matrix< N, R, T_coeff > coeff_type;
//compute x largest magnitude eigenvalues; x = R
eigval_type* eigxvalues = new eigval_type;
eigvec_type* eigxvectors = new eigvec_type;
lapack_sym_eigs< N, T_svd > eigs;
cov_matrix_type* data = new cov_matrix_type;
data->cast_from( data_ );
if( eigs.compute_x( *data, *eigxvectors, *eigxvalues) ) {
/*if( _is_quantify_coeff ){
coeff_type* evec_quant = new coeff_type;
T min_value = 0; T max_value = 0;
u_.cast_from( *eigxvectors );
u_.quantize( *evec_quant, min_value, max_value );
evec_quant->dequantize( u_, min_value, max_value );
delete evec_quant;
} else */ if ( sizeof( T ) != 4 ){
u_.cast_from( *eigxvectors );
} else {
u_ = *eigxvectors;
}
} else {
u_.zero();
}
delete eigxvalues;
delete eigxvectors;
delete data;
}
VMML_TEMPLATE_STRING
template< size_t M, size_t N, size_t R >
void
VMML_TEMPLATE_CLASSNAME::get_svd_u_red( const matrix< M, N, T >& data_, matrix< M, R, T >& u_ )
{
typedef matrix< M, N, T_svd > svd_m_type;
//FIXME: typedef matrix< M, N, T_coeff > coeff_type;
typedef matrix< M, N, T > m_type;
typedef vector< N, T_svd > lambdas_type;
svd_m_type* u_double = new svd_m_type;
u_double->cast_from( data_ );
//FIXME:: coeff_type* u_quant = new coeff_type;
m_type* u_out = new m_type;
lambdas_type* lambdas = new lambdas_type;
lapack_svd< M, N, T_svd >* svd = new lapack_svd< M, N, T_svd >();
if( svd->compute_and_overwrite_input( *u_double, *lambdas )) {
/* if( _is_quantify_coeff ){
T min_value = 0; T max_value = 0;
u_comp->cast_from( *u_double );
//FIXME: u_comp->quantize( *u_quant, min_value, max_value );
//FIXME: u_quant->dequantize( *u_internal, min_value, max_value );
} else */ if ( sizeof( T ) != 4 ){
u_out->cast_from( *u_double );
} else {
*u_out = *u_double;
}
u_out->get_sub_matrix( u_ );
} else {
u_.zero();
}
delete lambdas;
delete svd;
delete u_double;
//delete u_quant;
delete u_out;
}
#undef VMML_TEMPLATE_STRING
#undef VMML_TEMPLATE_CLASSNAME
}//end vmml namespace
#endif
|