本文整理汇总了C++中ArrayXXd::row方法的典型用法代码示例。如果您正苦于以下问题:C++ ArrayXXd::row方法的具体用法?C++ ArrayXXd::row怎么用?C++ ArrayXXd::row使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ArrayXXd
的用法示例。
在下文中一共展示了ArrayXXd::row方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Exception
ArrayXXd CMT::BlobNonlinearity::gradient(const ArrayXXd& inputs) const {
if(inputs.rows() != 1)
throw Exception("Data has to be stored in one row.");
ArrayXXd diff = ArrayXXd::Zero(mNumComponents, inputs.cols());
diff.rowwise() += inputs.row(0);
diff.colwise() -= mMeans;
ArrayXXd diffSq = diff.square();
ArrayXd precisions = mLogPrecisions.exp();
ArrayXd weights = mLogWeights.exp();
ArrayXXd negEnergy = diffSq.colwise() * (-precisions / 2.);
ArrayXXd negEnergyExp = negEnergy.exp();
ArrayXXd gradient(3 * mNumComponents, inputs.cols());
// gradient of mean
gradient.topRows(mNumComponents) = (diff * negEnergyExp).colwise() * (weights * precisions);
// gradient of log-precisions
gradient.middleRows(mNumComponents, mNumComponents) = (diffSq / 2. * negEnergyExp).colwise() * (-weights * precisions);
// gradient of log-weights
gradient.bottomRows(mNumComponents) = negEnergyExp.colwise() * weights;
return gradient;
}
示例2: arrayMultiplierRowWise
/*
Multiply each row of u by temp
*/
MatrixXd arrayMultiplierRowWise(MatrixXd u,ArrayXXd temp,int n){
ArrayXXd uArray = u.array();
int i;
for(i=0;i<n;i++){
uArray.row(i) *= temp;
}
return uArray.matrix();
}
示例3: logJoint
bool CMT::Mixture::train(
const MatrixXd& data,
const MatrixXd& dataValid,
const Parameters& parameters,
const Component::Parameters& componentParameters)
{
if(parameters.initialize && !initialized())
initialize(data, parameters, componentParameters);
ArrayXXd logJoint(numComponents(), data.cols());
Array<double, Dynamic, 1> postSum;
Array<double, 1, Dynamic> logLik;
ArrayXXd post;
ArrayXXd weights;
// training and validation log-loss for checking convergence
double avgLogLoss = numeric_limits<double>::infinity();
double avgLogLossNew;
double avgLogLossValid = evaluate(dataValid);
double avgLogLossValidNew = avgLogLossValid;
int counter = 0;
// backup model parameters
VectorXd priors = mPriors;
vector<Component*> components;
for(int k = 0; k < numComponents(); ++k)
components.push_back(mComponents[k]->copy());
for(int i = 0; i < parameters.maxIter; ++i) {
// compute joint probability of data and assignments (E)
#pragma omp parallel for
for(int k = 0; k < numComponents(); ++k)
logJoint.row(k) = mComponents[k]->logLikelihood(data) + log(mPriors[k]);
// compute normalized posterior (E)
logLik = logSumExp(logJoint);
// average negative log-likelihood in bits per component
avgLogLossNew = -logLik.mean() / log(2.) / dim();
if(parameters.verbosity > 0) {
if(i % parameters.valIter == 0) {
// print training and validation error
cout << setw(6) << i;
cout << setw(14) << setprecision(7) << avgLogLossNew;
cout << setw(14) << setprecision(7) << avgLogLossValidNew << endl;
} else {
// print training error
cout << setw(6) << i << setw(14) << setprecision(7) << avgLogLossNew << endl;
}
}
// test for convergence
if(avgLogLoss - avgLogLossNew < parameters.threshold)
return true;
avgLogLoss = avgLogLossNew;
// compute normalized posterior (E)
post = (logJoint.rowwise() - logLik).exp();
postSum = post.rowwise().sum();
weights = post.colwise() / postSum;
// optimize prior weights (M)
if(parameters.trainPriors) {
mPriors = postSum / data.cols() + parameters.regularizePriors;
mPriors /= mPriors.sum();
}
// optimize components (M)
if(parameters.trainComponents) {
#pragma omp parallel for
for(int k = 0; k < numComponents(); ++k)
mComponents[k]->train(data, weights.row(k), componentParameters);
} else {
return true;
}
if((i + 1) % parameters.valIter == 0) {
// check validation error
avgLogLossValidNew = evaluate(dataValid);
if(avgLogLossValidNew < avgLogLossValid) {
// backup new found model parameters
priors = mPriors;
for(int k = 0; k < numComponents(); ++k)
*components[k] = *mComponents[k];
avgLogLossValid = avgLogLossValidNew;
} else {
counter++;
if(parameters.valLookAhead > 0 && counter >= parameters.valLookAhead) {
// set parameters to best parameters found during training
mPriors = priors;
for(int k = 0; k < numComponents(); ++k) {
*mComponents[k] = *components[k];
delete components[k];
}
//.........这里部分代码省略.........
示例4: Exception
bool CMT::Mixture::train(
const MatrixXd& data,
const Parameters& parameters,
const Component::Parameters& componentParameters)
{
if(data.rows() != dim())
throw Exception("Data has wrong dimensionality.");
if(parameters.initialize && !initialized())
initialize(data, parameters, componentParameters);
ArrayXXd logJoint(numComponents(), data.cols());
Array<double, Dynamic, 1> postSum;
Array<double, 1, Dynamic> logLik;
ArrayXXd post;
ArrayXXd weights;
double avgLogLoss = numeric_limits<double>::infinity();
double avgLogLossNew;
for(int i = 0; i < parameters.maxIter; ++i) {
// compute joint probability of data and assignments (E)
#pragma omp parallel for
for(int k = 0; k < numComponents(); ++k)
logJoint.row(k) = mComponents[k]->logLikelihood(data) + log(mPriors[k]);
// compute normalized posterior (E)
logLik = logSumExp(logJoint);
// average negative log-likelihood in bits per component
avgLogLossNew = -logLik.mean() / log(2.) / dim();
if(parameters.verbosity > 0)
cout << setw(6) << i << setw(14) << setprecision(7) << avgLogLossNew << endl;
// test for convergence
if(avgLogLoss - avgLogLossNew < parameters.threshold)
return true;
avgLogLoss = avgLogLossNew;
// compute normalized posterior (E)
post = (logJoint.rowwise() - logLik).exp();
postSum = post.rowwise().sum();
weights = post.colwise() / postSum;
// optimize prior weights (M)
if(parameters.trainPriors) {
mPriors = postSum / data.cols() + parameters.regularizePriors;
mPriors /= mPriors.sum();
}
// optimize components (M)
if(parameters.trainComponents) {
#pragma omp parallel for
for(int k = 0; k < numComponents(); ++k)
mComponents[k]->train(data, weights.row(k), componentParameters);
} else {
return true;
}
}
if(parameters.verbosity > 0)
cout << setw(6) << parameters.maxIter << setw(14) << setprecision(7) << evaluate(data) << endl;
return false;
}