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] &&
62 !(isascii(line[end_of_key]) && isspace(line[end_of_key]))) {
65 if (!line[end_of_key]) {
66 tprintf(
"ParamsModel::Incomplete line %s\n", line);
69 line[end_of_key++] = 0;
71 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,
87 kMinFinalCost, kMaxFinalCost);
91 float epsilon = 0.0001;
93 if (weights_vec_[p].size() != that.weights_vec_[p].
size())
return false;
94 for (
int i = 0; i < weights_vec_[p].
size(); i++) {
95 if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
96 fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
105 const char *full_path) {
107 if (!fp.
Open(full_path,
nullptr)) {
108 tprintf(
"Error opening file %s\n", full_path);
115 const int kMaxLineSize = 100;
116 char line[kMaxLineSize];
124 while (fp->
FGets(line, kMaxLineSize) !=
nullptr) {
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])
bool LoadFromFp(const char *lang, TFile *fp)
void SetValue(int index, bool value)
const char * string() const
char * FGets(char *buffer, int buffer_size)
const GenericVector< float > & weights_for_pass(PassEnum pass) const
float ComputeCost(const float features[]) const
int ParamsTrainingFeatureByName(const char *name)
void init_to_size(int size, const T &t)
bool Equivalent(const ParamsModel &that) const
DLLSYM void tprintf(const char *format,...)
bool Open(const STRING &filename, FileReader reader)
bool LoadFromFile(const char *lang, const char *full_path)
bool SaveToFile(const char *full_path) const
const GenericVector< float > & weights() const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void Copy(const ParamsModel &other_model)