/*! * \brief forward * X: [N, C, 1, 1], usually the output of affine(fc) layer * Y: [N, C, 1, 1], ground truth, with 1(true) or 0(false) * \param[in] const vector<Blob*>& in in[0]:X, in[1]:Y * \param[out] double& loss loss * \param[out] Blob** out out: dX * \param[in] int mode 1: only forward, 0:forward and backward */ void SVMLossLayer::go(const vector<shared_ptr<Blob>>& in, double& loss, shared_ptr<Blob>& dout, int mode) { if (dout) { dout.reset(); } /*! let delta equals to 1 */ double delta = 0.2; int N = in[0]->get_N(); int C = in[0]->get_C(); mat mat_x = in[0]->reshape(); mat mat_y = in[1]->reshape(); //mat_x.print("X:\n"); //mat_y.print("Y:\n"); /*! forward */ mat good_x = repmat(arma::sum(mat_x % mat_y, 1), 1, C); mat mat_loss = (mat_x - good_x + delta); mat_loss.transform([](double e) {return e > 0 ? e : 0;}); mat_y.transform([](double e) {return e ? 0 : 1;}); mat_loss %= mat_y; loss = accu(mat_loss) / N; if (mode == 1) return; /*! backward */ mat dx(mat_loss); dx.transform([](double e) {return e ? 1 : 0;}); mat_y.transform([](double e) {return e ? 0 : 1;}); mat sum_x = repmat(arma::sum(dx, 1), 1, C) % mat_y; dx = (dx - sum_x) / N; mat2Blob(dx, dout, in[0]->size()); return; }
/* Constructor accepts feature values, class labels (-1 or +1) and sample weights; Passed data will be stored as a reference to original data; */ DSC::DSC(const mat &features, const ivec &classes, const vec &weights, const int &nThresholds) : N_THRESHOLDS(nThresholds), features(features), classes(classes), weights(weights) { /* Separating features by classes: */ mat class1Features = features.rows(find(classes == -1)); mat class2Features = features.rows(find(classes == +1)); /* Determining the ranges for each feature: */ minFeatures = min(features) - 1e-10; maxFeatures = max(features) + 1e-10; ranges = maxFeatures - minFeatures; /* Distribution of threshold values for each feature; */ mat class1ThresholdsMat = floor(((class1Features.each_row() - minFeatures).each_row() / ranges) * (N_THRESHOLDS - 1) + 1 - 1e-10); class1ThresholdsMat(find(class1ThresholdsMat < 0)).zeros(); mat class2ThresholdsMat = ceil(((class2Features.each_row() - minFeatures).each_row() / ranges) * (N_THRESHOLDS - 1) + 1 + 1e-10); class2ThresholdsMat(find(class2ThresholdsMat > N_THRESHOLDS - 1)).fill(N_THRESHOLDS - 1); /* Threshold values is vectorized by column: */ class1Thresholds = vectorise(class1ThresholdsMat); class2Thresholds = vectorise(class2ThresholdsMat); /* Providing indexes of features for each label; After that matrix is vectorized by column: */ featureIndexes1 = vectorise(repmat(linspace<rowvec>(0, features.n_cols - 1, features.n_cols), class1Features.n_rows, 1)); featureIndexes2 = vectorise(repmat(linspace<rowvec>(0, features.n_cols - 1, features.n_cols), class2Features.n_rows, 1)); }
// creates a mesh grid of the two arrays. template <class T> inline array<array_2d<T> > meshgrid(const index_array& a, const index_array& b) { array<array_nd<T> > c(2); c[0] = repmat(array_nd<T>(idx(1,a.length()),a.cast()), idx(b.length() - 1,0)); c[1] = repmat(array_nd<T>(idx(b.length(),1),b.cast()), idx(0,a.length() - 1)); return c; }
const matrix<T,N,A> repmat( const matrix<T,N,A>& m, const std::size_t r, const std::size_t c ) { assert( r ); assert( c ); if ( 1 == r && 1 == c ) return m; if ( 1 == r ) return repmat( m, 1, c-1 ) || m; if ( 1 == c ) return repmat( m, r-1, 1 ) && m; return repmat( repmat( m, 1, c), r, 1 ); }
inline void op_princomp::direct_princomp ( Mat< std::complex<T> >& coeff_out, const Mat< std::complex<T> >& in ) { arma_extra_debug_sigprint(); typedef typename std::complex<T> eT; if(in.n_elem != 0) { // singular value decomposition Mat<eT> U; Col< T> s; const Mat<eT> tmp = in - repmat(mean(in), in.n_rows, 1); const bool svd_ok = svd(U,s,coeff_out, tmp); if(svd_ok == false) { arma_print("princomp(): singular value decomposition failed"); coeff_out.reset(); } } else { coeff_out.reset(); } }
inline bool op_princomp::direct_princomp ( Mat<eT>& coeff_out, const Mat<eT>& in ) { arma_extra_debug_sigprint(); if(in.n_elem != 0) { // singular value decomposition Mat<eT> U; Col<eT> s; const Mat<eT> tmp = in - repmat(mean(in), in.n_rows, 1); const bool svd_ok = svd(U,s,coeff_out, tmp); if(svd_ok == false) { return false; } } else { coeff_out.eye(in.n_cols, in.n_cols); } return true; }
/// /// \brief PLSData::Apply /// \param spectra Input matrix /// \param wavelength Spectral abscissa /// \param components Number of components to calculate /// \return /// Performs PLS analysis on the spectra matrix. bool PLSData::Classify(const mat &spectra, const vec &wavelength, int components) { mat Y = repmat(wavelength, 1, components); mat X_loadings, Y_loadings, X_scores, Y_scores, coefficients, percent_variance, fitted; bool success = Vespucci::Math::DimensionReduction::plsregress(spectra, Y, components, X_loadings, Y_loadings, X_scores, Y_scores, coefficients, percent_variance, fitted); //mat residuals = fitted - spectra; if (success){ AddMetadata("Type", "Classification (PCA)"); AddMetadata("Components calculated", QString::number(components)); AddMatrix("Percent Variance", percent_variance); AddMatrix("Predictor Loadings", X_loadings); AddMatrix("Response Loadings", Y_loadings); AddMatrix("Predictor Scores", X_scores); AddMatrix("Response Scores", Y_scores); AddMatrix("Coefficients", coefficients); AddMatrix("Fitted Data", fitted); //AddMatrix("Residuals", residuals); } return success; }
void ArmadilloSolver::solve_irls() { // Weight vector fvec wsq = ones<fmat>(y.n_elem); //fvec wsq_ = ones<fmat>(y.n_elem); //fvec err_u = ones<fmat>(y.n_elem/2); //fvec err_v = ones<fmat>(y.n_elem/2); fmat A_(A.n_rows, A.n_cols); //fvec y_(y.n_elem); fvec x_(A.n_cols); fmat T1(A.n_cols, A.n_cols); fvec T2(A.n_cols); if (this->debug) { std::cout << "[DEBUG] Solver dimensions: " << std::endl; std::cout << "[DEBUG] \t T1: " << T1.n_rows << " x " << T1.n_cols << std::endl; std::cout << "[DEBUG] \t T2: " << T2.n_rows << " x " << T2.n_cols << std::endl; std::cout << "[DEBUG] \t A: " << A.n_rows << " x " << A.n_cols << std::endl; std::cout << "[DEBUG] \t y: " << y.n_elem << std::endl; std::cout << "[DEBUG] \t x: " << x_.n_elem << std::endl; std::cout << "[DEBUG] \t Q: " << Q.n_rows << " x " << Q.n_cols << std::endl; } int iters = this->n_iters; for (int iter=0; iter < iters; iter++) { A_ = A; // Re-weight rows of matrix and result vector A_.each_col() %= wsq; //y_ = y % wsq; T1 = A_.t() * A_ + Q; T2 = A_.t() * (y % wsq); x_ = arma::solve(T1,T2); // Computing wsq is a little different, since we want to // weight both x and y direction of the pixel by the L2 error. wsq = square(A * x_ - y) * 1.0/this->sigma_sq; wsq = repmat(wsq.subvec(0,n_kp-1) + wsq.subvec(n_kp,2*n_kp-1),2,1); // Cauchy error // Note that the error would be 1.0/(1+f**2), but since we do not take the square // root above, we can omit the squaring here. for (int j=0; j < wsq.n_elem; j++) { wsq[j] = 1.0/(1.0+wsq[j]); } //wsq.transform( [](float f) {return 1.0/(1.0+f); } ); wsq = sqrt(wsq); } this->x = x_; this->wsq = square(wsq); }
/** Perform the operation. @returns True if successful false otherwise. **/ virtual bool Operate() { Mat<Number> X = MatrixOperator<Number>::_source; _mean = sum(X, 0) / X.n_rows; mat mX = repmat(_mean, X.n_rows, 1); Mat<Number> deltaX = X - mX; MatrixOperator<Number>::_result = deltaX; return true; }
inline bool op_princomp::direct_princomp ( Mat< std::complex<T> >& coeff_out, Mat< std::complex<T> >& score_out, const Mat< std::complex<T> >& in ) { arma_extra_debug_sigprint(); typedef std::complex<T> eT; const u32 n_rows = in.n_rows; const u32 n_cols = in.n_cols; if(n_rows > 1) // more than one sample { // subtract the mean - use score_out as temporary matrix score_out = in - repmat(mean(in), n_rows, 1); // singular value decomposition Mat<eT> U; Col< T> s; const bool svd_ok = svd(U,s,coeff_out,score_out); if(svd_ok == false) { return false; } // U.reset(); // normalize the eigenvalues s /= std::sqrt( double(n_rows - 1) ); // project the samples to the principals score_out *= coeff_out; if(n_rows <= n_cols) // number of samples is less than their dimensionality { score_out.cols(n_rows-1,n_cols-1).zeros(); } } else // 0 or 1 samples { coeff_out.eye(n_cols, n_cols); score_out.copy_size(in); score_out.zeros(); } return true; }
/* "getBestStump()" function saves the best decision stump's threshold, weak hypothesis and its error; */ void DSC::getBestStump(Threshold &threshold, ivec &hypothesis, double &error) { /* Separating weights by classes: */ mat class1Weights = weights(find(classes == -1)); mat class2Weights = weights(find(classes == +1)); /* Accumulate weights for given thresholds ("number of thresholds" x "number of features"): */ mat thresholdWeights1 = accumarray(join_rows(class1Thresholds, featureIndexes1), repmat(class1Weights, features.n_cols, 1), SizeMat(N_THRESHOLDS, features.n_cols)); mat thresholdWeights2 = accumarray(join_rows(class2Thresholds, featureIndexes2), repmat(class2Weights, features.n_cols, 1), SizeMat(N_THRESHOLDS, features.n_cols)); /* Looking for threshold with minimum error; Here we construct cummulative sum of weights for seaprate classes, then we add them; In order to find the smallest error, we consider both directions of a threshold; */ mat thresholdErrors = flipud(cumsum(flipud(thresholdWeights1))) + cumsum(thresholdWeights2); thresholdErrors = join_cols(thresholdErrors, sum(weights) - thresholdErrors); uword index; uword featureType; error = thresholdErrors.min(index, featureType); threshold.featureType = featureType; if (index > N_THRESHOLDS - 1) { threshold.direction = -1; index -= N_THRESHOLDS; } else { threshold.direction = +1; } threshold.value = minFeatures(featureType) + (ranges(featureType) / N_THRESHOLDS) * index; /* Evaluating hypothesis for derived threshold: */ classify(threshold, features, hypothesis); return; }
HorzRepsum::HorzRepsum(const MX& x, int n) : n_(n) { casadi_assert(x.size2() % n == 0); std::vector<Sparsity> sp = horzsplit(x.sparsity(), x.size2()/n); Sparsity block = sp[0]; for (int i=1;i<sp.size();++i) { block = block+sp[i]; } Sparsity goal = repmat(block, 1, n); setDependencies(project(x, goal)); setSparsity(block); }
/** Perform the operation. @returns True if successful false otherwise. **/ virtual bool Operate() { Mat<Number> source = MatrixOperator<Number>::_source; Mat<Number> mean = sum(source, 0) / source.n_rows; mean = repmat(mean, source.n_rows, 1); Mat<Number> delta = source - mean; delta = delta % delta; Number px = 1.0 / source.n_rows; MatrixOperator<Number>::_result = sum(delta*px, 0); return true; }
/*! * \brief forward * X: [N, C, 1, 1], usually the output of affine(fc) layer * Y: [N, C, 1, 1], ground truth, with 1(true) or 0(false) * \param[in] const vector<Blob*>& in in[0]:X, in[1]:Y * \param[out] double& loss loss * \param[out] Blob** out out: dX */ void SoftmaxLossLayer::go(const vector<shared_ptr<Blob>>& in, double& loss, shared_ptr<Blob>& dout, int mode) { //Blob X(*in[0]); //Blob Y(*in[1]); if (dout) { dout.reset(); } int N = in[0]->get_N(); int C = in[0]->get_C(); int H = in[0]->get_H(); int W = in[0]->get_W(); assert(H == 1 && W == 1); mat mat_x = in[0]->reshape(); mat mat_y = in[1]->reshape(); /*! forward */ mat row_max = repmat(arma::max(mat_x, 1), 1, C); mat_x = arma::exp(mat_x - row_max); mat row_sum = repmat(arma::sum(mat_x, 1), 1, C); mat e = mat_x / row_sum; //e.print("e:\n"); //mat rrs = arma::sum(e, 1); //rrs.print("rrs:\n"); mat prob = -arma::log(e); //prob.print("prob:\n"); //(prob%mat_y).print("gg:\n"); /*! loss should near -log(1/C) */ loss = accu(prob % mat_y) / N; /*! only forward */ if (mode == 1) return; /*! backward */ mat dx = e - mat_y; dx /= N; mat2Blob(dx, dout, (*in[0]).size()); return; }
void test_repmat() { // TODO(sanja): silly sanity check, write a better test double **A = new_matrix2(2, 3); int i, j; for (i = 0; i < 2; ++i) for (j = 0; j < 3; ++j) A[i][j] = (3 * i + (j+1)); double **B = new_matrix2(4, 9); repmat(B, A, 2, 3, 2, 3); print_matrix(A, 2, 3); print_matrix(B, 4, 9); }
int ccl_mat_distance(const double *A,const int a_i,const int a_j,const double *B,const int b_i,const int b_j,double * D){ gsl_matrix * D_ = gsl_matrix_alloc(a_j,b_j); memset(D,0,a_j*b_j*sizeof(double)); if (a_i!=b_i){ return 0; } gsl_matrix * A_ = gsl_matrix_alloc(a_i,a_j); gsl_matrix * B_ = gsl_matrix_alloc(b_i,b_j); gsl_matrix * A_o = gsl_matrix_alloc(a_i,a_j); gsl_matrix * B_o = gsl_matrix_alloc(b_i,b_j); memcpy(A_->data,A,a_i*a_j*sizeof(double)); memcpy(B_->data,B,b_i*b_j*sizeof(double)); memcpy(A_o->data,A,a_i*a_j*sizeof(double)); memcpy(B_o->data,B,b_i*b_j*sizeof(double)); gsl_matrix_mul_elements(A_,A_); gsl_matrix_mul_elements(B_,B_); double * a2 = malloc(a_j*sizeof(double)); double * b2 = malloc(b_j*sizeof(double)); //rep ccl_mat_sum(A_->data,a_i,a_j,1,a2); ccl_mat_sum(B_->data,b_i,b_j,1,b2); repmat(gsl_vector_view_array(a2,a_j).vector.data,a_j,1,1,b_j,D); repmat(gsl_vector_view_array(b2,b_j).vector.data,1,b_j,a_j,1,D_->data); ccl_mat_add(D,D_->data,a_j,b_j); gsl_blas_dgemm(CblasTrans,CblasNoTrans,1.0,A_o,B_o,0.0,D_); gsl_matrix_scale(D_,2); ccl_mat_sub(D,D_->data,a_j,b_j); gsl_matrix_free(A_); gsl_matrix_free(B_); gsl_matrix_free(A_o); gsl_matrix_free(B_o); gsl_matrix_free(D_); free(a2); free(b2); }
cv::Mat SIFT::sp_normalize_sift_arr( Mat sift_arr,float threshold ) { Mat siftlen=sp_normalize_sift_len(sift_arr); Mat normalize_ind1=compare(siftlen,threshold); Mat normalize_ind2=change(normalize_ind1); Mat sift_arr_hcontrast=search(sift_arr,normalize_ind1); sift_arr_hcontrast=sift_arr_hcontrast/repmat(search(siftlen,normalize_ind1),1,sift_arr.cols);//此处应为点除 Mat sift_arr_lcontrast=search(sift_arr,normalize_ind2); sift_arr_lcontrast=sift_arr_lcontrast/threshold; sift_arr_hcontrast= suppresss(sift_arr_hcontrast,0.2); sift_arr_lcontrast= suppresss(sift_arr_lcontrast,0.2); Mat tep(sift_arr_hcontrast.rows,1,CV_32F); sqrt(sum_row(square(sift_arr_hcontrast)),tep); sift_arr_hcontrast = sift_arr_hcontrast /repmat(tep, 1,sift_arr.cols); assign(sift_arr,normalize_ind1,sift_arr_hcontrast); assign(sift_arr,normalize_ind2,sift_arr_lcontrast); return sift_arr; }
/** Input: TrainData: 784*5000, 行数表示数据维度,列数表示数据个数 TrainLabels: 5000*1, values from 1 to N, don't start from 0! InputWeight: 随机生成的输入矩阵 Bias:随机生成的偏执值 C: 参数C Output: OutputWeight:计算得到的输出矩阵 HH:计算得到的特征矩阵HH=H'*H **/ void run_ELM(mat TrainData,mat TrainLabels, mat &H, mat &K, mat *InputWeight, mat *Bias, std::string *ActivationFunction, double C, int layers, int NumberClasses ){ printf("Now in POSELM function!\n"); int NumberTrainData = TrainData.n_cols; H = TrainData; int NumberHiddenUnits = TrainData.n_rows; for(int layer_id = 0; layer_id < layers; layer_id++){ NumberHiddenUnits = InputWeight[layer_id].n_rows; H = InputWeight[layer_id] * H + repmat(Bias[layer_id],1,NumberTrainData); H = ActivateFunction(ActivationFunction[layer_id],H); } K = H.t() * H; printf("Now exit from POSELM function!\n"); }
cv::Mat SIFT::repmat( Mat src,int height,int width ) { CvSize size=src.size(); if(height==1) { Mat x(src.rows,src.cols*width,CV_32F); for(int i=0;i<width;i++) { for(int j=0;j<size.height;j++) { float *pt=src.ptr<float>(j); for(int m=0;m<size.width;m++) { x.at<float>(j,i*size.width+m)=pt[m]; } } } return x; } else if(width==1) { Mat x(src.rows*height,src.cols,CV_32F); for(int i=0;i<height;i++) { for(int j=0;j<size.height;j++) { float *pt=src.ptr<float>(j); for(int k=0;k<size.width;k++) { x.at<float>(i*size.height+j,k)=pt[k]; } } } return x; }else { Mat x(src.rows * height, src.cols * height, CV_32F); for (int i = 0; i < src.rows; i++ ) { Mat repRow = repmat(x.row(i), 1, width); for (int j = 0; j < height; j++) { repRow.copyTo(x.row(j * height + i)); } } return x; } }
/*! * \brief forward * X: [N, C, Hx, Wx] * weight: [F, C, Hw, Ww] * bias: [F, 1, 1, 1] * out: [N, F, 1, 1] * \param[in] const vector<Blob*>& in in[0]:X, in[1]:weights, in[2]:bias * \param[in] const Param* param params * \param[out] Blob& out Y */ void AffineLayer::forward(const vector<shared_ptr<Blob>>& in, shared_ptr<Blob>& out) { if (out) { out.reset(); } int N = in[0]->get_N(); int F = in[1]->get_N(); mat x = in[0]->reshape(); mat w = in[1]->reshape(); mat b = in[2]->reshape(); b = repmat(b, 1, N).t(); mat ans = x * w.t() + b; mat2Blob(ans, out, F, 1, 1); return; }
/** * Generate random samples chosen from the multivariate Gaussian * distribution with mean MU and covariance SIGMA. * * X ~ N(u, Lambda) => Y = B * X + v ~ N(B * u + v, B * Lambda * B') * Therefore, if X ~ N(0, Lambda), * then Y = B * X + MU ~ N(MU, B * Lambda * B'). * We only need to do the eigen decomposition: SIGMA = B * Lambda * B'. * * @param MU 1 x d mean vector * * @param SIGMA covariance matrix * * @param cases number of d dimensional random samples * * @return cases-by-d sample matrix subject to the multivariate * Gaussian distribution N(MU, SIGMA) * */ Matrix& mvnrnd(Matrix& MU, Matrix& SIGMA, int cases) { int d = MU.getColumnDimension(); if (MU.getRowDimension() != 1) { errf("MU is expected to be 1 x %d matrix!\n", d); } if (norm(SIGMA.transpose().minus(SIGMA)) > 1e-10) errf("SIGMA should be a %d x %d real symmetric matrix!\n", d); Matrix** eigenDecompostion = eigs(SIGMA, d, "lm"); Matrix& B = *eigenDecompostion[0]; Matrix& Lambda = *eigenDecompostion[1]; /*disp(B); disp(Lambda);*/ Matrix& X = *new DenseMatrix(d, cases); std::default_random_engine generator(time(NULL)); std::normal_distribution<double> normal(0.0, 1.0); double sigma = 0; for (int i = 0; i < d; i++) { sigma = Lambda.getEntry(i, i); if (sigma == 0) { X.setRowMatrix(i, zeros(1, cases)); continue; } if (sigma < 0) { errf("Covariance matrix should be positive semi-definite!\n"); exit(1); } for (int n = 0; n < cases; n++) { X.setEntry(i, n, normal(generator) * pow(sigma, 0.5)); } } Matrix& Y = plus(mtimes(B, X), repmat(MU.transpose(), 1, cases)).transpose(); return Y; }
const matrix_type make_gaussian_electron( const matrix_type& s, const value_type v ) const { auto ans = s; auto const factor = v / value_type( 511 ) + value_type( 1 ); std::for_each( ans.begin(), ans.end(), [factor]( value_type & s_ ) { const value_type A[] = {0.3626, 0.9737, 2.7209, 1.7660}; const value_type B[] = {0.4281, 3.5770, 19.3905, 64.3334}; value_type tmp[4]; std::fill( tmp, tmp + 4, value_type() ); auto const ss = s_ * s_; std::transform( B, B + 4, tmp, [ss]( value_type b ) { return std::exp( -b * ss ); } ); s_ = factor * std::inner_product( A, A + 4, tmp, value_type() ); } ); return repmat( ans, 8, 1 ); }
void repmat(char *dest, const char *src, int ndim, int *destdimsize, int *dimsize, const int *dims, int *rep) { int d = ndim-1; int i, chunk; /* copy the first repetition into dest */ if(d == 0) { chunk = dimsize[0]; memcpy(dest,src,chunk); } else { /* recursively repeat each slice of src */ for(i=0;i<dims[d];i++) { repmat(dest + i*destdimsize[d-1], src + i*dimsize[d-1], ndim-1, destdimsize, dimsize, dims, rep); } chunk = destdimsize[d-1]*dims[d]; } /* copy the result rep-1 times */ memrep(dest,chunk,rep[d]); }
inline bool op_princomp::direct_princomp ( Mat< std::complex<typename T1::pod_type> >& coeff_out, const Base< std::complex<typename T1::pod_type>, T1 >& X, const typename arma_cx_only<typename T1::elem_type>::result* junk ) { arma_extra_debug_sigprint(); arma_ignore(junk); typedef typename T1::pod_type T; typedef std::complex<T> eT; const unwrap<T1> Y( X.get_ref() ); const Mat<eT>& in = Y.M; if(in.n_elem != 0) { // singular value decomposition Mat<eT> U; Col< T> s; const Mat<eT> tmp = in - repmat(mean(in), in.n_rows, 1); const bool svd_ok = svd(U,s,coeff_out, tmp); if(svd_ok == false) { return false; } } else { coeff_out.eye(in.n_cols, in.n_cols); } return true; }
void ccl_gaussian_rbf(const double * X,int i, int j,const double *C,int k, int d,double s,double * BX){ double* D = malloc(d*j*sizeof(double)); ccl_mat_distance(C,k,d,X,i,j,D); int cc; for (cc=0;cc<d*j;cc++){ BX[cc] = exp(-0.5/s*D[cc]); } //print_mat_d(BX,d,j); double * vec = malloc(j*sizeof(double)); ccl_mat_sum(BX,d,j,1,vec); for (cc=0;cc<j;cc++){ vec[cc] = 1/vec[cc]; } double * tmp = malloc(d*j*sizeof(double)); repmat(vec,1,j,d,1,tmp); gsl_matrix BX_ = gsl_matrix_view_array(BX,d,j).matrix; gsl_matrix tmp_ = gsl_matrix_view_array(tmp,d,j).matrix; gsl_matrix_mul_elements(&BX_,&tmp_); free(vec); free(D); free(tmp); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mxArray *srcmat; int ndim, *dimsize, eltsize; const int *dims; int ndimdest, *destdims, *destdimsize; char *src, *dest; int *rep; int i,nrep; int extra_rep = 1; int empty; if(nrhs < 2) mexErrMsgTxt("Usage: xrepmat(A, [N M ...])"); srcmat = prhs[0]; if(mxIsSparse(srcmat)) { mexErrMsgTxt("Sorry, can't handle sparse matrices yet."); } if(mxIsCell(srcmat)) { mexErrMsgTxt("Sorry, can't handle cell arrays yet."); } ndim = mxGetNumberOfDimensions(srcmat); dims = mxGetDimensions(srcmat); eltsize = mxGetElementSize(srcmat); /* compute dimension sizes */ dimsize = mxCalloc(ndim, sizeof(int)); dimsize[0] = eltsize*dims[0]; for(i=1;i<ndim;i++) dimsize[i] = dimsize[i-1]*dims[i]; /* determine repetition vector */ ndimdest = ndim; if(nrhs == 2) { nrep = mxGetN(prhs[1]); if(nrep > ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<nrep;i++) { double repv = mxGetPr(prhs[1])[i]; rep[i] = (int)repv; } if(nrep == 1) { /* special behavior */ nrep = 2; rep[1] = rep[0]; } } else { nrep = nrhs-1; if(nrep > ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<nrep;i++) { rep[i] = (int)*mxGetPr(prhs[i+1]); } } for(i=nrep;i<ndimdest;i++) rep[i] = 1; /* compute output size */ destdims = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<ndim;i++) destdims[i] = dims[i]*rep[i]; for(;i<ndimdest;i++) { destdims[i] = rep[i]; extra_rep *= rep[i]; } destdimsize = mxCalloc(ndim, sizeof(int)); destdimsize[0] = eltsize*destdims[0]; for(i=1;i<ndim;i++) destdimsize[i] = destdimsize[i-1]*destdims[i]; /* for speed, array should be uninitialized */ plhs[0] = mxCreateNumericArray(ndimdest, destdims, mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); /* if any rep[i] == 0, output should be empty array. Added by KPM 11/13/02. */ empty = 0; for (i=0; i < nrep; i++) { if (rep[i]==0) empty = 1; } if (empty) return; src = (char*)mxGetData(srcmat); dest = (char*)mxGetData(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); if(mxIsComplex(srcmat)) { src = (char*)mxGetPi(srcmat); dest = (char*)mxGetPi(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); } }
cv::Mat SIFT::sp_find_sift_grid( Mat I,Mat gridX,Mat gridY,int patchSize, float sigma_edge ) { int num_angles=8; float num_bins=4; int num_samples=num_bins*num_bins; int alpha = 9; //此处需要判断总共传入多少个变量,如果变量数小于5,就把sigma_edge设置为1 float angle_step=2*pi/num_angles; //初始化angles 为一个一维矩阵从0到2*pi;间隔为angle_step Mat angles=create(0,2*pi,angle_step); angles=deleteO(angles); //删除最后一个元素 CvSize size=I.size(); //int hgt=size.height; //int wid=size.width; int num_patches=gridX.total();//计算gridX总共有多少个元素 Mat sift_arr=Mat::zeros(num_patches,num_samples*num_angles,CV_32F); //计算滤波算子 int f_wid = 4 * ceil(sigma_edge) + 1; Mat G=gaussian(f_wid,sigma_edge); Mat GX=gradientX(G); Mat GY=gradientY(G); GX=GX*2/totalSumO(GX); GY=GY*2/totalSumO(GY); Mat I_X(I.rows,I.cols,CV_32F); I_X=filter2(GX,I); //因为I,图片读入不同,所以I_X不同,与I有关的均布相同,但是,都正确 Mat I_Y(I.rows,I.cols,CV_32F); I_Y=filter2(GY,I); Mat T(I_X.rows,I_X.cols,CV_32F); add(I_X.mul(I_X),I_Y.mul(I_Y),T); Mat I_mag(I_X.rows,I_X.cols,CV_32F); sqrt(T,I_mag); Mat I_theta=matan2(I_Y,I_X); Mat interval=create(2/num_bins,2,2/num_bins); interval-=(1/num_bins+1); Mat sample_x=meshgrid_X(interval,interval); Mat sample_y=meshgrid_Y(interval,interval); sample_x=reshapeX(sample_x);//变为一个1维矩阵 sample_y=reshapeX(sample_y); Mat I_orientation[8] = {Mat::zeros(size,CV_32F)}; for(int i=0;i<8;i++) { I_orientation[i] = Mat::zeros(size,CV_32F); } float *pt=angles.ptr<float>(0); for(int a=0;a<num_angles;a++) { Mat tep1=mcos(I_theta-pt[a]);//cos //cout<<tep1.at<float>(0,1)<<endl; Mat tep(tep1.rows,tep1.cols,CV_32F); pow(tep1,alpha,tep); tep=compareB(tep,0); I_orientation[a]=tep.mul(I_mag); } for(int i=0;i<num_patches;i++) { double r=patchSize/2; float l=(float)(i/gridX.rows); float m=i%gridX.rows; float cx=gridX.at<float>(m,l)+r-0.5; float cy=gridY.at<float>(m,l)+r-0.5; Mat sample_x_t=Add(sample_x*r,cx); Mat sample_y_t=Add(sample_y*r,cy); float *pt1=sample_y_t.ptr<float>(0); float sample_res=pt1[1]-pt1[0]; // int c=(int)i/gridX.rows; // float *ptc1=gridX.ptr<float>(c); // int x_lo=ptc1[i%gridX.rows]; int x_lo = gridX.at<float>(i % gridX.rows, i / gridX.rows); int x_hi=patchSize+x_lo-1; /* float *ptc2=gridY.ptr<float>(c);*/ int y_lo=gridY.at<float>(i % gridY.rows, i / gridY.rows); int y_hi=y_lo+patchSize-1; Mat A=create(x_lo,x_hi,1); Mat B=create(y_lo,y_hi,1); Mat sample_px=meshgrid_X(A,B); Mat sample_py=meshgrid_Y(A,B); int num_pix = sample_px.total();//计算sample_px元素总数 sample_px=reshapeY(sample_px); sample_py=reshapeY(sample_py); Mat dist_px=abs(repmat(sample_px,1,num_samples)-repmat(sample_x_t,num_pix,1)); Mat dist_py=abs(repmat(sample_py,1,num_samples)-repmat(sample_y_t,num_pix,1)); Mat weights_x=dist_px/sample_res; Mat weights_x_l=Less(weights_x,1); weights_x=(1-weights_x).mul(weights_x_l); Mat weights_y=dist_py/sample_res; Mat weights_y_l=Less(weights_y,1); weights_y=(1-weights_y).mul(weights_y_l); Mat weights=weights_x.mul(weights_y); Mat curr_sift=Mat::zeros(num_angles,num_samples,CV_32F); for(int a=0;a<num_angles;a++) { //Mat I=getNum(I_orientation[a],y_lo,y_hi,x_lo,x_hi); Mat I = I_orientation[a](Range(y_lo, y_hi), Range(x_lo, x_hi)); Mat tep=reshapeY(I); // Fill tep with zeros to fit size of weight if (tep.cols < weights.cols) { for (int i = tep.rows; i < weights.rows; i++) tep.push_back(0.0f); } tep=repmat(tep,1,num_samples); Mat t=tep.mul(weights); Mat ta=sum_every_col(t); float *p=ta.ptr<float>(0); for(int i=0;i<curr_sift.cols;i++) { curr_sift.at<float>(a,i)=p[i]; } } Mat tp=reshapeX(curr_sift); float *p=tp.ptr<float>(0); for(int j=0;j<sift_arr.cols;j++) { sift_arr.at<float>(i,j)=p[j]; } } return sift_arr; }
//NNBP performs backpropagation void FBNN::nnbp(void) { // std::cout << "start nnbp" << std::endl; std::vector<FMatrix_ptr> oDs; //initialize oDs for(int i = 0; i < m_iN; i++) oDs.push_back(std::make_shared<FMatrix>(FMatrix())); if(m_strOutput == "sigm") { *oDs[m_iN -1] = bitWiseMul(*m_oEp,bitWiseMul(*m_oAs[m_iN -1],*m_oAs[m_iN -1] - 1)); } else if(m_strOutput == "softmax" || m_strOutput == "linear") { *oDs[m_iN -1] = - (*m_oEp); } for(int i = m_iN - 2; i > 0; i--) { FMatrix d_act; if(m_strActivationFunction == "sigm") { d_act = bitWiseMul(*m_oAs[i],1 - (*m_oAs[i])); } else if(m_strActivationFunction == "tanh_opt") { d_act = 1.7159 * 2/3 - 2/(3 * 1.7159) * bitWiseSquare(*m_oAs[i]); } if(m_fNonSparsityPenalty > 0) { FMatrix pi = repmat(*m_oPs[i],m_oAs[i]->rows(),1); FMatrix sparsityError = addPreColumn(m_fNonSparsityPenalty * (1 - m_fSparsityTarget) / (1 - pi) - m_fNonSparsityPenalty * m_fSparsityTarget / pi,0); //Backpropagate first derivatives if(i == m_iN - 2)//in this case in oDs there is not the bias term to be removed { *oDs[i] = bitWiseMul(*oDs[i+1] * (*m_oWs[i]) + sparsityError,d_act);//Bishop (5.56) } else//in this case in oDs the bias term has to be removed { *oDs[i] = bitWiseMul(delPreColumn(*oDs[i+1]) * (*m_oWs[i]) + sparsityError,d_act); } } else { //Backpropagate first derivatives if(i == m_iN - 2)//in this case in oDs there is not the bias term to be removed { *oDs[i] = bitWiseMul((*oDs[i+1]) * (*m_oWs[i]),d_act);//Bishop (5.56) } else//in this case in oDs the bias term has to be removed { *oDs[i] = bitWiseMul(delPreColumn(*oDs[i+1]) * (*m_oWs[i]),d_act); } } if(m_fDropoutFraction > 0) { *oDs[i] = bitWiseMul(*oDs[i],addPreColumn(*m_odOMs[i],1)); } } if(m_odWs.empty())//Initialize m_odWs { for(int i = 0; i < m_iN - 1; i++) { m_odWs.push_back(std::make_shared<FMatrix>(FMatrix())); } } for(int i = 0; i < m_iN - 1; i++) { if(i == m_iN - 2) { *m_odWs[i] = trans(*oDs[i+1]) * (*m_oAs[i]) / oDs[i+1]->rows(); } else { *m_odWs[i] = trans(delPreColumn(*oDs[i+1])) * (*m_oAs[i]) / oDs[i+1]->rows(); } } // std::cout << "end nnbp" << std::endl; }
void HMM::_fwdback(mat init_state_distrib, mat _transmat, mat obslik, mat &alpha, mat &beta, mat& gamma, double &loglik, mat &xi_summed, cube &gamma2, cube &obslik2, bool fwd_only, bool compute_gamma2) { /* * Compute the posterior probs. in an HMM using the forwards backwards algo. * * Notation: * Y(t) = observation, Q(t) = hidden state, M(t) = mixture variable (for MOG outputs) * A(t) = discrete input (action) (for POMDP models) * * INPUT: * init_state_distrib(i) = Pr(Q(1) = i) * transmat(i,j) = Pr(Q(t) = j | Q(t-1)=i) * or transmat{a}(i,j) = Pr(Q(t) = j | Q(t-1)=i, A(t-1)=a) if there are discrete inputs * obslik(i,t) = Pr(Y(t)| Q(t)=i) * */ bool scaled = true; bool maximize = false; bool compute_xi = true; int Q = obslik.n_rows; int T = obslik.n_cols; mat mixmat; mat act; // qui act è tutti zero, altrimenti potrebbe essere un input, TODO aggiungere &act negli input mat scale; if (obslik2.is_empty()) compute_gamma2 = false; act = zeros(1,T); // TODO this could be a colvec scale = ones(1,T); field<mat> transmat(1,1); transmat(0,0) = _transmat; // scale(t) = Pr(O(t) | O(1:t-1)) = gamma21/c(t) as defined by Rabiner (1989). // Hence prod_t scale(t) = Pr(O(1)) Pr(O(2)|O(1)) Pr(O(3) | O(1:2)) ... = Pr(O(1), ... ,O(T)) // or log P = sum_t log scale(t). // Rabiner suggests multiplying beta(t) by scale(t), but we can instead // normalise beta(t) - the constants will cancel when we compute gamma. if (compute_xi) xi_summed = zeros(Q,Q); //else // xi_summed = []; //%%%%%%%%% Forwards %%%%%%%%%% //cout << "fwdback > Forwards" << endl; int t = 0; alpha.col(0) = vectorize(init_state_distrib) % obslik.col(t); if (scaled){ std::pair<mat,double> _tmp = normaliseC(alpha.col(t)); alpha.col(t) = _tmp.first; scale(t) = _tmp.second; } for(int t=1; t<T; t++) { mat trans; mat m; trans = transmat(act(t-1)); if (maximize){ //m = max_mult(trans.t(), alpha.col(t-1)); // TODO max_mult } else { m = trans.t() * alpha.col(t-1); } alpha.col(t) = vectorize(m) % obslik.col(t); if (scaled) { std::pair<mat,double> _tmp = normaliseC(alpha.col(t)); alpha.col(t) = _tmp.first; scale(t) = _tmp.second; } if (compute_xi && fwd_only) {// useful for online EM xi_summed = xi_summed + normalise((alpha.col(t-1) * obslik.col(t).t()) % trans); } } if (scaled) { uvec _s = find(scale); // se c'è almeno uno zero // portando a logaritmo c'è almeno un infinito // quindi somma tutto a infinito if ( _s.is_empty() ) { loglik = -std::numeric_limits<double>::max(); } else { loglik = sum(sum(log(scale))); // nested arma::sum because sum(mat X) return a rowvec } } else { loglik = log(sum(alpha.col(T))); } if (fwd_only) { gamma = alpha; return; } //%%%%%%%%% Backwards %%%%%%%%%% //cout << "fwdback > Backwards" << endl; int M; mat trans; mat denom; beta = zeros(Q,T); if (compute_gamma2) { M = mixmat.n_cols; gamma2 = zeros(Q,M,T); } else { //gamma2 = [] } beta.col(T-1) = ones(Q,1); gamma.col(T-1) = normalise(alpha.col(T-1) % beta.col(T-1)); t=T-1; if (compute_gamma2) { denom = obslik.col(t) + (obslik.col(t)==0); // replace 0s with 1s before dividing gamma2.slice(t) = obslik2.slice(t) % mixmat % repmat(gamma.col(t), 1, M) % repmat(denom, 1, M); } for (int t=T-2; t>=0; t--) { // T-2 because there are some calls to t+1 // and col(T) will generate the error Mat::col(): out of bounds // so we must assure the limit of col(T-1) mat b = beta.col(t+1) % obslik.col(t+1); trans = transmat(act(t)); if (maximize){ mat B = repmat(vectorize(b).t(), Q, 1); beta.col(t) = max(trans % B, 1); } else beta.col(t) = trans * b; if (scaled) beta.col(t) = normalise( beta.col(t) ); gamma.col(t) = normalise(alpha.col(t) % beta.col(t)); if (compute_xi){ xi_summed = xi_summed + normalise((trans % (alpha.col(t) * b.t()))); } if (compute_gamma2){ denom = obslik.col(t) + (obslik(t)==0); // replace 0s with 1s before dividing gamma2.slice(t) = obslik2.slice(t) % mixmat % repmat(gamma.col(t), 1, M) % repmat(denom, 1, M); } } }
arma::mat OneSamplePermTestingCPU(arma::mat data, int nPermutations, double maxMemory) { std::string permMatrixPath = "/Users/felipegb94/PermTest/data/face/PermutationMatrix.arma"; int N; // Total number of subjects int V; // Total number of statistics/voxels to be tested int nPermutationsPerIteration; // Number of permutations done at once int numIterations; // nPermutations/nPermutationsPerIteration int lastIteration; int start, end; // Start and end of current interval arma::mat permutationMatrix; arma::mat maxT; // Maximum null distribution arma::mat dataSquared; arma::mat gMean; arma::mat gVar; arma::mat tStatMatrix; /* Set constant values and allocate memory */ N = data.n_rows; V = data.n_cols; //permutationMatrix.load(permMatrixPath); permutationMatrix = OneSampleGetPermutationMatrix(nPermutations, N); dataSquared = data % data; nPermutationsPerIteration = GetIntervalDimension(V, maxMemory); lastIteration = nPermutations % nPermutationsPerIteration; numIterations = floor(nPermutations/nPermutationsPerIteration); maxT = arma::zeros(nPermutations,1); std::cout << "Number of subjects (rows in data matrix): " << N << std::endl; std::cout << "Number of voxels per subject (cols in data matrix and cols in indexMatrix): "; std::cout << V << std::endl; std::cout << "Number of Permutations (rows in permutations matrix):" << nPermutations << std::endl; std::cout << "Rows in permutationMatrix = " << permutationMatrix.n_rows << std::endl; std::cout << "Cols in permutationMatrix = " << permutationMatrix.n_cols << std::endl; std::cout << "Interval Size = " << nPermutationsPerIteration << std::endl; std::cout << "Number of Passes = " << numIterations << std::endl; std::cout << "Last Iteration = " << lastIteration << std::endl; int i = 0; arma::mat varTerm1 = repmat(arma::sum(dataSquared,0), nPermutationsPerIteration,1)/N; arma::mat varTerm1LastItr = repmat(arma::sum(dataSquared,0), lastIteration, 1)/N; std::cout << "VarTerm rows " << varTerm1LastItr.n_rows << std::endl; std::cout << "VarTerm cols " << varTerm1LastItr.n_cols << std::endl; /* Permutation loop */ #if OPENMP_ENABLED #pragma omp parallel for #endif for(i = 0;i < numIterations;i++) { start = nPermutationsPerIteration * i; end = (nPermutationsPerIteration * i) + nPermutationsPerIteration - 1; printf("Iteration %d , start %d, end %d of %d \n", i, start, end, nPermutations-1); gMean = (permutationMatrix(arma::span(start,end), arma::span::all) * data) / N; gVar = (varTerm1) - (gMean % gMean); tStatMatrix = (gMean) / sqrt(gVar/(N-1)); maxT(arma::span(start,end),arma::span::all) = arma::max(tStatMatrix,1); } if(lastIteration != 0) { start = nPermutationsPerIteration * i; end = nPermutations - 1; printf("Iteration %d , start %d, end %d of %d \n", i, start, end, nPermutations-1); gMean = (permutationMatrix(arma::span(start,end), arma::span::all) * data) / N; gVar = varTerm1LastItr - (gMean % gMean); tStatMatrix = (gMean) / sqrt(gVar/(N-1)); maxT(arma::span(start,end),arma::span::all) = arma::max(tStatMatrix,1); } std::string prefix = "OneSampleMaxT_CPU"; SaveMaxT(maxT, nPermutations, prefix); return maxT; }