本文整理汇总了C++中CFactor类的典型用法代码示例。如果您正苦于以下问题:C++ CFactor类的具体用法?C++ CFactor怎么用?C++ CFactor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CFactor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createVarClustering
CFactorGraph CompressInterface::createCFactorGraph() {
createVarClustering();
createFacClustering();
// create lifted fg here
vector<CFactor> superFacs;
// clusterIdx => facIdx
for (map<size_t,size_t>::iterator facIter = _facRepr.begin(); facIter != _facRepr.end(); facIter++) {
VarSet superVarSet;
foreach (const dai::BipartiteGraph::Neighbor &tmpVar, _cfg.nbF(facIter->second)) {
Var varCluster = _varRepr[_varColorVec[tmpVar]];
if (!superVarSet.contains(varCluster)) {
superVarSet |= Var(varCluster);
}
}
CFactor superFac = CFactor(superVarSet, _cfg.factor(facIter->second).p());
superFac.sigma() = _cfg.factor(facIter->second).sigma();
superFac.position() = _cfg.factor(facIter->second).position();
superFac.counts() = createCounts(facIter->second, superVarSet);
superFacs.push_back(superFac);
}
return CFactorGraph(superFacs);
}
示例2: EnterEvidence
void CLWSamplingInfEngine::
EnterEvidence( const CEvidence *pEvidenceIn , int maximize , int sumOnMixtureNode )
{
PNL_CHECK_IS_NULL_POINTER(pEvidenceIn);
LWSampling(pEvidenceIn);
// Given evidencs, calculate particle weight by CPD
float w;
int iSampleSize = m_currentEvVec.size();
const int* ObsNodes = pEvidenceIn->GetAllObsNodes();
int NumberObsNodes = pEvidenceIn->GetNumberObsNodes();
int i, j;
for( i = 0; i < iSampleSize; i++)
{
w = 0;
for( j = 0; j < NumberObsNodes; j++)
{
if(pEvidenceIn->IsNodeObserved(ObsNodes[j]))
{
CFactor* pFactor = m_pGraphicalModel->GetFactor(ObsNodes[j]);
w = w + pFactor->GetLogLik( m_currentEvVec[i]);
}
}
m_particleWeight[i] = (float)exp(w);
}
NormalizeWeight();
}
示例3: compute_energies
void CFactorGraph::compute_energies()
{
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
fac->compute_energies();
SG_UNREF(fac);
}
}
示例4: ASSERT
void CFactorGraph::loss_augmentation(SGVector<int32_t> states_gt, SGVector<float64_t> loss)
{
if (loss.size() == 0)
{
loss.resize_vector(states_gt.size());
SGVector<float64_t>::fill_vector(loss.vector, loss.vlen, 1.0 / states_gt.size());
}
int32_t num_vars = states_gt.size();
ASSERT(num_vars == loss.size());
SGVector<int32_t> var_flags(num_vars);
var_flags.zero();
// augment loss to incorrect states in the first factor containing the variable
// since += L_i for each variable if it takes wrong state ever
// TODO: augment unary factors
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
SGVector<int32_t> vars = fac->get_variables();
for (int32_t vi = 0; vi < vars.size(); vi++)
{
int32_t vv = vars[vi];
ASSERT(vv < num_vars);
if (var_flags[vv])
continue;
SGVector<float64_t> energies = fac->get_energies();
for (int32_t ei = 0; ei < energies.size(); ei++)
{
CTableFactorType* ftype = fac->get_factor_type();
int32_t vstate = ftype->state_from_index(ei, vi);
SG_UNREF(ftype);
if (states_gt[vv] == vstate)
continue;
// -delta(y_n, y_i_n)
fac->set_energy(ei, energies[ei] - loss[vv]);
}
var_flags[vv] = 1;
}
SG_UNREF(fac);
}
// make sure all variables have been checked
int32_t min_var = SGVector<int32_t>::min(var_flags.vector, var_flags.vlen);
ASSERT(min_var == 1);
}
示例5: ASSERT
float64_t CFactorGraph::evaluate_energy(const SGVector<int32_t> state) const
{
ASSERT(state.size() == m_cardinalities.size());
float64_t energy = 0.0;
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
energy += fac->evaluate_energy(state);
SG_UNREF(fac);
}
return energy;
}
示例6: rearrangeCnfFactor
void CompressInterface::rearrangeCnfFactor(CFactor &cnfClause) {
State state(cnfClause.vars());
vector<size_t> (cnfClause.vars().size());
vector<size_t> pos,neg,sigma;
for (size_t i=0; i<cnfClause.states(); i++) {
if (cnfClause[i] == 1) {
for (vector<Var>::const_iterator varIter=cnfClause.vars().begin(); varIter!=cnfClause.vars().end();varIter++) {
if (state(*varIter) == 0) {
pos.push_back(varIter - cnfClause.vars().begin());
} else {
neg.push_back(varIter - cnfClause.vars().begin());
}
}
break;
} else if (cnfClause[i] == 0) {
//TODO handle factors which contain zero states
// in such cases the possible zero state could have been clamped;
// possible solutions:
// i) restore factor from backup (requires to clamp factors accordingly)
// ii) cache zero states in advance
return;
}
state++;
}
sigma.insert(sigma.begin(),pos.begin(), pos.end());
sigma.insert(sigma.begin() + pos.size(),neg.begin(), neg.end());
cnfClause.sigma() = sigma;
}
示例7: PNL_THROW
void CBayesLearningEngine::Learn()
{
if(!m_pGrModel)
{
PNL_THROW( CNULLPointer, "no graphical model")
}
CStaticGraphicalModel *grmodel = this->GetStaticModel();
CFactor *factor = NULL;
int numberOfFactors = grmodel->GetNumberOfFactors();
int domainNodes;
if(m_numberOfLearnedEvidences == m_numberOfAllEvidences)
{
PNL_THROW(COutOfRange, "number of unlearned evidences must be positive")
}
int currentEvidNumber;
const CEvidence* pCurrentEvid;
//below code is intended to work on tabular CPD and gaussian CPD
//later we will generalize it for other distribution types
if ((grmodel->GetFactor(0))->GetDistributionType() == dtTabular)
{
for( int ev = m_numberOfLearnedEvidences; ev < m_numberOfAllEvidences; ev++)
{
currentEvidNumber = ev;
pCurrentEvid = m_Vector_pEvidences[currentEvidNumber];
if( !pCurrentEvid)
{
PNL_THROW(CNULLPointer, "evidence")
}
for( domainNodes = 0; domainNodes < numberOfFactors; domainNodes++ )
{
factor = grmodel->GetFactor( domainNodes );
int DomainSize;
const int *domain;
factor->GetDomain( &DomainSize, &domain );
const CEvidence *pEvidences[] = { pCurrentEvid };
CTabularDistribFun* pDistribFun = (CTabularDistribFun*)(factor->GetDistribFun());
pDistribFun->BayesUpdateFactor(pEvidences, 1, domain);
}
}
}
else
{
for( domainNodes = 0; domainNodes < numberOfFactors; domainNodes++ )
示例8: psi
SGVector< float64_t > CFactorGraphModel::get_joint_feature_vector(int32_t feat_idx, CStructuredData* y)
{
// factor graph instance
CFactorGraphFeatures* mf = CFactorGraphFeatures::obtain_from_generic(m_features);
CFactorGraph* fg = mf->get_sample(feat_idx);
// ground truth states
CFactorGraphObservation* fg_states = CFactorGraphObservation::obtain_from_generic(y);
SGVector<int32_t> states = fg_states->get_data();
// initialize psi
SGVector<float64_t> psi(get_dim());
psi.zero();
// construct unnormalized psi
CDynamicObjectArray* facs = fg->get_factors();
for (int32_t fi = 0; fi < facs->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(facs->get_element(fi));
CTableFactorType* ftype = fac->get_factor_type();
int32_t id = ftype->get_type_id();
SGVector<int32_t> w_map = get_params_mapping(id);
ASSERT(w_map.size() == ftype->get_w_dim());
SGVector<float64_t> dat = fac->get_data();
int32_t dat_size = dat.size();
ASSERT(w_map.size() == dat_size * ftype->get_num_assignments());
int32_t ei = ftype->index_from_universe_assignment(states, fac->get_variables());
for (int32_t di = 0; di < dat_size; di++)
psi[w_map[ei*dat_size + di]] += dat[di];
SG_UNREF(ftype);
SG_UNREF(fac);
}
// negation (-E(x,y) = <w,phi(x,y)>)
psi.scale(-1.0);
SG_UNREF(facs);
SG_UNREF(fg);
return psi;
}
示例9: CreateWithRandomMatrices
CMNet* CMNet::CreateWithRandomMatrices( const intVecVector& clqs,
CModelDomain* pMD)
{
CMNet* pMNet = CMNet::Create( clqs, pMD );
pMNet->AllocFactors();
int numFactors = pMNet->GetNumberOfFactors();
int i;
for( i = 0; i < numFactors; i++ )
{
pMNet->AllocFactor( i );
CFactor* ft = pMNet->GetFactor(i);
ft->CreateAllNecessaryMatrices();
}
return pMNet;
}
示例10: cfactor_setitem
void cfactor_setitem(CFactor& factor, size_t index, double value)
{
if (index >= 0 && index < factor.states()) {
factor[index] = value;
}
else {
PyErr_SetString(PyExc_IndexError, "index out of range");
boost::python::throw_error_already_set();
}
}
示例11:
//create mnet with random matrices
CMRF2* CMRF2::CreateWithRandomMatrices( int numberOfCliques,
const int *cliqueSizes,
const int **cliques,
CModelDomain* pMD)
{
CMRF2* pMRF2 = CMRF2::Create( numberOfCliques, cliqueSizes, cliques, pMD );
pMRF2->AllocFactors();
int numFactors = pMRF2->GetNumberOfFactors();
int i;
for( i = 0; i < numFactors; i++ )
{
pMRF2->AllocFactor( i );
CFactor* ft = pMRF2->GetFactor(i);
ft->CreateAllNecessaryMatrices();
}
return pMRF2;
}
示例12: PNL_THROW
void CMlLearningEngine::Learn()
{
/*
function takes an information from m_pEvidences and learns factors
of graphical model using prior probabilities or not
*/
float logLikTmp = 0;
if(!m_pGrModel)
{
PNL_THROW( CNULLPointer, "no graphical model")
}
CStaticGraphicalModel *grmodel = this->GetStaticModel();
CFactor *parameter = NULL;
int numberOfDomains = grmodel -> GetNumberOfFactors();
for( int domainNodes = 0; domainNodes < numberOfDomains; domainNodes++ )
{
factor = grmodel->GetFactor( domainNodes );
factor ->UpdateStatisticsML( &m_Vector_pEvidences.front(),
m_Vector_pEvidences.size() );
PNL_CHECK_LEFT_BORDER(m_numberOfAllEvidences, 1);
logLikTmp += parameter->ProcessingStatisticalData(m_numberOfAllEvidences);
}
switch( grmodel -> GetModelType() )
{
case mtBNet:
{
break;
}
case mtMRF2:
case mtMNet:
{
logLikTmp = _LearnPotentials();
break;
}
default:
{
PNL_THROW(CBadConst, "model type" )
break;
}
}
m_critValue.push_back(logLikTmp);
}
示例13: cfactor_getitem
double cfactor_getitem(CFactor &factor, size_t index)
{
if (index >= 0 && index < factor.states()) {
return factor[index];
}
else {
PyErr_SetString(PyExc_IndexError, "index out of range");
boost::python::throw_error_already_set();
return -1;
}
}
示例14: CreateRandomCPD
CCPD* CStaticStructLearnSEM::CreateRandomCPD(int nfamily, const int* family, CBNet* pBNet)
{
int child = family[nfamily-1];
CModelDomain* pMD = pBNet->GetModelDomain();
CFactor* factor = pBNet->GetFactor(child);
EDistributionType dt = factor->GetDistributionType();
CCPD* pCPD;
if( dt == dtTabular )
{
pCPD = CTabularCPD::Create(family, nfamily, pMD);
pCPD->CreateAllNecessaryMatrices(1);
return pCPD;
}
else
{
if( dt == dtMixGaussian )
{
floatVector data;
static_cast<CMixtureGaussianCPD*>(factor)->GetProbabilities(&data);
pCPD = CMixtureGaussianCPD::Create(family, nfamily, pMD, &data.front());
static_cast<CCondGaussianDistribFun*>(pCPD->GetDistribFun()) -> CreateDefaultMatrices(1);
return pCPD;
}
else
{
if( (dt == dtGaussian) || (dt == dtCondGaussian) )
{
pCPD = CGaussianCPD::Create(family, nfamily, pMD);
pCPD->CreateAllNecessaryMatrices(1);
return pCPD;
}
else
PNL_THROW(CNotImplemented, "this type of distribution is not supported yet");
}
}
}
示例15: connect_components
void CFactorGraph::connect_components()
{
if (m_dset->get_connected())
return;
// need to be reset once factor graph is updated
m_dset->make_sets();
bool flag = false;
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
SGVector<int32_t> vars = fac->get_variables();
int32_t r0 = m_dset->find_set(vars[0]);
for (int32_t vi = 1; vi < vars.size(); vi++)
{
// for two nodes in a factor, should be an edge between them
// but this time link() isn't performed, if they are linked already
// means there is another path connected them, so cycle detected
int32_t ri = m_dset->find_set(vars[vi]);
if (r0 == ri)
{
flag = true;
continue;
}
r0 = m_dset->link_set(r0, ri);
}
SG_UNREF(fac);
}
m_has_cycle = flag;
m_dset->set_connected(true);
}