本文整理汇总了C++中Factor::SetAdditionalLogPotentials方法的典型用法代码示例。如果您正苦于以下问题:C++ Factor::SetAdditionalLogPotentials方法的具体用法?C++ Factor::SetAdditionalLogPotentials怎么用?C++ Factor::SetAdditionalLogPotentials使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Factor
的用法示例。
在下文中一共展示了Factor::SetAdditionalLogPotentials方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LoadGraphUAI
//.........这里部分代码省略.........
// Read number of factors (includes unary factors).
getline(file_graph, line);
TrimComments("#", &line);
int num_factors = atoi(line.c_str());
// Read factors (just the structure).
vector<Factor*> factors(num_factors);
vector<MultiVariable*> unary_factors(num_factors);
for (int i = 0; i < num_factors; ++i) {
getline(file_graph, line);
TrimComments("#", &line);
fields.clear();
StringSplit(line, "\t ", &fields);
// Read linked multi-variables.
int num_links = atoi(fields[0].c_str());
int offset = 1;
assert(num_links == fields.size() - offset);
if (num_links == 1) {
// Unary factor; in our formalism this is just a multi-variable.
int k = atoi(fields[offset].c_str());
unary_factors[i] = multi_variables[k];
} else {
vector<MultiVariable*> multi_variables_local(num_links);
for (int j = 0; j < num_links; ++j) {
int k = atoi(fields[offset + j].c_str());
multi_variables_local[j] = multi_variables[k];
}
// For now, set an empty vector of additional log potentials.
vector<double> additional_log_potentials;
Factor *factor =
factor_graph->CreateFactorDense(multi_variables_local,
additional_log_potentials);
factors[i] = factor;
}
}
// Read factors (the log-potentials).
// IMPORTANT: the scores in the UAI files are potentials (not log-potentials!)
for (int i = 0; i < num_factors; ++i) {
Factor *factor = factors[i];
line = "";
while (line == "") {
getline(file_graph, line);
TrimComments("#", &line);
Trim(" \t", &line);
}
int num_configurations = atoi(line.c_str());
if (factor == NULL) {
// Unary factor; in our formalism this is just a multi-variable.
assert(unary_factors[i] != NULL);
MultiVariable *multi_variable = unary_factors[i];
int index = 0;
assert(num_configurations == multi_variable->GetNumStates());
while (index < num_configurations) {
getline(file_graph, line);
TrimComments("#", &line);
Trim(" \t", &line);
fields.clear();
StringSplit(line, "\t ", &fields);
for (int j = 0; j < fields.size(); ++j) {
double log_potential = LOG_STABLE(atof(fields[j].c_str()));
multi_variable->SetLogPotential(index, log_potential);
assert(index < num_configurations);
++index;
}
}
} else {
int num_links = static_cast<FactorDense*>(factor)->
GetNumMultiVariables();
int index = 0;
assert(num_configurations ==
static_cast<FactorDense*>(factor)->GetNumConfigurations());
//int r = factor_graph->GetNumVariables() + num_factor_log_potentials;
num_factor_log_potentials += num_configurations;
//static_cast<FactorMultiDense*>(factor)->SetFirstGlobalIndex(r);
vector<double> additional_log_potentials(num_configurations);
while (index < num_configurations) {
getline(file_graph, line);
TrimComments("#", &line);
Trim(" \t", &line);
fields.clear();
StringSplit(line, "\t ", &fields);
for (int j = 0; j < fields.size(); ++j) {
double log_potential = LOG_STABLE(atof(fields[j].c_str()));
additional_log_potentials[index] = log_potential;
assert(index < num_configurations);
++index;
}
}
factor->SetAdditionalLogPotentials(additional_log_potentials);
}
}
cout << "Read " << num_multi_variables << " multi-variables and "
<< num_factors << " factors." << endl;
return 0;
}
示例2: LoadGraph
//.........这里部分代码省略.........
++num_factor_log_potentials;
//static_cast<FactorPAIR*>(factor)->SetGlobalIndex(r);
//static_cast<FactorPAIR*>(factor)->SetFactorLogPotential(log_potential);
factor = factor_graph->CreateFactorPAIR(binary_variables, log_potential);
} else if (fields[0] == "DENSE") {
// Read the number of multi-variables.
int num_multi_variables = atoi(fields[offset+num_links].c_str());
// Read the number of states for each multi-variable.
vector<MultiVariable*> multi_variables(num_multi_variables);
int num_configurations = 1;
int total_states = 0;
for (int k = 0; k < num_multi_variables; ++k) {
int num_states = atoi(fields[offset+num_links+1+k].c_str());
num_configurations *= num_states;
vector<BinaryVariable*> states(binary_variables.begin() + total_states,
binary_variables.begin() + total_states +
num_states);
total_states += num_states;
multi_variables[k] = factor_graph->CreateMultiVariable(states);
}
// Read the additional log-potentials.
vector<double> additional_scores;
for (int index = 0; index < num_configurations; ++index) {
// Read the factor log-potential for this configuration.
double log_potential = atof(fields[offset+num_links+1+num_multi_variables+index].c_str());
additional_scores.push_back(log_potential);
}
// Create the factor and declare it.
factor = new FactorDense;
factor_graph->DeclareFactor(factor, binary_variables, true);
static_cast<FactorDense*>(factor)->Initialize(multi_variables);
factor->SetAdditionalLogPotentials(additional_scores);
num_factor_log_potentials += additional_scores.size();
cout << "Read dense factor." << endl;
} else if (fields[0] == "SEQUENCE" ||
fields[0] == "SEQUENCE_BUDGET") {
bool has_budget = false;
if (fields[0] == "SEQUENCE_BUDGET") has_budget = true;
// Read the sequence length.
int length = atoi(fields[offset+num_links].c_str());
// If budget, read the budget.
int budget = -1;
if (has_budget) {
++offset; // TODO: Make sure this is fine.
budget = atoi(fields[offset+num_links].c_str());
}
// Read the number of states for each position in the sequence.
vector<int> num_states(length);
int total_states = 0;
for (int k = 0; k < length; ++k) {
num_states[k] = atoi(fields[offset+num_links+1+k].c_str());
total_states += num_states[k];
}
// Read the additional log-potentials.
vector<double> additional_scores;
int index = 0;
for (int i = 0; i <= length; ++i) {
// If i == 0, the previous state is the start symbol.
int num_previous_states = (i > 0)? num_states[i - 1] : 1;
// If i == length-1, the previous state is the final symbol.
int num_current_states = (i < length)? num_states[i] : 1;
for (int j = 0; j < num_previous_states; ++j) {