Exemple #1
0
double State::transition_row_partition_assignments(const MatrixD& data,
        vector<int> which_rows) {
    vector<int> global_column_indices = create_sequence(data.size2());
    double score_delta = 0;
    //
    int num_rows = which_rows.size();
    if (num_rows == 0) {
        num_rows = data.size1();
        which_rows = create_sequence(num_rows);
        //FIXME: use own shuffle so seed control is in effect
        std::random_shuffle(which_rows.begin(), which_rows.end());
    }
    set<View*>::iterator svp_it;
    for (svp_it = views.begin(); svp_it != views.end(); svp_it++) {
        // for each view
        View& v = **svp_it;
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                                v.global_to_local);
        const MatrixD data_subset = extract_columns(data, view_cols);
        map<int, vector<double> > row_data_map = construct_data_map(data_subset);
        vector<int>::iterator vi_it;
        for (vi_it = which_rows.begin(); vi_it != which_rows.end(); vi_it++) {
            // for each SPECIFIED row
            int row_idx = *vi_it;
            vector<double> vd = row_data_map[row_idx];
            score_delta += v.transition_z(vd, row_idx);
        }
    }
    data_score += score_delta;
    return score_delta;
}
Exemple #2
0
BenchResult doBenchANNPriority(const MatrixD& d, const MatrixD& q, const int K, const int itCount, const int searchCount)
{
	BenchResult result;
	boost::timer t;
	const int ptCount(d.cols());
	const double **pa = new const double *[d.cols()];
	for (int i = 0; i < ptCount; ++i)
		pa[i] = &d.coeff(0, i);
	ANNkd_tree* ann_kdt = new ANNkd_tree(const_cast<double**>(pa), ptCount, d.rows(), 8);
	result.creationDuration = t.elapsed();
	
	for (int s = 0; s < searchCount; ++s)
	{
		t.restart();
		ANNidx nnIdx[K];
		ANNdist dists[K];
		for (int i = 0; i < itCount; ++i)
		{
			const VectorD& tq(q.col(i));
			ANNpoint queryPt(const_cast<double*>(&tq.coeff(0)));
			ann_kdt->annkPriSearch(		// search
							queryPt,	// query point
							K,			// number of near neighbours
							nnIdx,		// nearest neighbours (returned)
							dists,		// distance (returned)
							0);			// error bound
		}
		result.executionDuration += t.elapsed();
	}
	result.executionDuration /= double(searchCount);
	
	return result;
}
bool pinv_QR(const MatrixD &A, MatrixD *invA, Scalar eps) {
  MatrixD At = A.transpose();
  HouseholderQR < MatrixD > qr = At.householderQr();
  int m = A.rows();
  //int n = A.cols();

  MatrixD Rt = MatrixD::Zero(m, m);
  bool invertible;

  MatrixD hR = (MatrixD) qr.matrixQR();
  MatrixD Y = ((MatrixD) qr.householderQ()).leftCols(m);

  //take the useful part of R
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= i; j++)
      Rt(i, j) = hR(j, i);
  }
  FullPivLU < MatrixD > invRt(Rt);

  invertible = fabs(invRt.determinant()) > eps;

  if (invertible) {
    *invA = Y * invRt.inverse();
    return true;
  } else {
    return false;
  }

}
void ConvolutionalNeuralNetwork::FP(int index){
    MatrixD Z = Inputs.at(index).reshape(1,1).t();
    for(int i = 0; i < network.size(); i++){
        (network.at(i)->ForwardPropargation(Z)).copyTo(Z);
    }
    Z.copyTo(ZL);
}
bool pinv_QR_Z(const MatrixD &A, const MatrixD &Z0, MatrixD *invA, MatrixD *Z, Scalar lambda_max, Scalar eps) {
  VectorD sigma;  //vector of singular values
  Scalar lambda2;

  MatrixD AZ0t = (A * Z0).transpose();
  HouseholderQR < MatrixD > qr = AZ0t.householderQr();

  int m = A.rows();
  int p = Z0.cols();

  MatrixD Rt = MatrixD::Zero(m, m);
  bool invertible;
  MatrixD hR = (MatrixD) qr.matrixQR();
  MatrixD Y = ((MatrixD) qr.householderQ()).leftCols(m);

  //take the useful part of R
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= i; j++)
      Rt(i, j) = hR(j, i);
  }

  FullPivLU < MatrixD > invRt(Rt);
  invertible = fabs(invRt.determinant()) > eps;

  if (invertible) {
    *invA = Z0 * Y * invRt.inverse();
    *Z = Z0 * (((MatrixD) qr.householderQ()).rightCols(p - m));
    return true;
  } else {
    MatrixD R = MatrixD::Zero(m, m);
    //take the useful part of R
    for (int i = 0; i < m; i++) {
      for (int j = i; j < m; j++)  // TODO: is starting at i correct?
        R(i, j) = hR(i, j);
    }

    //perform the SVD of R
    JacobiSVD<MatrixD> svd_R(R, ComputeThinU | ComputeThinV);
    sigma = svd_R.singularValues();
    lambda2 = (1 - (sigma(m - 1) / eps) * (sigma(m - 1) / eps)) * lambda_max * lambda_max;
    for (int i = 0; i < m; i++) {
      sigma(i) = sigma(i) / (sigma(i) * sigma(i) + lambda2);
    }
    (*invA) = Z0 * Y * svd_R.matrixU() * sigma.asDiagonal() * svd_R.matrixV().transpose();

    *Z = Z0 * (((MatrixD) qr.householderQ()).rightCols(p - m));
    return false;
  }

}
bool pinv_forBarP(const MatrixD &W, const MatrixD &P, MatrixD *inv) {

  MatrixD barW;
  int rowsBarW = 0;

  MatrixD tmp;
  bool invertible;

  for (int i = 0; i < W.rows(); i++) {
    if (W(i, i) > 0.99) {  //equal to 1 (safer)
      rowsBarW++;
      barW = (MatrixD(rowsBarW, W.cols()) << barW, W.row(i)).finished();
    }
  }

  tmp = barW * P * barW.transpose();
  FullPivLU < MatrixD > inversePbar(tmp);

  invertible = inversePbar.isInvertible();

  if (invertible) {
    (*inv) = P * barW.transpose() * inversePbar.inverse() * barW;
    return true;
  } else {
    (*inv) = MatrixD::Zero(W.rows(), W.rows());
    return false;
  }
}
bool OSNSVelocityIK::isOptimal(int priority, const VectorD& dotQ,
                               const MatrixD& tildeP, MatrixD* W,
                               VectorD* dotQn, double eps) {

  VectorD barMu;
  bool isOptimal = true;

  barMu = tildeP.transpose() * dotQ;

  for (int i = 0; i < n_dof; i++) {
    if ((*W)(i, i) < 0.1) {  //equal to 0.0 (safer)

      if (abs(dotQ(i) - dotQmax(i)) < eps)
        barMu(i) = -barMu(i);

      if (barMu(i) < 0.0) {
        (*W)(i, i) = 1.0;
        (*dotQn)(i) = 0.0;
        isOptimal = false;
      }
    }

  }
  return isOptimal;
}
MatrixD ConvolutionalNeuralNetwork::FP(MatrixD X){
    MatrixD Z;
    X.copyTo(Z);
    for(int i = 0; i < network.size(); i++){
        network.at(i)->ForwardPropargation(Z).copyTo(Z);
    }
    return Z;
}
Exemple #9
0
map<int, vector<double> > construct_data_map(const MatrixD data) {
  unsigned int num_rows = data.size1();
  map<int, vector<double> > data_map;
  for(unsigned int row_idx=0; row_idx<num_rows; row_idx++) {
    data_map[row_idx] = extract_row(data, row_idx);
  }
  return data_map;
}
bool pinv_damped(const MatrixD &A, MatrixD *invA, Scalar lambda_max, Scalar eps) {

  //A (m x n) usually comes from a redundant task jacobian, therfore we consider m<n
  int m = A.rows() - 1;
  VectorD sigma;  //vector of singular values
  Scalar lambda2;
  int r = 0;

  JacobiSVD<MatrixD> svd_A(A.transpose(), ComputeThinU | ComputeThinV);
  sigma = svd_A.singularValues();
  if (((m > 0) && (sigma(m) > eps)) || ((m == 0) && (A.array().abs() > eps).any())) {
    for (int i = 0; i <= m; i++) {
      sigma(i) = 1.0 / sigma(i);
    }
    (*invA) = svd_A.matrixU() * sigma.asDiagonal() * svd_A.matrixV().transpose();
    return true;
  } else {
    lambda2 = (1 - (sigma(m) / eps) * (sigma(m) / eps)) * lambda_max * lambda_max;
    for (int i = 0; i <= m; i++) {
      if (sigma(i) > EPSQ)
        r++;
      sigma(i) = (sigma(i) / (sigma(i) * sigma(i) + lambda2));
    }
    //only U till the rank
    MatrixD subU = svd_A.matrixU().block(0, 0, A.cols(), r);
    MatrixD subV = svd_A.matrixV().block(0, 0, A.rows(), r);

    (*invA) = subU * sigma.asDiagonal() * subV.transpose();
    return false;
  }

}
Exemple #11
0
// helper for cython
double State::transition_view_i(int which_view, const MatrixD& data) {
    vector<int> global_column_indices = create_sequence(data.size2());
    View& v = get_view(which_view);
    vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                            v.global_to_local);
    const MatrixD data_subset = extract_columns(data, view_cols);
    map<int, vector<double> > data_subset_map = construct_data_map(data_subset);
    return v.transition(data_subset_map);
}
bool pinv(const MatrixD &A, MatrixD *invA, Scalar eps) {

  //A (m x n) usually comes from a redundant task jacobian, therfore we consider m<n
  int m = A.rows() - 1;
  VectorD sigma;  //vector of singular values

  JacobiSVD<MatrixD> svd_A(A.transpose(), ComputeThinU | ComputeThinV);
  sigma = svd_A.singularValues();
  if (((m > 0) && (sigma(m) > eps)) || ((m == 0) && (A.array().abs() > eps).any())) {
    for (int i = 0; i <= m; i++) {
      sigma(i) = 1.0 / sigma(i);
    }
    (*invA) = svd_A.matrixU() * sigma.asDiagonal() * svd_A.matrixV().transpose();
    return true;
  } else {
    return false;
  }
}
Exemple #13
0
void State::construct_base_hyper_grids(const MatrixD& data, int N_GRID,
        vector<double> ROW_CRP_ALPHA_GRID,
        vector<double> COLUMN_CRP_ALPHA_GRID) {
    int num_rows = data.size1();
    int num_cols = data.size2();
    if (ROW_CRP_ALPHA_GRID.size() == 0) {
        ROW_CRP_ALPHA_GRID = create_crp_alpha_grid(num_rows, N_GRID);
    }
    if (COLUMN_CRP_ALPHA_GRID.size() == 0) {
        COLUMN_CRP_ALPHA_GRID = create_crp_alpha_grid(num_cols, N_GRID);
    }
    row_crp_alpha_grid = ROW_CRP_ALPHA_GRID;
    column_crp_alpha_grid = COLUMN_CRP_ALPHA_GRID;
    construct_cyclic_base_hyper_grids(N_GRID, num_rows, vm_b_grid);
    construct_continuous_base_hyper_grids(N_GRID, num_rows, r_grid, nu_grid);
    construct_multinomial_base_hyper_grids(N_GRID, num_rows,
                                           multinomial_alpha_grid);
}
Exemple #14
0
MatrixD extract_columns(const MatrixD fromM, vector<int> from_cols) {
  int num_rows = fromM.size1();
  int num_cols = from_cols.size();
  MatrixD toM(num_rows, num_cols);
  for(int to_col=0; to_col<num_cols; to_col++) {
    int from_col = from_cols[to_col];
    copy_column(fromM, from_col, toM, to_col);
  }
  return toM;
}
bool isIdentity(const MatrixD &A) {

  bool isIdentity = true;
  int n = A.rows();
  int i = 0;
  do {
    isIdentity &= (A(i, i) > 0.99);  // equal to 1.0 (safer)
    i++;
  } while (isIdentity && i < n);

  return isIdentity;
}
Exemple #16
0
double State::transition_views(const MatrixD& data) {
    vector<int> global_column_indices = create_sequence(data.size2());
    //
    double score_delta = 0;
    // ordering doesn't matter, don't need to shuffle
    for (int view_idx = 0; view_idx < get_num_views(); view_idx++) {
        View& v = get_view(view_idx);
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                                v.global_to_local);
        const MatrixD data_subset = extract_columns(data, view_cols);
        map<int, vector<double> > data_subset_map = construct_data_map(data_subset);
        score_delta += v.transition(data_subset_map);
    }
    return score_delta;
}
Exemple #17
0
double State::transition_features(const MatrixD &data, vector<int> which_features) {
    double score_delta = 0;
    int num_features = which_features.size();
    if(num_features==0) {
        which_features = create_sequence(data.size2());
        // FIXME: use seed to shuffle
        std::random_shuffle(which_features.begin(), which_features.end());
    }
    vector<int>::iterator it;
    for(it=which_features.begin(); it!=which_features.end(); it++) {
        int feature_idx = *it;
        vector<double> feature_data = extract_col(data, feature_idx);
        // kernel selection
        if(ct_kernel == 0) {
            score_delta += transition_feature_gibbs(feature_idx, feature_data);
        } else if(ct_kernel == 1) {
            score_delta += transition_feature_mh(feature_idx, feature_data);
        } else {
            printf("Invalid CT_KERNEL");
            assert(0==1);
        }
    }
    return score_delta;
}
Exemple #18
0
void copy_column(const MatrixD fromM, int from_col, MatrixD &toM, int to_col) {
  assert(fromM.size1()==toM.size1());
  int num_rows = fromM.size1();
  project(toM, boost::numeric::ublas::range(0, num_rows), boost::numeric::ublas::range(to_col, to_col+1)) = \
    project(fromM, boost::numeric::ublas::range(0, num_rows), boost::numeric::ublas::range(from_col, from_col+1));
}