32 static const float kScoreScaleFactor = 100.0f;
34 static const float kMinFinalCost = 0.001f;
36 static const float kMaxFinalCost = 100.0f;
40 tprintf(
"ParamsModel for pass %d lang %s\n", p, lang_.
string());
41 for (
int i = 0; i < weights_vec_[p].
size(); ++i) {
42 tprintf(
"%s = %g\n", kParamsTrainingFeatureTypeName[i],
51 static_cast<PassEnum>(p));
57 bool ParamsModel::ParseLine(
char *line,
char** key,
float *val) {
61 while (line[end_of_key] && !isspace(line[end_of_key])) end_of_key++;
62 if (!line[end_of_key]) {
63 tprintf(
"ParamsModel::Incomplete line %s\n", line);
66 line[end_of_key++] = 0;
68 if (sscanf(line + end_of_key,
" %f", val) != 1)
79 float unnorm_score = 0.0;
81 unnorm_score += weights_vec_[pass_][f] * features[f];
83 return ClipToRange(-unnorm_score / kScoreScaleFactor,
84 kMinFinalCost, kMaxFinalCost);
88 float epsilon = 0.0001;
90 if (weights_vec_[p].size() != that.weights_vec_[p].
size())
return false;
91 for (
int i = 0; i < weights_vec_[p].
size(); i++) {
92 if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
93 fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
102 const char *full_path) {
103 FILE *fp = fopen(full_path,
"rb");
105 tprintf(
"Error opening file %s\n", full_path);
114 const int kMaxLineSize = 100;
115 char line[kMaxLineSize];
123 while ((end_offset < 0 || ftell(fp) < end_offset) &&
124 fgets(line, kMaxLineSize, fp)) {
127 if (!ParseLine(line, &key, &value))
131 tprintf(
"ParamsModel::Unknown parameter %s\n", key);
143 tprintf(
"Missing field %s.\n", kParamsTrainingFeatureTypeName[i]);
155 tprintf(
"Refusing to save ParamsModel that has not been initialized.\n");
158 FILE *fp = fopen(full_path,
"wb");
160 tprintf(
"Could not open %s for writing.\n", full_path);
163 bool all_good =
true;
165 if (fprintf(fp,
"%s %f\n", kParamsTrainingFeatureTypeName[i],
weights[i])
const GenericVector< float > & weights() const
int ParamsTrainingFeatureByName(const char *name)
float ComputeCost(const float features[]) const
const GenericVector< float > & weights_for_pass(PassEnum pass) const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
bool SaveToFile(const char *full_path) const
void SetValue(int index, bool value)
const char * string() const
bool LoadFromFp(const char *lang, FILE *fp, inT64 end_offset)
bool LoadFromFile(const char *lang, const char *full_path)
void Copy(const ParamsModel &other_model)
void init_to_size(int size, T t)
bool Equivalent(const ParamsModel &that) const