本文整理汇总了C++中MatType类的典型用法代码示例。如果您正苦于以下问题:C++ MatType类的具体用法?C++ MatType怎么用?C++ MatType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MatType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
const BallBound<VecType>&
BallBound<VecType>::operator|=(const MatType& data)
{
if (radius < 0)
{
center = data.col(0);
radius = 0;
}
// Now iteratively add points. There is probably a closed-form solution to
// find the minimum bounding circle, and it is probably faster.
for (size_t i = 1; i < data.n_cols; ++i)
{
double dist = metric::EuclideanDistance::Evaluate(center, (VecType)
data.col(i)) - radius;
if (dist > 0)
{
// Move (dist / 2) towards the new point and increase radius by
// (dist / 2).
arma::vec diff = data.col(i) - center;
center += 0.5 * diff;
radius += 0.5 * dist;
}
}
return *this;
}
示例2: Initialize
inline static void Initialize(const MatType& V,
const size_t r,
arma::mat& W,
arma::mat& H)
{
size_t n = V.n_rows;
size_t m = V.n_cols;
double V_avg = 0;
size_t count = 0;
double min = DBL_MAX;
for(typename MatType::const_row_col_iterator it = V.begin();it != V.end();it++)
{
if(*it != 0)
{
count++;
V_avg += *it;
if(*it < min) min = *it;
}
}
V_avg = sqrt(((V_avg / (n * m)) - min) / r);
// Intialize to random values.
W.randu(n, r);
H.randu(r, m);
W = W + V_avg;
H = H + V_avg;
}
示例3:
void ClassificationTree::print_train_log(const TreeNode::PtrSplitNodeBase split, const TrainingSet &train_set) const
{
MatType ltype = train_set.get_label_type();
MatType ftype = train_set.get_feature_type();
int rows = (int)ltype.total();
cv::Mat_<double> left_tmp;
cv::Mat_<double> right_tmp;
TrainingSet left_set(ftype, ltype);
TrainingSet right_set(ftype, ltype);
split->operator()(train_set, left_set, right_set);
left_set.compute_target_mean(left_tmp);
right_set.compute_target_mean(right_tmp);
cv::Mat_<double> left_dist(rows, 1, (double*)left_tmp.data);
cv::Mat_<double> right_dist(rows, 1, (double*)right_tmp.data);
printf("left dist\n");
for (unsigned ii = 0; ii < left_dist.total(); ++ii) {
printf("\tlabel%d:%f\n", ii, left_dist.at<double>(ii) / left_set.size());
}
printf("right dist\n");
for (unsigned ii = 0; ii < right_dist.total(); ++ii) {
printf("\tlabel%d:%f\n", ii, right_dist.at<double>(ii) / right_set.size());
}
}
示例4: DimsString
inline std::string
DimsString( const MatType& A, std::string label="Matrix" )
{
std::ostringstream os;
os << label << " ~ " << A.Height() << " x " << A.Width();
return os.str();
}
示例5: Initialize
inline static void Initialize(const MatType& V,
const size_t r,
arma::mat& W,
arma::mat& H)
{
const size_t n = V.n_rows;
const size_t m = V.n_cols;
double avgV = 0;
size_t count = 0;
double min = DBL_MAX;
// Iterate over all elements in the matrix (for sparse matrices, this only
// iterates over nonzeros).
for (typename MatType::const_row_col_iterator it = V.begin();
it != V.end(); ++it)
{
++count;
avgV += *it;
// Track the minimum value.
if (*it < min)
min = *it;
}
avgV = sqrt(((avgV / (n * m)) - min) / r);
// Initialize to random values.
W.randu(n, r);
H.randu(r, m);
W = W + avgV;
H = H + avgV;
}
示例6:
const BallBound<MetricType, VecType>&
BallBound<MetricType, VecType>::operator|=(const MatType& data)
{
if (radius < 0)
{
center = data.col(0);
radius = 0;
}
// Now iteratively add points.
for (size_t i = 0; i < data.n_cols; ++i)
{
const ElemType dist = metric->Evaluate(center, (VecType) data.col(i));
// See if the new point lies outside the bound.
if (dist > radius)
{
// Move towards the new point and increase the radius just enough to
// accommodate the new point.
const VecType diff = data.col(i) - center;
center += ((dist - radius) / (2 * dist)) * diff;
radius = 0.5 * (dist + radius);
}
}
return *this;
}
示例7: SubMatrix
SubMatrix(MatType& m)
: matrix(m),
begin_row(0),
end_row(m.numRows()),
begin_column(0),
end_column(m.numColumns())
{
}
示例8: EmptyCluster
size_t MaxVarianceNewCluster::EmptyCluster(const MatType& data,
const size_t emptyCluster,
const arma::mat& oldCentroids,
arma::mat& newCentroids,
arma::Col<size_t>& clusterCounts,
MetricType& metric,
const size_t iteration)
{
// If necessary, calculate the variances and assignments.
if (iteration != this->iteration || assignments.n_elem != data.n_cols)
Precalculate(data, oldCentroids, clusterCounts, metric);
this->iteration = iteration;
// Now find the cluster with maximum variance.
arma::uword maxVarCluster;
variances.max(maxVarCluster);
// Now, inside this cluster, find the point which is furthest away.
size_t furthestPoint = data.n_cols;
double maxDistance = -DBL_MAX;
for (size_t i = 0; i < data.n_cols; ++i)
{
if (assignments[i] == maxVarCluster)
{
const double distance = std::pow(metric.Evaluate(data.col(i),
newCentroids.col(maxVarCluster)), 2.0);
if (distance > maxDistance)
{
maxDistance = distance;
furthestPoint = i;
}
}
}
// Take that point and add it to the empty cluster.
newCentroids.col(maxVarCluster) *= (double(clusterCounts[maxVarCluster]) /
double(clusterCounts[maxVarCluster] - 1));
newCentroids.col(maxVarCluster) -= (1.0 / (clusterCounts[maxVarCluster] - 1.0)) *
arma::vec(data.col(furthestPoint));
clusterCounts[maxVarCluster]--;
clusterCounts[emptyCluster]++;
newCentroids.col(emptyCluster) = arma::vec(data.col(furthestPoint));
assignments[furthestPoint] = emptyCluster;
// Modify the variances, as necessary.
variances[emptyCluster] = 0;
// One has already been subtracted from clusterCounts[maxVarCluster].
variances[maxVarCluster] = (1.0 / (clusterCounts[maxVarCluster])) *
((clusterCounts[maxVarCluster] + 1) * variances[maxVarCluster] - maxDistance);
// Output some debugging information.
Log::Debug << "Point " << furthestPoint << " assigned to empty cluster " <<
emptyCluster << ".\n";
return 1; // We only changed one point.
}
示例9: set_coefficients
void Recipe::set_coefficients(int i, int j, const MatType &coef) {
for(int in_chan = 0; in_chan < coef.rows(); ++in_chan)
for(int out_chan = 0; out_chan < coef.cols(); ++out_chan)
{
int ac_map_i = in_chan*height + i;
int ac_map_j = out_chan*width +j;
ac(ac_map_i, ac_map_j) = coef(in_chan,out_chan);
}
}
示例10: PerformSplit
size_t PerformSplit(MatType& data,
const size_t begin,
const size_t count,
const typename SplitType::SplitInfo& splitInfo,
std::vector<size_t>& oldFromNew)
{
// This method modifies the input dataset. We loop both from the left and
// right sides of the points contained in this node.
size_t left = begin;
size_t right = begin + count - 1;
// First half-iteration of the loop is out here because the termination
// condition is in the middle.
while ((left <= right) &&
(SplitType::AssignToLeftNode(data.col(left), splitInfo)))
left++;
while ((!SplitType::AssignToLeftNode(data.col(right), splitInfo)) &&
(left <= right) && (right > 0))
right--;
// Shortcut for when all points are on the right.
if (left == right && right == 0)
return left;
while (left <= right)
{
// Swap columns.
data.swap_cols(left, right);
// Update the indices for what we changed.
size_t t = oldFromNew[left];
oldFromNew[left] = oldFromNew[right];
oldFromNew[right] = t;
// See how many points on the left are correct. When they are correct,
// increase the left counter accordingly. When we encounter one that isn't
// correct, stop. We will switch it later.
while (SplitType::AssignToLeftNode(data.col(left), splitInfo) &&
(left <= right))
left++;
// Now see how many points on the right are correct. When they are correct,
// decrease the right counter accordingly. When we encounter one that isn't
// correct, stop. We will switch it with the wrong point we found in the
// previous loop.
while ((!SplitType::AssignToLeftNode(data.col(right), splitInfo)) &&
(left <= right))
right--;
}
Log::Assert(left == right + 1);
return left;
}
示例11: EmptyCluster
size_t MaxVarianceNewCluster::EmptyCluster(const MatType& data,
const size_t emptyCluster,
const MatType& centroids,
arma::Col<size_t>& clusterCounts,
arma::Col<size_t>& assignments)
{
// First, we need to find the cluster with maximum variance (by which I mean
// the sum of the covariance matrices).
arma::vec variances;
variances.zeros(clusterCounts.n_elem); // Start with 0.
// Add the variance of each point's distance away from the cluster. I think
// this is the sensible thing to do.
for (size_t i = 0; i < data.n_cols; i++)
{
variances[assignments[i]] += arma::as_scalar(
arma::var(data.col(i) - centroids.col(assignments[i])));
}
// Now find the cluster with maximum variance.
arma::uword maxVarCluster;
variances.max(maxVarCluster);
// Now, inside this cluster, find the point which is furthest away.
size_t furthestPoint = data.n_cols;
double maxDistance = -DBL_MAX;
for (size_t i = 0; i < data.n_cols; i++)
{
if (assignments[i] == maxVarCluster)
{
double distance = arma::as_scalar(
arma::var(data.col(i) - centroids.col(maxVarCluster)));
if (distance > maxDistance)
{
maxDistance = distance;
furthestPoint = i;
}
}
}
// Take that point and add it to the empty cluster.
clusterCounts[maxVarCluster]--;
clusterCounts[emptyCluster]++;
assignments[furthestPoint] = emptyCluster;
// Output some debugging information.
Log::Debug << "Point " << furthestPoint << " assigned to empty cluster " <<
emptyCluster << ".\n";
return 1; // We only changed one point.
}
示例12: dimCheck
/** Dimensionality check during initialization */
bool dimCheck(){
if( Sx_.rows() != Sx_.cols() ){
std::cerr << "Error: MatType must be a square matrix \n";
return false;
}
if( Sx_.rows() != x_.size() ){
std::cerr << "Error: VecType and MatType dimension mismatch \n";
return false;
}
nDim_ = x_.size();
return true;
}
示例13: while
void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Train(
const MatType& data,
const arma::Row<size_t>& labels,
const arma::rowvec& instanceWeights)
{
size_t j, i = 0;
bool converged = false;
size_t tempLabel;
arma::uword maxIndexRow, maxIndexCol;
arma::mat tempLabelMat;
LearnPolicy LP;
const bool hasWeights = (instanceWeights.n_elem > 0);
while ((i < maxIterations) && (!converged))
{
// This outer loop is for each iteration, and we use the 'converged'
// variable for noting whether or not convergence has been reached.
i++;
converged = true;
// Now this inner loop is for going through the dataset in each iteration.
for (j = 0; j < data.n_cols; j++)
{
// Multiply for each variable and check whether the current weight vector
// correctly classifies this.
tempLabelMat = weights.t() * data.col(j) + biases;
tempLabelMat.max(maxIndexRow, maxIndexCol);
// Check whether prediction is correct.
if (maxIndexRow != labels(0, j))
{
// Due to incorrect prediction, convergence set to false.
converged = false;
tempLabel = labels(0, j);
// Send maxIndexRow for knowing which weight to update, send j to know
// the value of the vector to update it with. Send tempLabel to know
// the correct class.
if (hasWeights)
LP.UpdateWeights(data.col(j), weights, biases, maxIndexRow, tempLabel,
instanceWeights(j));
else
LP.UpdateWeights(data.col(j), weights, biases, maxIndexRow,
tempLabel);
}
}
}
}
示例14: CalculateCentroid
typename std::enable_if<ApplyKernel, bool>::type
MeanShift<UseKernel, KernelType, MatType>::
CalculateCentroid(const MatType& data,
const std::vector<size_t>& neighbors,
const std::vector<double>& distances,
arma::colvec& centroid)
{
double sumWeight = 0;
for (size_t i = 0; i < neighbors.size(); ++i)
{
if (distances[i] > 0)
{
double dist = distances[i] / radius;
double weight = kernel.Gradient(dist) / dist;
sumWeight += weight;
centroid += weight * data.unsafe_col(neighbors[i]);
}
}
if (sumWeight != 0)
{
centroid /= sumWeight;
return true;
}
return false;
}
示例15: Initialize
inline static void Initialize(const MatType& V,
const size_t r,
arma::mat& W,
arma::mat& H)
{
const size_t n = V.n_rows;
const size_t m = V.n_cols;
if (columnsToAverage > m)
{
Log::Warn << "Number of random columns (columnsToAverage) is more than "
<< "the number of columns available in the V matrix; weird results "
<< "may ensue!" << std::endl;
}
W.zeros(n, r);
// Initialize W matrix with random columns.
for (size_t col = 0; col < r; col++)
{
for (size_t randCol = 0; randCol < columnsToAverage; randCol++)
{
// .col() does not work in this case, as of Armadillo 3.920.
W.unsafe_col(col) += V.col(math::RandInt(0, m));
}
}
// Now divide by p.
W /= columnsToAverage;
// Initialize H to random values.
H.randu(r, m);
}