本文整理汇总了C++中mat::elem方法的典型用法代码示例。如果您正苦于以下问题:C++ mat::elem方法的具体用法?C++ mat::elem怎么用?C++ mat::elem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mat
的用法示例。
在下文中一共展示了mat::elem方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
inline
typename detail::enable_if< is_floating_point< T >, T >::type
invert( mat< T, 1 >& m )
{
if ( std::fabs( m.elem( 0 ) ) <= std::numeric_limits< T >::epsilon() )
{
return 0;
}
T r = m.elem( 0 );
m.elem( 0 ) = static_cast< T >( 1 ) / m.elem( 0 );
return r;
}
示例2: nnmf
//[[Rcpp::export]]
Rcpp::List nnmf(const mat & A, const unsigned int k, mat W, mat H, umat Wm, umat Hm,
const vec & alpha, const vec & beta, const unsigned int max_iter, const double rel_tol,
const int n_threads, const int verbose, const bool show_warning, const unsigned int inner_max_iter,
const double inner_rel_tol, const int method, unsigned int trace)
{
/******************************************************************************************************
* Non-negative Matrix Factorization(NNMF) using alternating scheme
* ----------------------------------------------------------------
* Description:
* Decompose matrix A such that
* A = W H
* Arguments:
* A : Matrix to be decomposed
* W, H : Initial matrices of W and H, where ncol(W) = nrow(H) = k. # of rows/columns of W/H could be 0
* Wm, Hm : Masks of W and H, s.t. masked entries are no-updated and fixed to initial values
* alpha : [L2, angle, L1] regularization on W (non-masked entries)
* beta : [L2, angle, L1] regularization on H (non-masked entries)
* max_iter : Maximum number of iteration
* rel_tol : Relative tolerance between two successive iterations, = |e2-e1|/avg(e1, e2)
* n_threads : Number of threads (openMP)
* verbose : Either 0 = no any tracking, 1 == progression bar, 2 == print iteration info
* show_warning : If to show warning if targeted `tol` is not reached
* inner_max_iter : Maximum number of iterations passed to each inner W or H matrix updating loop
* inner_rel_tol : Relative tolerance passed to inner W or H matrix updating loop, = |e2-e1|/avg(e1, e2)
* method : Integer of 1, 2, 3 or 4, which encodes methods
* : 1 = sequential coordinate-wise minimization using square loss
* : 2 = Lee's multiplicative update with square loss, which is re-scaled gradient descent
* : 3 = sequentially quadratic approximated minimization with KL-divergence
* : 4 = Lee's multiplicative update with KL-divergence, which is re-scaled gradient descent
* trace : A positive integer, error will be checked very 'trace' iterations. Computing WH can be very expansive,
* : so one may not want to check error A-WH every single iteration
* Return:
* A list (Rcpp::List) of
* W, H : resulting W and H matrices
* mse_error : a vector of mean square error (divided by number of non-missings)
* mkl_error : a vector (length = number of iterations) of mean KL-distance
* target_error : a vector of loss (0.5*mse or mkl), plus constraints
* average_epoch : a vector of average epochs (one complete swap over W and H)
* Author:
* Eric Xihui Lin <[email protected]>
* Version:
* 2015-12-11
******************************************************************************************************/
unsigned int n = A.n_rows;
unsigned int m = A.n_cols;
//int k = H.n_rows; // decomposition rank k
unsigned int N_non_missing = n*m;
if (trace < 1) trace = 1;
unsigned int err_len = (unsigned int)std::ceil(double(max_iter)/double(trace)) + 1;
vec mse_err(err_len), mkl_err(err_len), terr(err_len), ave_epoch(err_len);
// check progression
bool show_progress = false;
if (verbose == 1) show_progress = true;
Progress prgrss(max_iter, show_progress);
double rel_err = rel_tol + 1;
double terr_last = 1e99;
uvec non_missing;
bool any_missing = !A.is_finite();
if (any_missing)
{
non_missing = find_finite(A);
N_non_missing = non_missing.n_elem;
mkl_err.fill(mean((A.elem(non_missing)+TINY_NUM) % log(A.elem(non_missing)+TINY_NUM) - A.elem(non_missing)));
}
else
mkl_err.fill(mean(mean((A+TINY_NUM) % log(A+TINY_NUM) - A))); // fixed part in KL-dist, mean(A log(A) - A)
if (Wm.empty())
Wm.resize(0, n);
else
inplace_trans(Wm);
if (Hm.empty())
Hm.resize(0, m);
if (W.empty())
{
W.randu(k, n);
W *= 0.01;
if (!Wm.empty())
W.elem(find(Wm > 0)).fill(0.0);
}
else
inplace_trans(W);
if (H.empty())
{
H.randu(k, m);
H *= 0.01;
if (!Hm.empty())
H.elem(find(Hm > 0)).fill(0.0);
}
if (verbose == 2)
{
Rprintf("\n%10s | %10s | %10s | %10s | %10s\n", "Iteration", "MSE", "MKL", "Target", "Rel. Err.");
//.........这里部分代码省略.........