logistic-regression-copy.cc
Go to the documentation of this file.
1 // ivectorbin/logistic-regression-copy.cc
2 
3 // Copyright 2014 Daniel Povey
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Copy a logistic-regression model, possibly changing the binary mode;\n"
32  "also supports the --scale-priors option which can scale the prior probabilities\n"
33  "the model assigns to different classes (e.g., you can remove the effect of\n"
34  "unbalanced training data by scaling by the inverse of the class priors in the\n"
35  "training data)\n"
36  "Usage: logistic-regression-copy [options] <model-in> <model-out>\n"
37  "e.g.: echo '[ 2.6 1.7 3.9 1.24 7.5 ]' | logistic-regression-copy --scale-priors=- \\\n"
38  " 1.model scaled_priors.mdl\n";
39 
40  ParseOptions po(usage);
41 
42  bool binary = true;
43  std::string scale_priors_rxfilename;
44 
45  po.Register("binary", &binary, "Write output in binary mode");
46  po.Register("scale-priors", &scale_priors_rxfilename, "(extended) filename for file "
47  "containing a vector of prior-scales (e.g. inverses of training priors)");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 2) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string model_rxfilename = po.GetArg(1),
57  model_wxfilename = po.GetArg(2);
58 
59 
60  LogisticRegression model;
61  ReadKaldiObject(model_rxfilename, &model);
62 
63  if (scale_priors_rxfilename != "") {
64  Vector<BaseFloat> prior_scales;
65  ReadKaldiObject(scale_priors_rxfilename, &prior_scales);
66  model.ScalePriors(prior_scales);
67  }
68 
69  WriteKaldiObject(model, model_wxfilename, binary);
70 
71  KALDI_LOG << "Wrote model to " << PrintableWxfilename(model_wxfilename);
72  return 0;
73  } catch(const std::exception &e) {
74  std::cerr << e.what();
75  return -1;
76  }
77 }
int main(int argc, char *argv[])
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
void ScalePriors(const Vector< BaseFloat > &prior_scales)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
#define KALDI_LOG
Definition: kaldi-error.h:153