31 static const float kScoreScaleFactor = 100.0f;
33 static const float kMinFinalCost = 0.001f;
35 static const float kMaxFinalCost = 100.0f;
39 tprintf(
"ParamsModel for pass %d lang %s\n", p, lang_.
string());
40 for (
int i = 0; i < weights_vec_[p].
size(); ++i) {
41 tprintf(
"%s = %g\n", kParamsTrainingFeatureTypeName[i],
56 bool ParamsModel::ParseLine(
char *line,
char** key,
float *val) {
60 while (line[end_of_key] &&
61 !(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)
81 float unnorm_score = 0.0;
83 unnorm_score += weights_vec_[pass_][f] * features[f];
85 return ClipToRange(-unnorm_score / kScoreScaleFactor,
86 kMinFinalCost, kMaxFinalCost);
90 float epsilon = 0.0001;
92 if (weights_vec_[p].size() != that.weights_vec_[p].
size())
return false;
93 for (
int i = 0; i < weights_vec_[p].
size(); i++) {
94 if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
95 fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
103 const int kMaxLineSize = 100;
104 char line[kMaxLineSize];
112 while (fp->
FGets(line, kMaxLineSize) !=
nullptr) {
115 if (!ParseLine(line, &key, &value))
119 tprintf(
"ParamsModel::Unknown parameter %s\n", key);
131 tprintf(
"Missing field %s.\n", kParamsTrainingFeatureTypeName[i]);
143 tprintf(
"Refusing to save ParamsModel that has not been initialized.\n");
146 FILE *fp = fopen(full_path,
"wb");
148 tprintf(
"Could not open %s for writing.\n", full_path);
151 bool all_good =
true;
153 if (fprintf(fp,
"%s %f\n", kParamsTrainingFeatureTypeName[i],
weights[i])
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
DLLSYM void tprintf(const char *format,...)
int ParamsTrainingFeatureByName(const char *name)
@ PTRAIN_NUM_FEATURE_TYPES
void init_to_size(int size, const T &t)
void SetValue(int index, bool value)
char * FGets(char *buffer, int buffer_size)
const char * string() const
bool SaveToFile(const char *full_path) const
float ComputeCost(const float features[]) const
bool LoadFromFp(const char *lang, TFile *fp)
const GenericVector< float > & weights() const
bool Equivalent(const ParamsModel &that) const
const GenericVector< float > & weights_for_pass(PassEnum pass) const
void Copy(const ParamsModel &other_model)