33static const float kScoreScaleFactor = 100.0f;
35static const float kMinFinalCost = 0.001f;
37static const float kMaxFinalCost = 100.0f;
41 tprintf(
"ParamsModel for pass %d lang %s\n",
p, lang_.c_str());
42 for (
unsigned i = 0;
i < weights_vec_[
p].size(); ++
i) {
43 tprintf(
"%s = %g\n", kParamsTrainingFeatureTypeName[
i], weights_vec_[
p][
i]);
56bool ParamsModel::ParseLine(
char *line,
char **key,
float *val) {
61 while (line[end_of_key] && !(isascii(line[end_of_key]) && isspace(line[end_of_key]))) {
64 if (!line[end_of_key]) {
65 tprintf(
"ParamsModel::Incomplete line %s\n", line);
68 line[end_of_key++] = 0;
70 if (sscanf(line + end_of_key,
" %f", val) != 1) {
82 float unnorm_score = 0.0;
84 unnorm_score += weights_vec_[pass_][f] * features[f];
86 return ClipToRange(-unnorm_score / kScoreScaleFactor, kMinFinalCost, kMaxFinalCost);
90 float epsilon = 0.0001f;
92 if (weights_vec_[
p].size() != that.weights_vec_[
p].size()) {
95 for (
unsigned i = 0;
i < weights_vec_[
p].size();
i++) {
96 if (weights_vec_[
p][
i] != that.weights_vec_[
p][
i] &&
97 std::fabs(weights_vec_[
p][
i] - that.weights_vec_[
p][
i]) > epsilon) {
106 const int kMaxLineSize = 100;
107 char line[kMaxLineSize];
112 std::vector<float> &
weights = weights_vec_[pass_];
116 while (fp->
FGets(line, kMaxLineSize) !=
nullptr) {
119 if (!ParseLine(line, &key, &
value)) {
124 tprintf(
"ParamsModel::Unknown parameter %s\n", key);
136 tprintf(
"Missing field %s.\n", kParamsTrainingFeatureTypeName[
i]);
146 const std::vector<float> &
weights = weights_vec_[pass_];
148 tprintf(
"Refusing to save ParamsModel that has not been initialized.\n");
151 FILE *fp = fopen(full_path,
"wb");
153 tprintf(
"Could not open %s for writing.\n", full_path);
156 bool all_good =
true;
157 for (
unsigned i = 0;
i <
weights.size();
i++) {
158 if (fprintf(fp,
"%s %f\n", kParamsTrainingFeatureTypeName[
i],
weights[
i]) < 0) {
int ParamsTrainingFeatureByName(const char *name)
void tprintf(const char *format,...)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
@ PTRAIN_NUM_FEATURE_TYPES
void SetValue(int index, bool value)
char * FGets(char *buffer, int buffer_size)
bool SaveToFile(const char *full_path) const
const std::vector< float > & weights() const
float ComputeCost(const float features[]) const
bool LoadFromFp(const char *lang, TFile *fp)
bool Equivalent(const ParamsModel &that) const
const std::vector< float > & weights_for_pass(PassEnum pass) const
void Copy(const ParamsModel &other_model)